Core dump revisited
Sheldon
shejo284 at gmail.com
Tue Dec 19 11:56:38 EST 2006
Duncan Booth skrev:
> "Sheldon" <shejo284 at gmail.com> 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)
/S
More information about the Python-list
mailing list