On Tue, Feb 24, 2009 at 11:36 AM, Stefan Behnel <[email protected]> wrote:
> Hi,
>
> Robert Bradshaw wrote:
>> That is what the cleanup functions do--they try and release the
>> memory in use by the module. The problem is that it isn't easy to
>> tell when a module is still in use (as far as I can tell). It sounds
>> like things have improved with Py3's unloading of modules.
>
> This would be a Py3-only feature. The cleanup code for Py2 would still be
> optional.
>

However, some people could argue that cleanup code will slow-down
Python process termination.

>
>> A concrete example might help explain things. Say I have a Cython class
>>
>> class A:
>>      def __del__(self):
>>          print 1
>>
>> Now when this is compiled, a python int 1 will be created and stored
>> in the module dictionary. If the module defining A is cleaned up
>> before every instance of A is deallocated, then the cached Python 1
>> will be collected and then at some later point A.__del__ could be
>> called, which may result in a segfault.
>
> Ok, but all this means is that the class needs to hold a reference to the
> module instance that created it, right?
>

That would work, but such approach will introduce reference cycles
that I would like to avoid. And of course, you need a extra slot in
object struct of cdef classes...

In any case, the only way to write safe code is to avoid the usage of
module-level globals in __del__() and __dealloc__() methods... For
example, in petsc4py and slepc4py (the second depend on the first, and
the second cimport types from the first) I'm able to generate
full-cleanup code for both. But this work as long as cimported types
are not nullified (I've just pushed a fix for this, I made a mistake
in a previous commit), and just because core CPython does not
dlclose() extension modules and extension types are not heap-types
(i.e. the typeobject struct is always available on static storage).



> _______________________________________________
> Cython-dev mailing list
> [email protected]
> http://codespeak.net/mailman/listinfo/cython-dev
>



-- 
Lisandro Dalcín
---------------
Centro Internacional de Métodos Computacionales en Ingeniería (CIMEC)
Instituto de Desarrollo Tecnológico para la Industria Química (INTEC)
Consejo Nacional de Investigaciones Científicas y Técnicas (CONICET)
PTLC - Güemes 3450, (3000) Santa Fe, Argentina
Tel/Fax: +54-(0)342-451.1594
_______________________________________________
Cython-dev mailing list
[email protected]
http://codespeak.net/mailman/listinfo/cython-dev

Reply via email to