Author: varioustoxins Date: Fri Apr 7 08:33:34 2006 New Revision: 2455 URL: http://svn.gna.org/viewcvs/relax?rev=2455&view=rev Log: removed memory leak in fitting functions c module edward at nmr-relax.com https://gna.org/bugs/index.php?func=detailitem&item_id=5602 https://gna.org/bugs/index.php?func=detailitem&item_id=5473 Modified: 1.2/maths_fns/c_chi2.c 1.2/maths_fns/exponential.c 1.2/maths_fns/relax_fit.c 1.2/maths_fns/relax_fit.h Modified: 1.2/maths_fns/c_chi2.c URL: http://svn.gna.org/viewcvs/relax/1.2/maths_fns/c_chi2.c?rev=2455&r1=2454&r2=2455&view=diff ============================================================================== --- 1.2/maths_fns/c_chi2.c (original) +++ 1.2/maths_fns/c_chi2.c Fri Apr 7 08:33:34 2006 @@ -24,7 +24,7 @@ #define square(x) (x)*(x) -double chi2(void) { +double chi2(double *values, double *sd, double *back_calc, int num_times) { /* Function to calculate the chi-squared value. The chi-sqared equation @@ -43,10 +43,6 @@ The chi-squared value is returned. */ - /* Declarations */ - extern int num_times; - extern double *values, *sd; - extern double back_calc[]; int i; double chi2 = 0.0; Modified: 1.2/maths_fns/exponential.c URL: http://svn.gna.org/viewcvs/relax/1.2/maths_fns/exponential.c?rev=2455&r1=2454&r2=2455&view=diff ============================================================================== --- 1.2/maths_fns/exponential.c (original) +++ 1.2/maths_fns/exponential.c Fri Apr 7 08:33:34 2006 @@ -30,14 +30,11 @@ -void exponential(void) { +void exponential(double *params, double *relax_times, double *back_calc, int num_times) { /* Function to back calculate the peak intensities. */ /* Declarations */ - extern int num_times; - extern double *params, *relax_times; - extern double back_calc[]; double Rx, I0; int i; Modified: 1.2/maths_fns/relax_fit.c URL: http://svn.gna.org/viewcvs/relax/1.2/maths_fns/relax_fit.c?rev=2455&r1=2454&r2=2455&view=diff ============================================================================== --- 1.2/maths_fns/relax_fit.c (original) +++ 1.2/maths_fns/relax_fit.c Fri Apr 7 08:33:34 2006 @@ -27,26 +27,35 @@ /* The header for all functions which will be called */ #include "relax_fit.h" - +/* functions for chi2 and exponential */ +#include "c_chi2.h" +#include "exponential.h" static PyObject * setup(PyObject *self, PyObject *args, PyObject *keywords) { /* Python declarations */ PyObject *values_arg, *sd_arg, *relax_times_arg, *scaling_matrix_arg; - PyArrayObject *numpy_values, *numpy_sd, *numpy_relax_times, *numpy_scaling_matrix; + extern PyArrayObject *numpy_values, *numpy_sd, *numpy_relax_times, *numpy_scaling_matrix; /* Normal declarations */ extern double *values, *sd, *relax_times, *scaling_matrix; extern double relax_time_array; extern int num_params, num_times; - + /* The keyword list */ static char *keyword_list[] = {"num_params", "num_times", "values", "sd", "relax_times", "scaling_matrix", NULL}; /* Parse the function arguments */ if (!PyArg_ParseTupleAndKeywords(args, keywords, "iiOOOO", keyword_list, &num_params, &num_times, &values_arg, &sd_arg, &relax_times_arg, &scaling_matrix_arg)) return NULL; - + + + + Py_XDECREF(numpy_values); + Py_XDECREF(numpy_sd); + Py_XDECREF(numpy_relax_times); + Py_XDECREF(numpy_scaling_matrix); + /* Make the Numeric arrays contiguous */ numpy_values = (PyArrayObject *) PyArray_ContiguousFromObject(values_arg, PyArray_DOUBLE, 1, 1); numpy_sd = (PyArrayObject *) PyArray_ContiguousFromObject(sd_arg, PyArray_DOUBLE, 1, 1); @@ -59,10 +68,8 @@ relax_times = (double *) numpy_relax_times->data; scaling_matrix = (double *) numpy_scaling_matrix->data; - /* Increment the reference count for the python object None */ + /* Return nothing */ Py_INCREF(Py_None); - - /* Return nothing */ return Py_None; } @@ -77,8 +84,8 @@ /* Declarations */ PyObject *arg1; - extern PyArrayObject *numpy_params; - double chi2(void); + PyArrayObject *numpy_params; + double* params; /* Parse the function arguments, the only argument should be the parameter array */ @@ -92,13 +99,11 @@ params = (double *) numpy_params->data; /* Back calculated the peak intensities */ - exponential(); - - /* Deallocate the memory used by the parameters */ + exponential(params, relax_times, back_calc, num_times); + Py_DECREF(numpy_params); - /* Calculate and return the chi-squared value */ - return Py_BuildValue("f", chi2()); + return Py_BuildValue("f", chi2(values,sd,back_calc,num_times)); } @@ -108,13 +113,14 @@ /* Declarations */ PyObject *arg1; - extern PyArrayObject *numpy_params; + PyArrayObject *numpy_params; /* Temp Declarations */ PyArrayObject *aaa_numpy; double aaa[MAXPARAMS] = {1.0, 2.0}; double *aaa_pointer; int i; + double* params; /* Parse the function arguments, the only argument should be the parameter array */ if (!PyArg_ParseTuple(args, "O", &arg1)) @@ -127,20 +133,19 @@ params = (double *) numpy_params->data; /* Back calculated the peak intensities */ - exponential(); + exponential(params, relax_times, back_calc, num_times); /* Test code (convert aaa to a Numeric array */ - aaa_numpy = (PyArrayObject *) PyArray_FromDims(1, &num_params, PyArray_DOUBLE); - aaa_pointer = (double *) aaa_numpy->data; + /* aaa_numpy = (PyArrayObject *) PyArray_FromDimsAndData(1, num_params, PyArray_DOUBLE, aaa_pointer); */ + /*aaa_numpy = (PyArrayObject *) PyArray_FromDims(1, &num_params, PyArray_DOUBLE); + aaa_pointer = (double *) aaa_numpy->data;*/ /* Fill the Numeric array */ - for (i = 0; i < 2; i++) - aaa_pointer[i] = aaa[i]; - - /* Deallocate the memory used by the parameters */ + /*for (i = 0; i < 2; i++) + aaa_pointer[i] = aaa[i];*/ + Py_DECREF(numpy_params); - return NULL; return PyArray_Return(aaa_numpy); } Modified: 1.2/maths_fns/relax_fit.h URL: http://svn.gna.org/viewcvs/relax/1.2/maths_fns/relax_fit.h?rev=2455&r1=2454&r2=2455&view=diff ============================================================================== --- 1.2/maths_fns/relax_fit.h (original) +++ 1.2/maths_fns/relax_fit.h Fri Apr 7 08:33:34 2006 @@ -41,11 +41,12 @@ double *sd; /* Variables sent to 'func', 'dfunc', and 'd2func' during optimisation */ -PyArrayObject *numpy_params; +/*PyArrayObject *numpy_params;*/ /* Pointers to contiguous PyArrayObjects */ double *values, *sd, *relax_times, *scaling_matrix; -double *params; +/*double *params;*/ + /* Variables used for storage during the function calls of optimisation */ double back_calc[MAXTIMES];