https://github.com/python/cpython/blob/master/Include/objimpl.h#L83 says
PyObject_Realloc(p != NULL, 0) does not return NULL, or free the memory at p.
PyObject_Realloc doesn't free the memory.
I've noticed a memory leak in my program which uses PyObject_Realloc every now and then.
I tried to fix it using this:
PyLongObject *new = (PyLongObject *) PyObject_Realloc(obj, new_size);
if (new == NULL) {
return PyErr_NoMemory();
}
if (new != obj) {
PyObject_Free(obj);
}
obj = new;
But now I get the malloc error pointer being freed was not allocated
How am I meant to make sure my program doesn't leak memory when using PyObject_Malloc and PyObject_Realloc?