Ensure safe dynamic memory allocation
This is a collections of changes to prevent undefined behavior due from memory allocation:
- After every call of
malloc
orcalloc
the resulting pointer should be checked to not beNULL
, 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