ctypes and garbage collection

Ulrich Eckhardt eckhardt at satorlaser.com
Mon Sep 6 09:11:14 EDT 2010


Joakim Hove wrote:
> I have used ctypes to wrap a C-library
> [...] 
> Observe that the c_container_get_node() function does _not_ allocate
> memory, it just returns a opaque handle to a node structure, still
> fully owned by the container structure.
[...]
> 
> class Container:
>      def __init__(self , filename):
>           self.c_ptr = c_container_alloc( filename )
> 
>     def __del__( self ):
>          c_container_free( self.c_ptr )
> 
>     def get_node( self , node_id):
>          Node( c_container_get_node( self , node_id ))
> 
> 
> class Node:
>      def __init__( self , c_ptr ):
>           self.c_ptr = c_ptr
> 
>      def __del__( self ):
>           pass
> 
> 
> Now, a use scenario might be like this:
> 
> 1. Instantiate a Container() instance.
> 2. Instantiate a Node() instance with the Container.get_node()
> function.
> 3. Forget about the Container instance and work happily with the Node
> instance.
> 4. Out of the blue comes the gc - and then? Will the Node instance be
> enough to protect the Container instance from beeing garbage
> collected?

No. You should be able to even see that by logging calls to alloc/free of
your library.

> I thought maybe the get_node() function should have something like a
> incref() call, and the Node.__del__() function a corresponding
> decref()? Or ??

I'd add an "__owner" field to the node, initialised with the owning
container instance.

Uli

-- 
Sator Laser GmbH
Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932




More information about the Python-list mailing list