Python/C - Memory-Management

Gustavo Niemeyer niemeyer at conectiva.com
Thu Mar 7 13:19:25 EST 2002


[...]
> If another function calls init_mem, it can use memory until free_mem
> is called... I want to do the same now in Python, but it seems as if
> the static memory isnt kept somewhere? When I try accessing it, I get
> a Segmentation Fault... Any suggestions?

o  Wrap your functions as a C type method.
o  Keep a static reference counter of the number of objects of this
   type alive.
o  Increase the counter every time a new object is born.
o  Decrease the counter every time an object dies.
o  When the counter goes from 0 to 1, allocate your memory.
o  When the counter goes from 1 to 0, deallocate it.
o  Keep in mind that if these functions share a common memory area,
   they may overlap each other, and crash your application, depending
   on the kind of operation they're doing.

-- 
Gustavo Niemeyer

[ 2AAC 7928 0FBF 0299 5EB5  60E2 2253 B29A 6664 3A0C ]




More information about the Python-list mailing list