[Development] QtCore missing check for memory allocation

Ulf Hermann ulf.hermann at theqtcompany.com
Wed Feb 25 17:50:33 CET 2015


> The commonly accepted solution to that problem is using memory debuggers
> like Valgrind. They are sufficiently sophisticated to tell you exactly
> where your bad pointer comes from. Ideally you have the resources to let
> your CI run your automated tests with such debugger.

Bad code which accesses invalid memory locations without triggering allocation failures is not my problem here.

> Writing allocation-safe code was considering a good idea years ago.
> Sadly the people supporting that approach totally forgot that checking
> each memory allocation dramatically inflates cyclomatic complexity of
> your code, rendering it unmaintainable quickly. Have a look at libdbus
> to get an idea of the overhead allocation-safe code causes.

If we just consider any allocation failure as fatal we can get by with exactly one more line of code per malloc. We can also write a malloc wrapper to do that for us and end up with virtually no added code (not considering operator new with nothrow for now). We don't need to handle allocation failures gracefully; we've never done that. We shouldn't tolerate undefined behavior on allocation failures, though.

Ulf



More information about the Development mailing list