geogram icon indicating copy to clipboard operation
geogram copied to clipboard

vector segfaults on out-of-memory error

Open Yitzi2 opened this issue 2 years ago • 2 comments

If the system does not have enough memory to allocate a GEO::vector, it will segfault. The reason for this is that GEO::vector is essentially an std::vector that uses the Geogram aligned allocator, and the contract for allocators for STL container classes requires that if they return (i.e. do not throw) they return a valid block of memory (https://stackoverflow.com/questions/4826838/do-standard-library-stl-containers-support-a-form-of-nothrow-allocation).

The Geogram aligned allocator does not fulfill this contract; if it cannot allocate, aligned_malloc will return 0, and this will be returned by the allocator, causing a segfault.

This could be fixed by having the allocate() method check for nullptr and throw std::bad_alloc if it is; that way, the contract will be fulfilled, and an out-of-memory will throw an exception that can be caught higher up.

Yitzi2 avatar Aug 09 '22 15:08 Yitzi2

Is this really a big problem ? It is possible to do so, but it will slow down certain operations significantly ...

BrunoLevy avatar Nov 28 '22 16:11 BrunoLevy

  • [ ] TODO: measure what would be the impact on performance under Linux / Mac / Windows

BrunoLevy avatar Feb 19 '23 15:02 BrunoLevy