Hi. First off, thanks for all of the work you do with GSL. It's certainly made my life much easier in a lot of areas. But today I came across an error that for some reason I haven't seen before, even though I've used gsl_multimin quite a bit in the past. It seems so simple, and so easy to replicate, that I'm sending it to the Bug list instead of Help. I apologize in advance if that was the wrong decision.
Below this message I've pasted some code that should look rather familiar, since it is the example code from the GSL documentation (the functions from Section 36.4 and the example code in Section 36.9, both now in the same file). However, I have added or changed four lines of code, commented near the bottom of the file. What I've done is allocated a new gsl_vector Y, assigned the current state of the minimizer to Y, pulled the current values of the minimizer from Y instead of from s->x, and then free Y. So the only real change here is that I am using the gsl_multimin_fdfminimizer_x function to get the current state of s into a gsl vector, instead of pulling it directly from s->x. When I run this code, it prints the current state of the minimizer as expected (as you can see in this output). The problem comes when trying to free Y. The error is generated by the gsl_vector_free(Y) call. I have no idea what I could be doing wrong here. 1 4.99629 6.99072 687.84780 2 4.98886 6.97215 683.55456 <snip> 11 2.19088 1.76182 45.31640 12 0.86892 2.02622 30.18555 Minimum found at: 13 1.00000 2.00000 30.00000 mmtest(17199) malloc: *** error for object 0xe: pointer being freed was not allocated *** set a breakpoint in malloc_error_break to debug Abort trap My call to the compiler is gcc -lgsl -lgslcblas -o mmtest mmtest.c and I get the same error with both gcc 4.2.1 and 4.6, as well as icc 12.0.0. This also happens using the C++ compilers (I was writing C++ code when it happened). I am running the code on a Mac Pro running OSX 10.6.7. This is GSL version 1.14. Hopefully this is a helpful test example (just copy, paste and compile), and there is an easy fix. Thanks, again, and here's the code from file mmtest.c : #include <gsl/gsl_multimin.h> double my_f (const gsl_vector *v, void *params) { double x, y; double *p = (double *)params; x = gsl_vector_get(v, 0); y = gsl_vector_get(v, 1); return p[2] * (x - p[0]) * (x - p[0]) + p[3] * (y - p[1]) * (y - p[1]) + p[4]; } void my_df (const gsl_vector *v, void *params, gsl_vector *df) { double x, y; double *p = (double *)params; x = gsl_vector_get(v, 0); y = gsl_vector_get(v, 1); gsl_vector_set(df, 0, 2.0 * p[2] * (x - p[0])); gsl_vector_set(df, 1, 2.0 * p[3] * (y - p[1])); } void my_fdf (const gsl_vector *x, void *params, double *f, gsl_vector *df) { *f = my_f(x, params); my_df(x, params, df); } int main (void) { size_t iter = 0; int status; const gsl_multimin_fdfminimizer_type *T; gsl_multimin_fdfminimizer *s; double par[5] = { 1.0, 2.0, 10.0, 20.0, 30.0 }; gsl_vector *x; gsl_multimin_function_fdf my_func; my_func.n = 2; my_func.f = my_f; my_func.df = my_df; my_func.fdf = my_fdf; my_func.params = par; x = gsl_vector_alloc (2); gsl_vector_set (x, 0, 5.0); gsl_vector_set (x, 1, 7.0); T = gsl_multimin_fdfminimizer_conjugate_fr; s = gsl_multimin_fdfminimizer_alloc (T, 2); gsl_multimin_fdfminimizer_set (s, &my_func, x, 0.01, 1e-4); // ALLOCATING Y gsl_vector * Y = gsl_vector_alloc(2); do { iter++; status = gsl_multimin_fdfminimizer_iterate (s); if (status) break; status = gsl_multimin_test_gradient (s->gradient, 1e-3); if (status == GSL_SUCCESS) printf ("Minimum found at:\n"); // ASSIGNING CURRENT VALUE TO Y Y = gsl_multimin_fdfminimizer_x(s); // USING Y printf ("%5d %.5f %.5f %10.5f\n", (int)iter, gsl_vector_get(Y, 0), gsl_vector_get(Y, 1), s->f); } while (status == GSL_CONTINUE && iter < 100); gsl_multimin_fdfminimizer_free (s); gsl_vector_free (x); // FREEING Y -- THIS IS WHERE THE ERROR IS gsl_vector_free(Y); return 0; } ------------------------------------------- Michael Braun Homer A. Burnell (1928) Career Development Professor, and Assistant Professor of Management Science (Marketing Group) MIT Sloan School of Management 100 Main St.., E62-535 Cambridge, MA 02139 bra...@mit.edu 617-253-3436 _______________________________________________ Bug-gsl mailing list Bug-gsl@gnu.org http://lists.gnu.org/mailman/listinfo/bug-gsl