[Numpy-discussion] Ctypes reference counting

Ryan May rmay at ou.edu
Fri Jan 18 13:49:09 EST 2008


Hi,

Can someone explain the reference counting wrt using ctypes and numpy.
Specifically, I have code like:

from ctypes import *
import numpy as N

class Data(Structure):
	_fields_=[('var',POINTER(c_float))]

d = Data()
d.var = N.arange(100., dtype=N.float32).ctypes.data_as(POINTER(c_float))
print d.var[5] #Should be 5.0

Instead it prints 0.0.  (On my bigger code, it just segfaults.)
Obviously, the array going in get's collected b/c its reference count
goes to 0.  Why does getting the data as a pointer not add a reference?
 Is there a "correct" way to do what I'm attempting?

Overall, I'm trying to create arrays using Numpy that are saved into
structures and passed into ctypes-wrapped C code that does the heavy
lifting.  However, this heavy lifting is done in a different (ie.
separate and higher) scope than the code that creates the arrays.

Thanks,

Ryan

-- 
Ryan May
Graduate Research Assistant
School of Meteorology
University of Oklahoma



More information about the NumPy-Discussion mailing list