⚠️ Forum Maintenance: Feb 6th, 8am - 14pm (UTC+2)

Bad ptr when passing an argument

  • 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?


  • Can you post some code?

  • 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]

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

  • 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?

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

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

  • [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?

  • 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 .

  • 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?

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

    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 ?


  • 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.

Log in to reply