"Sheldon" <[EMAIL PROTECTED]> wrote:

> 
> Duncan Booth skrev:
> 
>> "Sheldon" <[EMAIL PROTECTED]> wrote:
>>
>> > I am new to this and copied this code from a colleague. So, it
>> > corrupts the pointer. How do I do this properly?
>> >
>> Here is at least part of your problem:
>>
>>     msgop = PyList_GetItem(work.msgobj, i);
>>     work.msg_scenes[i] = PyString_AsString(msgop);
>>     ppsop = PyList_GetItem(work.ppsobj, i);
>>     work.pps_scenes[i] = PyString_AsString(ppsop);
>> ...
>>     free(work.pps_scenes[i]);
>>     free(work.msg_scenes[i]);
>>
>> You initialised msg_scenes and pps_scenes with a malloc'ed block but
>> you then just overwrote the pointer with the result of
>> PyString_AsString. You don't own the memory for the string returned
>> from PyString_AsString, so freeing it will cause a corruption. You
>> should copy the string data into the malloc'ed block (with
>> appropriate length checks). 
> 
> Do you mean with: PyString_FromStringAndSize() and
> PyString_Size(PyObject *string)
> 
If you wish, or even just strlen() if you aren't concerned about embedded 
nulls.

-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to