Core dump revisited

Duncan Booth duncan.booth at invalid.invalid
Tue Dec 19 11:11:36 EST 2006


"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).



More information about the Python-list mailing list