Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [Solved] QString.toStdString() and all other toXXX methods occur the segment fault.
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved General and Desktop
9 Posts 3 Posters 8.5k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    augiekim
    wrote on 28 Apr 2011, 13:14 last edited by
    #1

    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

    1 Reply Last reply
    0
    • G Offline
      G Offline
      giesbert
      wrote on 28 Apr 2011, 13:20 last edited by
      #2

      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 :-)

      Nokia Certified Qt Specialist.
      Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

      1 Reply Last reply
      0
      • A Offline
        A Offline
        augiekim
        wrote on 28 Apr 2011, 13:52 last edited by
        #3

        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.

        1 Reply Last reply
        0
        • G Offline
          G Offline
          giesbert
          wrote on 28 Apr 2011, 14:02 last edited by
          #4

          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.

          Nokia Certified Qt Specialist.
          Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

          1 Reply Last reply
          0
          • A Offline
            A Offline
            augiekim
            wrote on 28 Apr 2011, 14:37 last edited by
            #5

            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?

            1 Reply Last reply
            0
            • F Offline
              F Offline
              Franzk
              wrote on 28 Apr 2011, 14:43 last edited by
              #6

              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.

              "Horse sense is the thing a horse has which keeps it from betting on people." -- W.C. Fields

              http://www.catb.org/~esr/faqs/smart-questions.html

              1 Reply Last reply
              0
              • G Offline
                G Offline
                giesbert
                wrote on 28 Apr 2011, 15:18 last edited by
                #7

                [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

                Nokia Certified Qt Specialist.
                Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

                1 Reply Last reply
                0
                • A Offline
                  A Offline
                  augiekim
                  wrote on 29 Apr 2011, 00:50 last edited by
                  #8

                  Thank you again!

                  1 Reply Last reply
                  0
                  • A Offline
                    A Offline
                    augiekim
                    wrote on 29 Apr 2011, 02:23 last edited by
                    #9

                    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

                    1 Reply Last reply
                    0

                    1/9

                    28 Apr 2011, 13:14

                    • Login

                    • Login or register to search.
                    1 out of 9
                    • First post
                      1/9
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved