blackd0t
blackd0t

Reputation: 439

Qt crashes when using QList (heap corruption)

I've built Qt from sources with MSVC2008 as it was recommended to avoid any incompatibility issues. I'm having a problem though when using Qt functions that return QList objects.

QList<QNetworkInterface> *netInterfaces = new QList<QNetworkInterface>;
*netInterfaces = QNetworkInterface::allInterfaces();
delete netInterfaces;

This code will always crash on "delete netInterfaces" in free.c when trying to free memory. It states the heap is corrupted and I have no idea why, as everything was compiled with the same version of MSVC.

It happens for all Qt functions that return QList objects even when I just call the function:

QNetworkInterface::allInterfaces();

With the code above, application will always crash immediately when trying to free heap allocated by Qt. Weird thing is, this only happens with the Debug build. Release build works fine without any crashes.

My Qt 4.8.0 was compiled with /MT (Multi-threaded) and I link to the library dynamically. My application is also compiled as Multi-threaded.

Does anyone know what could be the possible cause of the issue here?

Upvotes: 1

Views: 2862

Answers (1)

blackd0t
blackd0t

Reputation: 439

I've found the solution to my problem. Apparently Qt has it's own set of heap allocators and while I had a Multi-threaded (/MT) build and a shared library, Qt would allocate heap using their own memory handlers in the DLL. After this memory was allocated by Qt, my application was trying to free these memory blocks in my own code which resulted in crashes as heap structure differed between Qt and the main application.

I've rebuilt Qt with /MT flag, but now I produced a static library instead of the shared one. After I linked Qt statically with my application all heap problems magically went away.

I may be wrong a bit with my explaination here, but I hope at least the solution will help some people who find this thread. I've seen quite a few people having this problem on the internet and the key is to actually understand that you shouldn't link shared library when it's built with /MT flag. /MD would be totally fine.

Here is the thread I found that enlightened me: http://qt-project.org/forums/viewthread/16513

Cheers!

Upvotes: 2

Related Questions