PyArg_Parse() & review for memory leaks

Anton Graph "aglyportat\" at n-o.spa__mmm.yahoo dott com>
Fri Jun 14 13:37:22 EDT 2002


int
dumptag(PyObject *layout, char *prndriver, char *extra, char **res, int &len
	) {
	PyObject *args = Py_BuildValue("(Oss)", layout, prndriver, extra);
	if(!args)
		l_fatal("args build failed");

	PyObject *result = PyEval_CallObject(dumptag_callback, args);
	Py_DECREF(args);
	if( !result ) {
		if (PyErr_Occurred()) {
			PyErr_Print();
			l_fatal("error occured while executing dumptag()");
		}
		l_fatal("return value is expected from dumptag()");
		return 0;
	}
	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?




More information about the Python-list mailing list