[GRASS-dev] RE: [GRASS-user] Question with Python-SWIG example [SEC=UNCLASSIFIED]

Glynn Clements glynn at gclements.plus.com
Thu May 20 13:50:36 EDT 2010


Martin Landa wrote:

> >> > This particular issue should be fixed by r42311. I don't know if this
> >> > is related to the problem, but it's something to eliminate.
> >>
> >> right, this issue is eliminated, anyway the reported problem still persists.
> >
> > The only other thing which I can suggest is to add print statements to
> > the functions in $GISBASE/etc/python/grass/lib/ctypesheader.py,
> > specifically String.__init__ and String.from_param.
> 
> hm, seems to be OK
> 
> String.from_param(): $Revision: 41297 $
> String.__init__(): $Revision: 41297 $
> String.from_param(): $Revision: 41297 $
> String.__init__(): $Revision: 41297 $
> String.from_param(): /home/martin/smetiste/example7.py
> String.__init__(): /home/martin/smetiste/example7.py
> String.from_param(): elevation
> String.__init__(): elevation
> String.from_param():
> String.__init__():
> WARNING: Illegal filename <L�����>. Character <�> not allowed.
> String.__init__(): String.from_param(): �
> String.from_param(): �
>
This is what I get:

String.from_param('$Revision: 41297 $')
String.__init__('$Revision: 41297 $')
String.from_param('$Revision: 41297 $')
String.__init__('$Revision: 41297 $')
String.from_param('./foo.py')
String.__init__('./foo.py')
String.from_param('elevation.dem')
String.__init__('elevation.dem')
String.from_param('')
String.__init__('')
ReturnString(168559720)
String.from_param(168559720)
String.__init__(<ctypes.LP_c_char object at 0xb7d1e6ec>)
PERMANENT

Does this work:
	...
	c_input = c_char_p(input)
	c_mapset = c_char_p("")
	mapset = grass.G_find_raster2(c_input, c_mapset)
	...
?

Or this:
	...
	grass.G_find_raster2.argtypes = [c_char_p, c_char_p]
	grass.G_find_raster2.restype = c_char_p
	mapset = grass.G_find_raster2(c_input, c_mapset)
	...
?

The most common issue with ctypes, and the most likely reason why the
underlying function would end up getting garbage data, is that the
ctypes pointer types don't hold a reference to the underlying object,
which can result in it being garbage collected while it is still being
used.

This is mostly an issue when calling functions which store any pointer
arguments, as the pointer may cease to be valid once the function
returns.

This shouldn't be an issue for G_find_raster2() itself, so I suspect
that ctypesgen's String wrapper is passing a pointer to a temporary
value which is being reclaimed before the function is called.

-- 
Glynn Clements <glynn at gclements.plus.com>


More information about the grass-dev mailing list