PyArg_Parse() & review for memory leaks

Anton Graph "aglyportat\" at n-o.spa__mmm.yahoo dott com>
Sat Jun 15 22:44:44 EDT 2002


Daniel Yoo wrote:
> 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.
> 
Yep, you're right. False alarm. Thanks for the URL.

> 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