From: Angus M. <am...@gm...> - 2011-07-13 14:33:43
|
Hi all, I have some C++ code that implements a data messaging system between processes, and from which I am generating a python wrapper using swig. 90% of the time this is all working fine. In two instances though I have seen a problem where a variable passed into python contains garbage values, rather than the expected vector of zeros. The weirdness is that if I, in python, convert the data into a numpy array AND print that array, then I get the values I expect. I've tried this with only one of either using np.asarray or the print, but the "fix" only works with both. Obviously I'd like to not have to depend on a print statement to get my values out. Here's a little code python code snippet to show what I mean: ################ import numpy as np # this is just data format declarations import RTMA_config as rc # these are the swig-wrapped messaging interface from PyRTMA import RTMA_Module, CMessage, copy_from_msg def setup_RTMA(server): mod = RTMA_Module(rc.MID, 0) mod.ConnectToMMM(server) mod.Subscribe(rc.MT_DATA) mod.SendModuleReady() print "Connected to RTMA at", server return mod def run(mod): while True: msg = CMessage() rcv = mod.ReadMessage(msg, 0.1) if rcv == 1: process_message(msg) def process_message(msg): if msg.GetHeader().msg_type == rc.MT_DATA: data = rc.MDF_DATA() copy_from_msg(data, msg) #print "A", np.asarray(data.dof_vals[:]) # this is the critical line -> put this in and the next line works print "B", data.dof_vals[:] # shows garbage values without previous line, or 0s with it server = 'localhost:7111' mod = setup_RTMA(server) run(mod) ################ copy_to_msg is defined in PyRTMA.i as: %pythoncode %{ from ctypes import memmove, addressof, sizeof def copy_from_msg(data, cmsg): memmove(addressof(data), cmsg.data, sizeof(data)) %} Does anyone have any idea what might be causing my particular issue, or how I should go about debugging it? Thanks, Angus. -- AJC McMorland Post-doctoral research fellow Neurobiology, University of Pittsburgh |