I’m looking at Array::add and trying to figure out what happens if for some reason memory allocation fails. From my reading of the code (assuming neither JUCE_DEBUG nor JUCE_DLL is defined though I don’t think this assumption is super important), the following is possible:
[code]- something calls Array::add
- Array::add calls ArrayAllocationBase::ensureAllocatedSize
- ArrayAllocationBase::ensureAllocatedSize calls ArrayAllocationBase::setAllocatedSize
- ArrayAllocationBase::setAllocatedSize calls HeapBlock::realloc
- HeapBlock::realloc calls ::juce_malloc (or ::juce_realloc – either one has the same struggle below from what I can see)
- juce_Memory.h defines juce_malloc as malloc
- malloc fails
- the data member variable in HeapBlock is NULL
- This line of code in Array::add uses memory that it shouldn’t:
new (data.elements + numUsed++) ElementType (newElement);[/code]data.elements is a HeapBlock. I’m not sure which overloaded operator on HeapBlock is relevant here, but as far as I can tell either NULL is getting dereferenced or (NULL + some small integer).
Am I reading the code right? Seems like Array::add should either return bool (or something else to indicate failure) or throw an exception. To do that, ArrayAllocationBase would need to change, and possibly HeapBlock as well.
Are you interested in a patch to communicate this failure to callers of Array::add?