[Qt-interest] Replace Qt Allocator

Daniel Albuschat d.albuschat at gmail.com
Mon Sep 21 08:51:13 CEST 2009


2009/9/17 Thiago Macieira <thiago at kde.org>:
> Em Quarta-feira 16. Setembro 2009, às 23.13.13, Eric Clark escreveu:
>> The main reason for this post was to understand why Qt offers the ability
>>  to "drop-in" a system allocator by changing the source of qFree(),
>>  qMalloc() and qRealloc(), if it is not the only thing that needs to be
>>  done? I also have to add the global new and delete as mentioned before.
>>  This could be easily fixed if Qt had already overridden those operators to
>>  invoke the qFree() and qMalloc() that exist in Qt. Then I would have only
>>  had to swap out the source of those functions, build QtCore and everything
>>  would work just fin
>
> You can easily override qMalloc, but just as you did to your source code, you
> need to override ::operator new() too.
>
> It was never claimed otherwise.

And there's good reason for this. Qt shouldn't mess too much with
memory allocation and global operators because this might break other
techniques, like overriding operator new and operator delete yourself
(e.g. for debugging purposes instead of optimizations).
How does nedmalloc replace the system malloc? The only actual safe way
to do this is hooking *all* calls to malloc/realloc/free of the whole
application to the optimized implementations. Everything else needs to
be handled with care, as you can see. Especially using #define for
that task...

By the way, qMalloc and friends are not even documented (except for a
small, un-published comment in qmalloc.cpp), so you obviously can't
rely on them.

But there's one thing I agree on. When a class does new an object (or
call malloc), you should make sure that when this code is in the
header file, the corresponding delete must go in the header file, too.
And when the new is in the implementation-fiel, the delete must be in
the implementation-file, too. That way even half-assed techniques like
#define'ing malloc should work reasonably well.

Regards,

Daniel Albuschat

-- 
eat(this); // delicious suicide




More information about the Qt-interest-old mailing list