Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QString::toStdString() crashes on std::string destructor

I've been debugging this for 2 hours now, and it boils down to this. If I call QString::toStdString

QString s = "testtesttesttesttesttest";
const std::string &temp = s.toStdString();

the program later crashes on std::string destructor

__CLR_OR_THIS_CALL ~basic_string()
    {   // destroy the string
    _Tidy(true); // <---- It crashes on this line.
    }

I thought it was memory corruption at first, but this happens even if main() contains only those 2 lines. Does anyone know why this happens, and also how can I fix it?

My Qt version is 4.8.1.

like image 934
sashoalm Avatar asked Mar 25 '13 09:03

sashoalm


2 Answers

Your Qt DLLs need to be compiled with STL support and exactly the same C-Runtime Library as your code. It looks as though you are using two different CRTs at the same time, which would destroy the objects created on one heap by Qt into the heap used by your program.

Check the DLL Usage with the Dependency Walker!

like image 114
Jens Avatar answered Nov 09 '22 12:11

Jens


Most probable reason could be that your Runtime Library is "Multi-threaded (/MT)" and you need to change it to "Multi-threaded DLL (/MD)" (if you are on the release version)

If you are on the debug version change from "Multi-threaded Debug (/MTd)" to "Multi-threaded Debug DLL (/MDd)"

If you have an odd compilation of Qt, the solution should be the opposite.

You will find that on "Configuration properties->C/C++->Code Generation->Runtime Library"

like image 22
Andres Hurtis Avatar answered Nov 09 '22 13:11

Andres Hurtis