[Gdal-dev] References and scripting language bindings

Ari Jolma ari.jolma at tkk.fi
Thu Sep 15 02:26:20 EDT 2005


Hi, this problem was discussed on IRC yesterday but I'm slow to 
understand, so I try to describe the problem again and think about a 
solution.

The problem is that if you do something like

layer = datasource.Open('data').GetLayerByIndex(0)
print layer.Name

this will cause a problem if the scripting language has deleted the 
datasource (and the layer with it). This is  because it was just a 
temporary variable. I first thought that layer should be undefined on 
the 2nd line. But this is analogous to the case if you create a 
reference to a variable and there scripting languages typically do not 
delete the variable until the referencing variable is not used anymore 
(it goes out of scope for example).

The datasource class (and many other classes) in OGR has similar 
reference count system. Thus I think we should use them in the scripting 
language bindings. I.e. do not destroy a datasource object if it has 
references to it. In GetLayerByIndex increment the reference count. In a 
layer object make a note which datasource object it needs and decrement 
the count of that when the layer object it destroyed.

Is this difficult to implement in swig? The problem seems to be where to 
put the link to the parent (e.g. the datasource handle for the layer 
object)? It has to be done while still in the C wrapper.

I don't know how all this works in Java which has a different kind of 
garbage collection system.

Ari




More information about the Gdal-dev mailing list