Finalizing C extension modules

Matthias Baas baas at ira.uka.de
Thu Apr 18 08:00:37 EDT 2002


Hi,

I have a question concerning extension modules written in C/C++.
Suppose I have a C/C++ module that allocates some resources in its
init function, where would be the proper place to release them again
when the Python interpreter is shutting down? Is there an equivalent
to Py_Finalize() for extension modules? 

There's this part in the docs of Py_Finalize() which makes me a bit
uneasy: 

"Dynamically loaded extension modules loaded by Python are not
unloaded. [...] Some memory allocated by extension modules may not be
freed."

Why is it that extension modules aren't unloaded?

So to be a bit more specific about my problem. I have a C extension
module containing some functions that are supposed to return a
specific pure Python object from another pure Python module. So I
import this Python module in the init function, get the class object
and keep it so that my own functions can create instances of this
Python class and return them to the caller. However, I will never call
Py_DECREF() for that class object I was retrieving from the pure
Python module. Is this ok? Or will this result in a memory leak? Where
am I supposed to call Py_DECREF()?

- Matthias -




More information about the Python-list mailing list