From: Matt K. <mat...@ho...> - 2006-08-28 14:02:48
|
>>Matt Knox wrote:>>> Hi there. I'm in the unfortunate situation of trying = to track down a >> memory error in someone elses code, and to make matters = worse I don't >> really know jack squat about C programming. The problem se= ems to arise >> when several numpy arrays are created from C arrays in the = C api and >> returned to python, and then trying to print out or cast to a = string >> the resulting array. I think the problem may be happening due to = the >> following chunk of code:>> {>> PyObject* temp =3D PyArray_Simpl= eNewFromData(1, &numobjs, typeNum, >> dbValues);>> PyObject* temp2 =3D= PyArray_FromArray((PyArrayObject*)temp, >> ((PyArrayObject*)temp)->descr, = DEFAULT_FLAGS | ENSURECOPY);>> Py_DECREF(temp);>> PyDict_SetItemS= tring(returnVal, "data", temp2);>> Py_DECREF(temp2);>> }>> >> Lets as= sume that all my other inputs up this point are fine and that >> numobjs, t= ypeNum, and dbValues are fine. Is their anything obviously >> wrong with th= e above chunk of code? or does it appear ok? Ultimately >> the dictionary "= returnVal" is returned by the function this code came >> from, and everythi= ng else is discarded. Any help is very greatly >> appreciated. Thanks in ad= vance, > You didn't indicate what kind of trouble you are having.>> First of all, = this is kind of odd style. Why is a new array created > from a data-pointe= r and then copied using PyArray_FromArray (the > ENSURECOPY flag will give = you a copy)? Using>> temp2 =3D PyArray_Copy(temp)>> seems simpler. This = will also avoid the reference-count problem that > is currently happening i= n the PyArray_FromArray call on the descr > structure. Any array-creatio= n function that takes a descr structure > "steals" a reference to it, so yo= u need to increment the reference count > if you are passing an unowned ref= erence to a ->descr structure.>> -Travis =20 Sorry. Yeah, the problem was the interpreter crashing on exit, which aftery= our response definitely seems like it was a reference count issue. Ichanged= the PyArray_FromArray call to be PyArray_Copy and it seems to workfine. Th= ank you very much! =20 Love the numpy stuff (when I can stay in the python world and not mess with= the C stuff :) ). Keep up the great work! =20 - Matt _________________________________________________________________ Be one of the first to try Windows Live Mail. http://ideas.live.com/programpage.aspx?versionId=3D5d21c51a-b161-4314-9b0e-= 4911fb2b2e6d= |