Python C API String Memory Consumption
MRAB
google at mrabarnett.plus.com
Tue Apr 7 07:19:29 EDT 2009
k3xji wrote:
> Interestaing I changed malloc()/free() usage with PyMem_xx APIs and
> the problem resolved. However, I really cannot understand why the
> first version does not work. Here is the latest code that has no
> problems at all:
>
> static PyObject *
> penc(PyObject *self, PyObject *args)
> {
> PyObject * result = NULL;
> unsigned char *s= NULL;
> unsigned char *buf = NULL;
> unsigned int v,len,i = 0;
>
> if (!PyArg_ParseTuple(args, "s#", &s, &len))
> return NULL;
>
> buf = (unsigned char *) PyMem_Malloc(len);
> if (buf == NULL) {
> PyErr_NoMemory();
> return NULL;
> }
>
> /* string manipulation. */
>
> result = PyString_FromStringAndSize((char *)buf, len);
> PyMem_Free(buf);
> return result;
> }
>
In general I'd say don't mix your memory allocators. I don't know
whether CPython implements PyMem_Malloc using malloc, but it's better to
stick with CPython's memory allocators when writing for CPython.
More information about the Python-list
mailing list