Results 1 to 10 of 10

Thread: char* to QString. Segfault after delete []

  1. #1
    Join Date
    Jun 2006
    Location
    Sweden
    Posts
    99
    Thanks
    11
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default char* to QString. Segfault after delete []

    Hello,

    In the application I'm working on, i've got a class CfgMgr with a method: getValue() that's supposed to return a QString:

    Qt Code:
    1. QString CfgMgr::getValue()
    2. {
    3. QString qstrValue;
    4. char* cstr = 0; // null pointer
    5. if (Get_Value(&cstr)){ // external call that allocates memory using 'new'
    6. qstrValue = cstr; // copies the data?
    7. delete [] cstr;
    8. }
    9. return qstrValue;
    10. }
    11.  
    12. QString myValue = CfgMgr::getValue();
    To copy to clipboard, switch view to plain text mode 

    This works fine most of the time, but sometimes the application segfaults at qatomic_i386.h:80 after coming from QString:perator=(..)

    I suspect that the problem is one of dangling pointers. I tried doing the following since QByteArray supposedly performs a deep copy:

    Qt Code:
    1. QString CfgMgr::getValue()
    2. {
    3. QString qstrValue;
    4. char* cstr = 0; // null pointer
    5. if (Get_Value(&cstr)){ // external call that allocates memory using 'new'
    6. QByteArray ba(cstr); // deep copy
    7. delete [] cstr;
    8. qstrValue = ba;
    9. }
    10. return qstrValue;
    11. }
    12. QString myValue = CfgMgr::getValue();
    To copy to clipboard, switch view to plain text mode 

    Unfortunately, this does not seem to work either. This might just be the symptom of a problem created elsewhere but it's not unlikely that i've made a mistake here either.

    Does anyone see any obvious errors ?

    Any help would be greatly appreciated.

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: char* to QString. Segfault after delete []

    The first version is correct. Can you post the GetValue function?

  3. #3
    Join Date
    Jun 2006
    Location
    Sweden
    Posts
    99
    Thanks
    11
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: char* to QString. Segfault after delete []

    Unfortunately i can't since the Get_Value function is part of an external library (of which i don't have the source) that performs reading and writing of configuration data to file. All i've got to go on is that the documentation says it does 'new' on the char* buffer i provide and copies the config data into it.

    I feel pretty comfortable assuming the Get_Value function does what it says it does and that the problem lies in my code somewhere. But if it's not with my use of QString and char* then i'm at a total loss.

  4. #4
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: char* to QString. Segfault after delete []

    What does Get_Value returns? Is it a bool or the length of the string? Check if the allocated char* is null terminated after Get_Value returns...

  5. #5
    Join Date
    Jun 2006
    Location
    Sweden
    Posts
    99
    Thanks
    11
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: char* to QString. Segfault after delete []

    Get_Value returns a bool signifying if the operation succeeded or not.

    I'm a bit uncertain about how to tell if the string is null-terminated. QString and QByteArray don't null-terminate and strlen(...) doesn't count the null-character. Suggestions?

    But like i said earlier, i'm fairly certain the library ensures the buffer is filled correctly.

  6. #6
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: char* to QString. Segfault after delete []

    if it segfaults where you said it does then it certainly must be something wrong with the buffer.
    is the library running any additional thread(s)?

    anyway, this situation is very common to buffer overruns (because of the non-null terminated string, in QString:perator=).
    sometimes it crahses because it attempts to read memory beyond the limits of the buffer, allocated by other objects.

  7. #7
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: char* to QString. Segfault after delete []

    Actually, I think the crash in QString:perator = occurs when returning the QString.
    Just for curiosity, can you try:
    Qt Code:
    1. qstrValue = QString::fromAscii(cstr);
    To copy to clipboard, switch view to plain text mode 
    instead of what you have now?

    I'm too lazy to take a look in the QString(const char*) constructor now... Oh, and a lot of work to do too .

  8. #8
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: char* to QString. Segfault after delete []

    Quote Originally Posted by marcel View Post
    Actually, I think the crash in QString:perator = occurs when returning the QString.
    Just for curiosity, can you try:
    Qt Code:
    1. qstrValue = QString::fromAscii(cstr);
    To copy to clipboard, switch view to plain text mode 
    instead of what you have now?

    I'm too lazy to take a look in the QString(const char*) constructor now... Oh, and a lot of work to do too .
    neah, that's not it .

  9. #9
    Join Date
    Jun 2006
    Location
    Sweden
    Posts
    99
    Thanks
    11
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: char* to QString. Segfault after delete []

    yeah, the char* constructor and the operator= method both result in calls to fromAscii().

    I suspected that the QByteArray solution should have worked and also that it didn't because i hadn't replaced all the calls to Get_Value, and some earlier call might have been the actual cause of the crash. So now i'm using QByteArray everywhere and it seems to be working, but on the other hand, it might just be dumb luck because the QString solution was working most of the time earlier as well.

    Having taken a look at the code for QString i can't find that it copies the buffer at any time. I've followed the code to a call to a method called iconv(..) but the specification of iconv doesn't explicitly say that a copy is performed. This leads me to believe that using QByteArray must be the only safe route since it explicitly performs a deep copy.

    What do you think?

  10. #10
    Join Date
    Jun 2006
    Location
    Sweden
    Posts
    99
    Thanks
    11
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: char* to QString. Segfault after delete []

    I looked more closely at the QString code and found that it indeed performs a deep copy after all. My mistake. I ran my binary through Valgrind and it complained about an illegal write at the strangest place; setting a private integer class member to 0. The problem simply must lie elsewhere. I'm beginning to suspect my make system since it keeps whining about clock skew. Maybe i'm getting a strange build as a result.

    Thanks for your help marcel.

Similar Threads

  1. easiest Way QString can do
    By baray98 in forum Qt Programming
    Replies: 12
    Last Post: 15th April 2008, 21:49
  2. Convert from iso-8859-1 to... Something else :-)
    By Nyphel in forum Qt Programming
    Replies: 4
    Last Post: 7th March 2007, 18:59
  3. c++, placement delete upon exception
    By stinos in forum General Programming
    Replies: 6
    Last Post: 31st October 2006, 16:38
  4. QSqlQueryModel + set Write
    By raphaelf in forum Qt Programming
    Replies: 7
    Last Post: 5th June 2006, 09:55
  5. Converting QString to unsigned char
    By salston in forum Qt Programming
    Replies: 3
    Last Post: 24th April 2006, 23:10

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.