##// END OF EJS Templates
Fix memory leak in c extension
Juan C. Valdez -
r880:f40770083742
parent child
Show More
@@ -1,55 +1,58
1 #include <Python.h>
1 #include <Python.h>
2 #include <numpy/arrayobject.h>
2 #include <numpy/arrayobject.h>
3 #include <math.h>
3 #include <math.h>
4
4
5 static PyObject *hildebrand_sekhon(PyObject *self, PyObject *args);
5 static PyObject *hildebrand_sekhon(PyObject *self, PyObject *args);
6
6
7 static PyMethodDef extensionsMethods[] = {
7 static PyMethodDef extensionsMethods[] = {
8 { "hildebrand_sekhon", (PyCFunction)hildebrand_sekhon, METH_VARARGS, "get noise with" },
8 { "hildebrand_sekhon", (PyCFunction)hildebrand_sekhon, METH_VARARGS, "get noise with" },
9 { NULL, NULL, 0, NULL }
9 { NULL, NULL, 0, NULL }
10 };
10 };
11
11
12 PyMODINIT_FUNC initcSchain() {
12 PyMODINIT_FUNC initcSchain() {
13 Py_InitModule("cSchain", extensionsMethods);
13 Py_InitModule("cSchain", extensionsMethods);
14 import_array();
14 import_array();
15 }
15 }
16
16
17 static PyObject *hildebrand_sekhon(PyObject *self, PyObject *args) {
17 static PyObject *hildebrand_sekhon(PyObject *self, PyObject *args) {
18 /* Do your stuff here. */
18 /* Do your stuff here. */
19 double navg;
19 double navg;
20 PyObject *data_obj, *data_array;
20 PyObject *data_obj, *data_array;
21
21
22 if (!PyArg_ParseTuple(args, "Od", &data_obj, &navg)) return NULL;
22 if (!PyArg_ParseTuple(args, "Od", &data_obj, &navg)) return NULL;
23 data_array = PyArray_FROM_OTF(data_obj, NPY_FLOAT64, NPY_IN_ARRAY);
23 data_array = PyArray_FROM_OTF(data_obj, NPY_FLOAT64, NPY_IN_ARRAY);
24 if (data_array == NULL) {
24 if (data_array == NULL) {
25 Py_XDECREF(data_array);
25 Py_XDECREF(data_array);
26 Py_XDECREF(data_obj);
26 Py_XDECREF(data_obj);
27 return NULL;
27 return NULL;
28 }
28 }
29 double *sortdata = (double*)PyArray_DATA(data_array);
29 double *sortdata = (double*)PyArray_DATA(data_array);
30 int lenOfData = (int)PyArray_SIZE(data_array) ;
30 int lenOfData = (int)PyArray_SIZE(data_array) ;
31 double nums_min = lenOfData*0.2;
31 double nums_min = lenOfData*0.2;
32 if (nums_min <= 5) nums_min = 5;
32 if (nums_min <= 5) nums_min = 5;
33 double sump = 0;
33 double sump = 0;
34 double sumq = 0;
34 double sumq = 0;
35 int j = 0;
35 int j = 0;
36 int cont = 1;
36 int cont = 1;
37 double rtest = 0;
37 double rtest = 0;
38 while ((cont == 1) && (j < lenOfData)) {
38 while ((cont == 1) && (j < lenOfData)) {
39 sump = sump + sortdata[j];
39 sump = sump + sortdata[j];
40 sumq = sumq + pow(sortdata[j], 2);
40 sumq = sumq + pow(sortdata[j], 2);
41 if (j > nums_min) {
41 if (j > nums_min) {
42 rtest = (double)j/(j-1) + 1/navg;
42 rtest = (double)j/(j-1) + 1/navg;
43 if ((sumq*j) > (rtest*pow(sump, 2))) {
43 if ((sumq*j) > (rtest*pow(sump, 2))) {
44 j = j - 1;
44 j = j - 1;
45 sump = sump - sortdata[j];
45 sump = sump - sortdata[j];
46 sumq = sumq - pow(sortdata[j],2);
46 sumq = sumq - pow(sortdata[j],2);
47 cont = 0;
47 cont = 0;
48 }
48 }
49 }
49 }
50 j = j + 1;
50 j = j + 1;
51 }
51 }
52
52
53 double lnoise = sump / j;
53 double lnoise = sump / j;
54
55 Py_DECREF(data_array);
56
54 return Py_BuildValue("d", lnoise);
57 return Py_BuildValue("d", lnoise);
55 }
58 }
General Comments 0
You need to be logged in to leave comments. Login now