[Development] QtCore missing check for memory allocation

Al-Khanji Louai louai.al-khanji at theqtcompany.com
Tue Mar 10 09:55:13 CET 2015


It's my understanding that on Windows you link the global operator new/delete replacement into every dll separately.

A howto:

1) Implement your replacement global operator new and delete. Just do so in a single .cpp file, you don't need a header.
2) Compile this into a static library.
3) Pass the static library to the configure script as an extra library so that it is linked into every Qt .dll.

If you want, also link it into your main .exe or just add the .cpp directly. You don't need to link to Qt statically, but you do need to compile Qt yourself in order to statically link into every dll your custom implementation of global operator new & delete.

Now, replacing malloc/free on Windows is non-trivial. To my knowledge, your best bet would be to use detours [1], but I have never tried it.

Cheers,
Louai

[1] http://research.microsoft.com/en-us/projects/detours/

________________________________________
From: development-bounces+louai.al-khanji=theqtcompany.com at qt-project.org <development-bounces+louai.al-khanji=theqtcompany.com at qt-project.org> on behalf of Koehne Kai <Kai.Koehne at theqtcompany.com>
Sent: Tuesday, March 10, 2015 10:18 AM
To: Alex Montgomery; Knoll Lars
Cc: development at qt-project.org
Subject: Re: [Development] QtCore missing check for memory allocation

> -----Original Message-----
> From: development-bounces+kai.koehne=theqtcompany.com at qt-
> project.org [mailto:development-
> bounces+kai.koehne=theqtcompany.com at qt-project.org] On Behalf Of
> Alex Montgomery
> Sent: Monday, March 09, 2015 6:56 PM
> To: Knoll Lars
> Cc: development at qt-project.org
> Subject: Re: [Development] QtCore missing check for memory allocation
>
> On Mon, Mar 9, 2015 at 12:57 AM, Knoll Lars
> <Lars.Knoll at theqtcompany.com> wrote:
>
> > Yes, the best solution IMO is still to use your own malloc and
> > operator new replacements. In addition, OOM handlers on the OS level can
> help.
>
> Except that dynamically linked Windows Qt applications (read: most) don't
> work this way, so Windows users are left out in the cold. DLLs do not allow
> you to simply replace one new operator across link boundaries. See the
> comments in this Qt bug:
> https://bugreports.qt.io/browse/QTBUG-37395

True.

> Is there any strategy for the large body of Qt developers targeting Windows,
> or is statically linking the only supported method for replacing Qt allocators
> on Windows?

You almost definitely need to recompile Qt , because you can't really override operator new/delete across DLL boundaries (as you already pointed out in the bug report).

Question is whether you can easily force all Qt modules to use a custom operator new / delete ... I've seen suggestions [1] to define an inline operator new / delete (e.g. in qglobal.h), but that seems to violate the standard explicitly stating that "The program's definitions shall not be specified
as inline " (C++14 17.6.4.3.3 3). Might nevertheless work though, since there's a high chance that qglobal.h is included in all places where Qt allocates/deallocates memory...

Regards

Kai

[1]: https://social.msdn.microsoft.com/Forums/vstudio/en-US/ab642c88-2d2d-4f5d-9fd7-2341442d5a46/replacing-new-and-delete-how-to-export-the-implementation?forum=vclanguage)

_______________________________________________
Development mailing list
Development at qt-project.org
http://lists.qt-project.org/mailman/listinfo/development


More information about the Development mailing list