GMP crashes in my fractal explorer

John Westwood john at westwoodsolutions.com
Fri Jan 8 12:47:31 CET 2010


Hi Paul,

I am using the log function of MPFR somewhere else in my program. I use raw 
GMP in the actual fractal calculation. I ran valgrind and here is the relevant 
part of the output:



==28510== Thread 2:                                                                                       
==28510== Invalid free() / delete / delete[]                                                              
==28510==    at 0x4C21A58: free (vg_replace_malloc.c:325)                                                 
==28510==    by 0x419EC9: Phoenix::gmp_calculateNoPeriod(std::vector<Point, 
std::allocator<Point> >*) (phoenix.cpp:228)                                                                                                                     
==28510==    by 0x411AB0: Fractal::calculate(std::vector<Point, 
std::allocator<Point> >*, bool) (fractal.h:244)       
==28510==    by 0x4105F4: RenderThread::calcTopHalfRow(int&) 
(renderthread.cpp:311)                                   
==28510==    by 0x4103AA: RenderThread::run() (renderthread.cpp:247)                                                  
==28510==    by 0x5C7DCA4: ??? (in /usr/lib64/libQtCore.so.4.5.3)                                                     
==28510==    by 0x7B4F7CC: start_thread (in /lib64/libpthread-2.10.1.so)                                              
==28510==    by 0x68D037C: clone (in /lib64/libc-2.10.1.so)                                                           
==28510==  Address 0x303030 is not stack'd, malloc'd or (recently) free'd                                             
==28510==                                                                                                             
==28510== Invalid free() / delete / delete[]                                                                          
==28510==    at 0x4C21A58: free (vg_replace_malloc.c:325)                                                             
==28510==    by 0x419EC9: Phoenix::gmp_calculateNoPeriod(std::vector<Point, 
std::allocator<Point> >*) (phoenix.cpp:228)                                                                                                                     
==28510==    by 0x411AB0: Fractal::calculate(std::vector<Point, 
std::allocator<Point> >*, bool) (fractal.h:244)       
==28510==    by 0x410802: RenderThread::calcBottomHalfRow(int&) 
(renderthread.cpp:352)                                
==28510==    by 0x4103CB: RenderThread::run() (renderthread.cpp:253)                                                  
==28510==    by 0x5C7DCA4: ??? (in /usr/lib64/libQtCore.so.4.5.3)                                                     
==28510==    by 0x7B4F7CC: start_thread (in /lib64/libpthread-2.10.1.so)                                              
==28510==    by 0x68D037C: clone (in /lib64/libc-2.10.1.so)                                                           
==28510==  Address 0x303030 is not stack'd, malloc'd or (recently) free'd                                             


Regards,

John




On Friday 08 Jan 2010 08:21:33 you wrote:
>        Dear John,
> 
> you mention MPFR but your code does not seem to use MPFR (unless you
>  included "mpf2mpfr.h" before). In any case, if you think your problem is
>  related to MPFR, please send us a complete program demonstrating the
>  problem.
> 
> Did you try valgrind on your program?
> 
> Paul Zimmermann
> 
> > From: John Westwood <john at westwoodsolutions.com>
> > Date: Thu, 7 Jan 2010 15:42:42 +0000
> > Content-Type: text/plain; charset="us-ascii"
> >
> > Hello,
> >
> > I am writing a fractal explorer program in Qt and C++, it uses the MPFR
> > and GMP libraries to implement arbitrary precision. It works fine for the
> > Mandelbrot fractal, but for the Phoenix fractal (which has a slightly
> > more complex formula) it crashes.
> >
> > The debugger (I am using Qt Creator) shows the crash to be in
> > /lib64/libc.so.6. My code looks like this:
> >
> > vector<Element>* Phoenix::gmp_calculateNoPeriod(vector<Point>* points)
> > {
> >         results.clear();
> >         vector<Point>::const_iterator i = points->begin();
> >
> >         int testCount;
> >         mpf_t cx;
> >         mpf_t cy;
> >         mpf_t zx;
> >         mpf_t zy;
> >         mpf_t zx2;
> >         mpf_t zy2;
> >         mpf_t temp;
> >         mpf_t new_zx;
> >         mpf_t new_zy;
> >         mpf_t old_zx;
> >         mpf_t old_zy;
> >
> >         mpf_init(cx);
> >         mpf_init(cy);
> >         mpf_init(zx);
> >         mpf_init(zy);
> >         mpf_init(zx2);
> >         mpf_init(zy2);
> >         mpf_init(temp);
> >         mpf_init(new_zx);
> >         mpf_init(new_zy);
> >         mpf_init(old_zx);
> >         mpf_init(old_zy);
> >
> >         initParams(i->x, i->y, cx, cy, zx, zy);
> >         mpf_set(old_zx, zx);
> >         mpf_set(old_zy, zy);
> >
> >         for(testCount = 0; testCount < maxIterations; ++testCount)
> >         {
> >                 mpf_mul(zx2, zx, zx);
> >                 mpf_mul(zy2, zy, zy);
> >
> >                 mpf_set_d(new_zx, 0.5);
> >                 mpf_mul(new_zx, new_zx, old_zx);
> >                 mpf_add(new_zx, new_zx, zx2);
> >                 mpf_sub(new_zx, new_zx, zy2);
> >                 mpf_add(new_zx, new_zx, cx);
> >
> >                 mpf_set_d(temp, 0.5);
> >                 mpf_mul(temp, temp, old_zy);
> >                 mpf_mul_ui(new_zy, zx, 2);
> >                 mpf_mul(new_zy, new_zy, zy);
> >                 mpf_add(new_zy, new_zy, temp);
> >                 mpf_add(new_zy, new_zy, cy);
> >
> >                 mpf_set(old_zx, zx);
> >                 mpf_set(old_zy, zy);
> >                 mpf_set(zx, new_zx);
> >                 mpf_set(zy, new_zy);
> >
> >                 mpf_add(temp, zx2, zy2);
> >
> >                 if(mpf_cmp_d(temp, bailout) > 0)
> >                 {
> >                     break;
> >                 }
> >         }
> >
> >         // Convert arbitrary precision results to double precision
> >         char zx_array[17] = "0000000000000000";
> >         char zy_array[17] = "0000000000000000";
> >
> >         gmp_sprintf(zx_array, "%.16Ff0", zx);
> >         gmp_sprintf(zy_array, "%.16Ff0", zy);
> >
> >         double zx_d = atof(zx_array);
> >         double zy_d = atof(zy_array);
> >
> >         // Update state
> >         updateState(i->x, i->y, testCount, zx_d, zy_d);
> >         results.push_back(Element(testCount, zx_d, zy_d));
> >
> >         mpf_clear(cx);
> >         mpf_clear(cy);
> >         mpf_clear(zx);
> >         mpf_clear(zy);
> >         mpf_clear(zx2);
> >         mpf_clear(zy2);
> >         mpf_clear(temp);
> >         mpf_clear(new_zx);
> >         mpf_clear(new_zy);
> >         mpf_clear(old_zx);
> >         mpf_clear(old_zy);
> >
> >         return &results;
> > }
> >
> > If I comment out the last mpf_clear then the program does not crash -
> > however, this will cause a memory leak.
> >
> > I am using GMP 4.3.1 on Mandriva 2010. If anybody has any ideas on what I
> > am doing wrong it would be much appreciated. Thank you.
> >
> > Regards,
> >
> > John
> > _______________________________________________
> > gmp-discuss mailing list
> > gmp-discuss at gmplib.org
> > https://gmplib.org/mailman/listinfo/gmp-discuss
> 


More information about the gmp-discuss mailing list