[Development] QtCore missing check for memory allocation
ulf.hermann at theqtcompany.com
Wed Feb 25 13:35:17 CET 2015
> 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.
> Is QT_NO_DEBUG really disabling the check for valid memory allocation?
You need QT_NO_EXCEPTIONS and QT_NO_DEBUG for Q_CHECK_PTR to be a qt_noop(). I can't say I like this situation, but the point seems to be that you cannot throw bad_alloc if you've compiled without exceptions. I would argue for just crashing in this case, by accessing a known-bad address. That's quite a behavior change, of course.
More information about the Development