[Solved] QString.toStdString() and all other toXXX methods occur the segment fault.



  • In my codes, all QString conversion methods do not work correctly.

    e.g. -

    @
    QString qstr = "string";
    std::string stdstr = qstr.toStdString(); //invalid address pointer
    char* pszstr = qstr.toUtf8(); //invalid address pointer , too
    char* pszstr2 = qstr.toAscii(); //invalid address pointer , too
    char* pszstr3 = qstr.toLocal8Bit(); //invalid address pointer ,too
    @

    When I debugged into these methods, the return objects are destroyed just before calling operator=.

    these codes works fine on my Mac. but with VC2010 compiler on Windows, the scope of the objects of the return values of these methods are destroyed before copied.
    Why MS VC2010 compiler destroys the object of the return value before it is copied?

    EDIT: please use @-tags to highlight code blocks



  • Are you sure, they are destroyed before they are copied? Normally, they are destroyed directly after the line of execution, which means when the IP goes to the next line. all char* variables are invalid in the next line, that is correct, as these returned objects are temporary.

    And the return values are not char*, toUtf8 returns a QByteArray.

    what should be possible (and is also used by me):

    @
    QString qstr = "string";

    foo(qstr.toUtf8().constData());
    

    @

    inside foo, the char* is valid.

    What could be, if the variables are not used after that calls, is that the compiler optimizes them out of the binary, as they are useless :-)



  • Yes, I'm sure that they are destroyed before they are copied.
    I downloaded Qt libraries 4.7.2 for Windows (VS 2008, 218 MB) from the web page,
    and My codes are compiled by VS 2010 with QT Creator.
    Is there any problem if my codes are VS 2010 and Qt DLLs are VS 2008?

    It's really strange. I'll capture and post the screen shots of debugging steps tomorrow.



  • YES, that is forbidden.

    The different MSVS versions use different heap versions (msvcrt*.dll, found in c:\windows\winsxs...).
    You MUST use the Qt libs compiled with the same MSVS version as you use to compile your binaries. That explains, why it does not work.

    If you want to use MSVS 2010, you have to recompile Qt from the source package.



  • Thank you so much!
    I need to study more about compilers.
    Is it because the memory allocated in a DLL is located in the DLL's segment area?
    It makes sense. My Qt DLL allocated return object into the stack segment in MSVCRT90.dll and my codes can access only in MSVCRT100.dll that's why my codes cannot access the return value of QString.

    Thank you again.

    BTW, how can I change my profile photo on right?



  • Actually, you can tell VS2010 to go into 2008 compatibility mode (don't ask me how, though).

    For the profile photo you need a gravatar.com account.



  • [quote author="augiekim" date="1304001473"]
    BTW, how can I change my profile photo on right?[/quote]

    here is a "how to on using gravatar":http://developer.qt.nokia.com/wiki/HowToUseGravatar



  • Thank you again!



  • I was wondering how the return objects can alive even though the call stacks including their frames are destroyed when these function calls are over.
    I've just found a nice explanation about it. I didn't know the detail of "tail call" and this page helped me.

    "Tail call - Wikipedia":http://en.wikipedia.org/wiki/Tail_call


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.