GMP crashes in my fractal explorer

John Westwood john at westwoodsolutions.com
Thu Jan 7 16:42:42 CET 2010


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


More information about the gmp-discuss mailing list