C extension question about avoiding memory leaks with the object returned...
Martin v. Löwis
martin at v.loewis.de
Wed Oct 8 14:57:41 EDT 2003
seberino at spawar.navy.mil (Christian Seberino) writes:
> return Py_BuildValue("s", my_string);
In this case, Py_BuildValue is very expensive. Use PyString_FromString
instead.
> If my_string allocated dynamically:
> ==========================
> Will Python successfully handle garbage collection of my_string?
No. The resulting Python string object will be a copy; the original
string is considered read-only, and no attempt to release it is made.
> Will there be any problems because it was built within my C function?
If you don't release it, there will be a memory leak. Apart from that:
no.
> If my_string allocated statically:
> ==========================
> Don't all statically declared objects get killed when you exit a C function?
You might need to explain what a statically declared string is, in
C. If you are talking about string literals: no, they don't get killed
when a C function exits. They exist from the start of the program
until it ends.
> This is not a problem because Python will get a COPY right?
Right.
Regards,
Martin
More information about the Python-list
mailing list