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

c++, crash, qt, visual-studio

Solution

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!

Problem

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

Original source