[Qt-interest] Replace Qt Allocator
Dan White
ygor at comcast.net
Wed Sep 16 16:46:34 CEST 2009
Short Answer: Don't use nedmalloc with C++
According to what appears to be the nedMalloc homepage <http://www.nedprod.com/programs/portable/nedmalloc/>, it is a variant on the old, classic C function "malloc"
This is dangerously bad
Reference: <http://www.devx.com/tips/Tip/12491>
"Avoid Using malloc() and free() in C++
The use of malloc() and free() functions in a C++ file is not recommended and is even dangerous:
1. malloc() requires the exact number of bytes as an argument whereas new calculates the size of the allocated object automatically. By using new, silly mistakes such as the following are avoided:
long * p = malloc(sizeof(short)); //p originally pointed to a short;
//changed later (but malloc's arg was not)
2. malloc() does not handle allocation failures, so you have to test the return value of malloc() on each and every call. This tedious and dangerous function imposes performance penalty and bloats your .exe files. On the other hand, new throws an exception of type std::bad_alloc when it fails, so your code may contain only one catch(std::bad_alloc) clause to handle such exceptions.
3. As opposed to new, malloc() does not invoke the object's constructor. It only allocates uninitialized memory. The use of objects allocated this way is undefined and should never occur. Similarly, free() does not invoke its object's destructor."
... which pretty much describes your problems.
“Sometimes I think the surest sign that intelligent life exists elsewhere in the universe is that none of it has tried to contact us.”
Bill Waterson (Calvin & Hobbes)
----- Original Message -----
From: Eric Clark <eclark at ara.com>
To: Qt Interest (qt-interest at trolltech.com) <qt-interest at trolltech.com>
Sent: Wed, 16 Sep 2009 14:31:42 +0000 (UTC)
Subject: [Qt-interest] Replace Qt Allocator
Hello All,
Our program uses a system allocator called nedmalloc. We override the new and delete operators to use this system allocator. However, after porting to Qt 4 we have ran into some issues with the QList class and it's destructor. When the list is destructed, it attempts to free the memory created by the list, but nedfree() crashes because the memory was instantiated using the system allocator, malloc(). Here is the problem:
The problem is indeed a violation of the ODR requirement in the ISO C++ spec. For example in this line:
QModelIndexList indexes = list->selectionModel()->selectedRows();
QModelIndexList exists in the local binary due to being a templated type based on QList, therefore its destructor is called from the local binary where ::operator delete has been defined to invoke nedmalloc. Unfortunately the initial constructor of the list (selectedRows()) exists in one of the Qt DLL binaries where ::operator new is defined to invoke the system allocator. Therefore the list is being constructed with the system allocator and destructed with nedmalloc - this obviously enough provokes a segfault as ODR is violated.
I was told by some guys on this interest board that I should build nedmalloc into Qt. However, after doing so, I still get the same error. What I did to build nedmalloc into Qt, was I modified the body of qFree(), qMalloc(), and qRealloc() to use nedfree(), nedmalloc(), and nedRealloc() respectively. This apparently was not enough to fix the problem. Could anyone tell me what else needs to be done? Do I need to override the new and delete operators in Qt as well? Are there any direct calls to malloc() in the code that I am unaware of?
Any help with this would be greatly appreciated!
Eric
More information about the Qt-interest-old
mailing list