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. Bad ptr when passing an argument
QtWS25 Last Chance

Bad ptr when passing an argument

Scheduled Pinned Locked Moved General and Desktop
11 Posts 3 Posters 6.1k Views
  • 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.
  • T Offline
    T Offline
    TuringMachine
    wrote on last edited by
    #1

    I have Built a project that uses Qt with visual studio 2010 .
    The configuration is Debug and the platform is win32.

    During one of my project methods i want to convert std::string to QString using :
    inline QString QString::fromStdString(const std::string &s)

    But the reference argument "s" is passed as a BAD PTR.
    Please , what can possibly cause this?

    Thanks.

    1 Reply Last reply
    0
    • Q Offline
      Q Offline
      qxoz
      wrote on last edited by
      #2

      Can you post some code?

      1 Reply Last reply
      0
      • T Offline
        T Offline
        TuringMachine
        wrote on last edited by
        #3

        This is part of my code :

        @
        std::string TempStr=pItem->GetProperty(Item::PROPERTY_ID_TITLE).ToString();
        QString &QS=QString::fromStdString(TempStr);
        @

        And this is qstring.h code :

        @
        inline QString QString::fromStdString(const std::string &s)
        { return fromAscii(s.data(), int(s.size())); }
        @

        When the argument is passed from my code to QString::fromStdString it's a bad ptr .

        [EDIT: code formatting, please wrap in @-tags, Volker]

        1 Reply Last reply
        0
        • T Offline
          T Offline
          TuringMachine
          wrote on last edited by
          #4

          i forgot to mention that i'm using Qt 4.7.1.

          1 Reply Last reply
          0
          • K Offline
            K Offline
            koahnig
            wrote on last edited by
            #5

            You should tag your code with '@' at the start and at the end.

            @
            std::string TempStr=pItem->GetProperty(Item::PROPERTY_ID_TITLE).ToString();
            QString &QS=QString::fromStdString(TempStr);
            @

            What is the reference before QS for?

            Try:
            @
            std::string TempStr=pItem->GetProperty(Item::PROPERTY_ID_TITLE).ToString();
            QString QS=QString::fromStdString(TempStr);
            @

            Vote the answer(s) that helped you to solve your issue(s)

            1 Reply Last reply
            0
            • T Offline
              T Offline
              TuringMachine
              wrote on last edited by
              #6

              No that's not the problem.
              The problem is with the passing of TempStr.

              1 Reply Last reply
              0
              • K Offline
                K Offline
                koahnig
                wrote on last edited by
                #7

                [quote author="TuringMachine" date="1322736478"]No that's not the problem.
                The problem is with the passing of TempStr.[/quote]

                Did you try?
                You have a reference to a most likely temporary object. If the compiler does not change this because it is obvious to the compiler as well, this might be the problem.

                Did you check in the debugger that TempStr is holding the information you expect?

                Vote the answer(s) that helped you to solve your issue(s)

                1 Reply Last reply
                0
                • T Offline
                  T Offline
                  TuringMachine
                  wrote on last edited by
                  #8

                  Hi,
                  Yes i did try (it was actually the original code and i changed it to be a reference thinking it might solve the problem).

                  And yes i did check with the debugger that TempStr is holding the right thing...and it is .

                  1 Reply Last reply
                  0
                  • K Offline
                    K Offline
                    koahnig
                    wrote on last edited by
                    #9

                    Out of curiosity I have just checked:
                    @
                    std :: string teststr = "testtest";
                    QString QS = QString :: fromStdString( teststr );
                    @
                    in debug with VS2005 it does not give any problems.

                    You are using vs2010 with Qt 4.7.1 which has not been supplied officially as binaries for vs2010.
                    Did you do the compilation yourself?
                    If you would use vs2010 with vs2008 or vs2005 binaries you should not get this far to my understanding.

                    Did you compile Qt with stl support?

                    Vote the answer(s) that helped you to solve your issue(s)

                    1 Reply Last reply
                    0
                    • T Offline
                      T Offline
                      TuringMachine
                      wrote on last edited by
                      #10

                      Hi,
                      The application was originally built by VS2005 and worked fine (but then i used Qt 4.6.3).
                      Now we are migrating to VS2010.
                      I compiled Qt 4.7.1 source using this guide :
                      http://www.holoborodko.com/pavel/2011/02/01/how-to-compile-qt-4-7-with-visual-studio-2010/

                      like it said i used the commands (in VS2010 command line) :
                      configure -debug-and-release -opensource -platform win32-msvc2010
                      ..\jom\jom.exe -j 4

                      [quote author="koahnig" date="1322748557"]

                      Did you compile Qt with stl support? [/quote]

                      what do you mean?
                      How do i do that ?

                      Thanks.

                      1 Reply Last reply
                      0
                      • K Offline
                        K Offline
                        koahnig
                        wrote on last edited by
                        #11

                        the configure has an option -stl which is needed to be switched for using fromStdString and toStdString routines.
                        I have just checked and it is a default value for the configuration. I had remembered that I had to set it explicitely somewhere (probably when compiling for win ce).
                        However, if stl support is not on, you have received an error message during compilation. So, since it is default, it is a long shot anyhow.

                        Vote the answer(s) that helped you to solve your issue(s)

                        1 Reply Last reply
                        0

                        • Login

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