C extension question about avoiding memory leaks with the object returned...

Christian Seberino seberino at spawar.navy.mil
Wed Oct 8 14:44:28 EDT 2003


I wrote some C functions and successfully imported them into Python.

All is apparently well but there is one question about the object returned
by the C function that is bugging me...

Take for example this last line of a C extension:

   return Py_BuildValue("s", my_string);


If my_string allocated dynamically:
==========================
Will Python successfully handle garbage collection of my_string?
Will there be any problems because it was built within my C function?

(I would think Python will successfully receive a COPY of my_string
but that the original my_string object will be a memory leak!!!)

If my_string allocated statically:
==========================
Don't all statically declared objects get killed when you exit a C function?
This is not a problem because Python will get a COPY right?

(This seems "safer" until my dynamic question above gets answered but
with static guys you lose flexibility with the SIZE of the string of course.)

Any help would be greatly appreciated.

Chris




More information about the Python-list mailing list