PyArg_Parse() & review for memory leaks

Daniel Yoo dyoo at hkn.eecs.berkeley.edu
Sat Jun 15 08:02:51 CEST 2002


Anton Graph <""aglyportat\"@n-o.spa__mmm.yahoo dott com> wrote:

[some code cut]

:        char *pstr;
:        if(!PyArg_Parse(result, "s", &pstr)) { // the plug-in just
:                // builds a string containing tag
:                return 0;
:        }
:        int st=strlen(pstr);
:        if(st>=len) {
:                delete [] *res;
:                len=st+1;
:                *res = new char[len];
:        }
:        astrncpy(*res, pstr, len);
:        Py_DECREF(result);
:        return st;
: }

: do I leak pstr here? Sample code I've googled up seems similar, but I 
: don't understand how does PyArg_Parse allocate enough space without 
: going to the heap?

I don't believe so: PyArg_Parse()ing a string shouldn't be doing any
allocation because pstr is simply directed to the string pointer that
Python is using internally to store that String object --- no string
copying is involved.

Section 5.5 of the Python/C API document explains more about
PyArg_Parse():

    http://python.org/doc/api/arg-parsing.html

Good luck to you!



More information about the Python-list mailing list