PyCObject & malloc creating memory leak

Tom Conneely tom.conneely at
Thu Sep 30 14:29:17 CEST 2010

I'm posting this last message as I've found the source of my initial
memory leak problem, unfortunately it was an embarrassingly basic
mistake. In my defence I've got a horrible cold, but I'm just making

I begin by mallocing the memory, which gives me a pointer "foo" to
that memory:
    char *foo = PyMem_Malloc(1024 * sizeof(char));

then assign a value to it:
    foo = "foo";

of course what this actually does is change the pointer to point to a
new memory address containing a constant "foo". Hence, when I free the
memory in the PYCObject's destructor, the pointer is for the constant
"foo", not the memory I initially allocated.

I only posted this to help people searching, sorry for the noise.

Tom Conneely

More information about the Python-list mailing list