[Development] QtCore missing check for memory allocation

Christian Kandeler christian.kandeler at theqtcompany.com
Wed Feb 25 17:20:54 CET 2015


On 02/25/2015 04:30 PM, Giuseppe D'Angelo wrote:
> Il 25/02/2015 13:35, Ulf Hermann ha scritto:
>>> I noticed that in qglobal.h Q_CHECK_PTR may be a noop in case
>>> QT_NO_DEBUG is set. Q_CHECK_PTR is used to check if memory allocations
>>> succeeded (e.g. QVector::reallocateData).
>>
>> Until 9d44645eae144fcfefa0de2455d41f04d29c40d4 (September 2014) most
>> of QVector's allocations weren't checked at all and surprisingly no
>> one had complained about that before I did. The common theme is "If
>> you need so much space you better design your own data structure". I
>> find that argument lacking because memory allocation can fail for a
>> number of reasons, not only because you have requested a too large
>> single chunk of memory. Furthermore people keep saying "What can we do
>> if we detect a failed memory allocation? Qt is in an invalid state
>> then and we have to crash anyway." I somewhat agree to that, but we
>> should really crash reliably without writing or reading random user
>> memory before.
>>
>> We should thus do Q_CHECK_PTR on every memory allocation in Qt and we
>> should fix Q_CHECK_PTR so that it works under all circumstances.
>
> That's a much bigger commitment than changing QVector, though. All of
> Qt's codebase assumes infinite memory, so that allocations never fail.
> In other words, only a handful of places currently check for such OOM
> conditions, and it's unclear what should happen in case a OOM is
> detected (apart from crashing).

Also, you are not even guaranteed to get a null pointer/bad_alloc due to 
things like Linux overcommitting.


Christian



More information about the Development mailing list