"Memory could not be Read/Written" crashes with C extension DLL... HELP PLEASE!

Chris Liechti cliechti at gmx.net
Sat Apr 6 15:05:46 EST 2002


kevin at cazabon.com (Kevin Cazabon) wrote in 
news:5a4226f0.0204061049.c2b1026 at posting.google.com:

> Through some help, I've found that this section is causing the
> problem... If I simply use "return Py_BuildValue("lllO", pp1,
> inBufferSize, pp3, inBuffer);", it works fine with no crashes.
> 
> What is the difference though, shouldn't the code below work too?  Is
> there maybe something wrong in the PyTuple code?  (As mentioned
> before, it only crashed when called multiple times (40+ usually), but
> worked fine otherwise.
> 
> Kevin.
> 
>>   // build pp1, inBufferSize, pp3 into PyObjects for returning
>>   pLong1 = PyLong_FromLong(pp1);
>>   pLong2 = PyLong_FromLong(inBufferSize);
>>   pLong3 = PyLong_FromLong(pp3);
>> 
>>   // build pLong1, pLong2, pLong3, pString into a tuple to return
>> 
>>   pReturn = PyTuple_New(4);
>>   PyTuple_SetItem(pReturn, 0, pLong1); 
>>   PyTuple_SetItem(pReturn, 1, pLong2);
>>   PyTuple_SetItem(pReturn, 2, pLong3);
>>   PyTuple_SetItem(pReturn, 3, inBuffer);
>> 
>>   return pReturn;
>> }

PyTuple_SetItem is stealing a reference, but the reference to inBuffer
was only borrowed. There is your wrong count :-)

Python22/Doc/api/tupleObjects.html: """
int PyTuple_SetItem(PyObject *p,  int pos, PyObject *o) 
Inserts a reference to object o at position pos of the tuple pointed to by 
p. It returns 0 on success. Note: This function ``steals'' a reference to 
o.
"""

Python22/Doc/ext/parseTuple.html: """
Note that any Python object references which are provided to the caller are 
borrowed references; do not decrement their reference count! 
"""

chris

-- 
Chris <cliechti at gmx.net>




More information about the Python-list mailing list