Skip to content

Ensure safe dynamic memory allocation

Paul Hapke requested to merge fix/add-memory-error-for-failed-mallocs into develop

This is a collections of changes to prevent undefined behavior due from memory allocation:

  • After every call of malloc or calloc the resulting pointer should be checked to not be NULL, in case allocation failed.
  • In a class memory allocation should happen in __cinit__ methods, not in __init__ as only the former is guaranteed to be called exactly once in the object creation
  • Check if all memory allocated in __cinit__ is freed in __dealloc__.
  • Make sure that raised MemoryErrors propagate and get handled. If an exception occurs in a void function, the exception may be silently swallowed. See here and here for the relevant Cython documentation.
Edited by Paul Hapke

Merge request reports