problem with numeric array on window.
Alexandre Gillet
gillet at scripps.edu
Fri Feb 9 20:37:54 EST 2001
Hi all,
We are having problems when creating numeric arrays in C extensions
under
windows. We narrowed the problem down to a very simple example (shown
below) where we simply allocate some memory, create a Numeric array
using
PyArrayFromDimsAndData. If we call that function as soon as we delete
the
returned array the python interpreter crashes as it tries to free
self->data. If we do not the the OWN_DATA flag in the array it works
fine
but we have a memory leak.
We tried this using both release and Debug versions of Python1.5.2, and
Numeric 17.1.1.
I have been using this mechanism under Unix for a long time and have not
had this problem before !
Using the same extension and test under unix works fine.
Has something changed ? Any help is welcome ...
Thank you
I join the two files we are using.
bug.c : a C module that create an array numeric with the flags set to
own_data, so when the array is garbage collected, the memory is free.
#################
bug.c#####################################################
#ifdef WIN32
#include <stdlib.h>
#include <malloc.h>
#endif
#include "Python.h"
#include "arrayobject.h"
static PyObject* createArray(PyObject* self, PyObject* args)
{
int *dims;
float *data;
PyArrayObject *out;
dims = (int *)malloc(2 * sizeof(int));
dims[0] = 500;
dims[1] = 2;
data = (float *)malloc(100000 * sizeof(float));
out = (PyArrayObject *)PyArray_FromDimsAndData(2, dims,
PyArray_FLOAT,
(char *)data);
if (!out) {
PyErr_SetString(PyExc_RuntimeError,
"Failed to allocate memory for normals");
return NULL;
}
out->flags |= OWN_DATA; /*so we'll free this memory when this
array will be garbage collected */
return (PyObject *)out;
}
static PyMethodDef bug_methods[] = {
{"createArray", createArray, 1},
{NULL, NULL} /* Sentinel */
};
static char bug_documentation[] = "No Doc";
#ifdef WIN32
extern __declspec(dllexport)
#endif
void initbug()
{
PyObject *m, *d;
m = Py_InitModule4("bug",
bug_methods,
bug_documentation,
(PyObject *)NULL,
PYTHON_API_VERSION);
d = PyModule_GetDict(m);
import_array();
if (PyErr_Occurred())
Py_FatalError("can't initialize module bug");
}
#####################################################################
###############testbug.py############################################
import bug
ar = bug.createArray()
del ar # fatal if ar owns the data member
#####################################################################
--
**********************************
Alexandre Gillet
The Scripps Research Institute, tel: (858) 784-9557
Dept. Molecular Biology, MB-5, fax: (858) 784-2860
10550 North Torrey Pines Road, email: gillet at scripps.edu
La Jolla, CA 92037-1000, USA.
More information about the Python-list
mailing list