3

I've got a problem. I use the following Code to convert a std::string into a QString.

std::string testStdStr = "Hello";
QString test = QString::fromStdString(testStdStr);

This code throws a bad_alloc exception unter MSVC 2013 Prof, but only in DEBUG Mode! If I compile in Release Mode everything works as expected.

Some Additional Information: SUBSYSTEM is changed to Windows AND the Entry Point is set to mainCRTStartup

The exception is thrown at the point of initialization of the QString Object! Any suggestions? If you need additional Information, just ask!

Kevin H. Klein
  • 403
  • 1
  • 5
  • 14
  • What Qt version are you using? – mfuchs Jun 07 '15 at 19:01
  • Thanks for your Answer! 5.4 msvc 32 bit – Kevin H. Klein Jun 07 '15 at 19:01
  • I added some Information – Kevin H. Klein Jun 07 '15 at 19:08
  • Did you try the debugger? To me it looks like the call to `d = Data::allocate(size + 1);` fails in [this](http://code.woboq.org/qt5/qtbase/src/corelib/tools/qstring.cpp.html#1495) constructor and `Q_CHECK_PTR(d);` leads to an exception because of that. I wonder what the actual `size` value was in this case. I guess you need to check where `QArrayData::allocate` returned `0` in your case and why. – mfuchs Jun 07 '15 at 19:17
  • Also of interest is what other allocations happen in your program. Maybe there really is no memory left, while more information is stored in ram when the executable is compiled in debug mode. – mfuchs Jun 07 '15 at 19:23
  • Thanks f.y.A.! There are not much more allocations, because it is just a simple App and this is the only real code which gets executed besides the initialization of the qt stuff. The Taskmanager sais that the App uses 2,7 Mb of ram while the exception occurs. I will debug it again and answer afterwords. Maybe my explanations gave you a hint.. – Kevin H. Klein Jun 07 '15 at 19:41
  • Does `QString test = "foo";` work? Are you 100% sure Qt was compiled with the same runtime library as the one you are compiling your app with? – jpo38 Jun 07 '15 at 20:28
  • This error only occurs when I try to convert from std::string. Your example works. The strange thing is that the exception is only thrown in debug mode. If the app is compiled in release mode it works. – Kevin H. Klein Jun 07 '15 at 20:36
  • Are you sure that MSVCRT is already initialized? – Dmitry Sazonov Jun 08 '15 at 01:12
  • How do you mean that? I used the precompiled builds from qt from the website.. – Kevin H. Klein Jun 08 '15 at 08:44

1 Answers1

6

This happens when you mix debug libraries and release libraries, check that all the libraries you link are in debug version (qt5cored.lib [not qt5core.lib], qtmaind.lib etc...)

Nadir
  • 101
  • 6
  • Yes. For example, I had this issue because I linked release libraries of QT (qt5core.dll) and the debug Runtime Library (compiler option "/MDd" was set instead of "/MD" in MS VisualStudio). – user74696c May 13 '20 at 07:19