Hi

Recently I had the following error occur when the CImg destructor is called, 0xC0000005: Access violation. It occurs at the end of the function unwrapFilteredImage when the destructor is called and I have traced it to the point where inImage is being destructed. The first thing that is destructed is outImage, then wrappedDisplay the the offending inImage. Any ideas as to why this is occuring?

Thanks for any help

void unwrapFilteredImage(U8 *inputImage, int image_width, int image_height)
{  
    int x0,y0,x1,y1;
    int Rclick = 0, Lclick = 0;

    // read in image, create display then display image in new window
    CImg <float> inImage(inputImage, image_width, image_height);
    CImgDisplay wrappedDisplay(800,600,"Wrapped Image", 0, false, false);
    inImage.display(wrappedDisplay);

    // select the region to unwrap with left click selecting one corner and right click the other
    while (!wrappedDisplay.is_closed()) {
         if (wrappedDisplay.button()&1) { // Left button clicked.
            x0 = wrappedDisplay.mouse_x()*image_width/800;
            y0 = wrappedDisplay.mouse_y()*image_height/600;
            Lclick=1;
         }
         else if (wrappedDisplay.button()&2) { // Right button clicked.
            x1 = wrappedDisplay.mouse_x()*image_width/800;
            y1 = wrappedDisplay.mouse_y()*image_height/600; 
            Rclick=1;
         }  
         // close the display when the left and right click have been detected
         if (Rclick + Lclick == 2)
             wrappedDisplay.close();
    }
    //// crop the image to the selected region
    inImage.crop(x0,y0,x1,y1,true);
    inImage.save("WrappedImage.bmp");

    float *WrappedImage, *UnwrappedImage;
    int image_size = inImage.width() * inImage.height();
    int No_of_Edges = (inImage.width())*(inImage.height()-1) + (inImage.width()-1)*(inImage.height());

    WrappedImage = inImage;

    //allocate memory
    UnwrappedImage = (float *) calloc(image_size, sizeof(float));
    PIXEL *pixel = (PIXEL *) calloc(image_size, sizeof(PIXEL));
    EDGE *edge = (EDGE *) calloc(No_of_Edges, sizeof(EDGE));;

    //initialise the pixels
    initialisePIXELs(WrappedImage, pixel, inImage.width(),inImage.height());
    calculate_reliability(WrappedImage, pixel, inImage.width(),inImage.height());
    horizentalEDGEs(pixel, edge, inImage.width(),inImage.height());
    verticalEDGEs(pixel, edge, inImage.width(),inImage.height());

    //sort the EDGEs depending on their reliability. The PIXELs with higher reliability (small value) first
    quick_sort(edge, No_of_Edges);

    //gather PIXELs into groups
    gatherPIXELs(edge, inImage.width(), inImage.height());

    //unwrap the whole image
    unwrapImage(pixel, inImage.width(),inImage.height());
    //copy the image from PIXEL structure to the wrapped phase array passed to this function
    returnImage(pixel, UnwrappedImage, inImage.width(),inImage.height());

    free(edge);
    free(pixel);
    free(WrappedImage);

    //check the output image
    CImg <float> outImage(UnwrappedImage, inImage.width(),inImage.height());
    outImage.save("UnwrappedImage.bmp");

    free(UnwrappedImage);

    generate3dSurface(outImage);
}

Allow me to deconstruct the code into what I believe are the relevant lines:

CImg <float> inImage(inputImage, image_width, image_height);
float *WrappedImage, *UnwrappedImage;
WrappedImage = inImage;
free(WrappedImage);

If CImg handles its memory internally as one would expect, I see nothing good coming out of that call to free().

Ahhh I forgot the i deleted the memory allocation for WrappedImage. Once I got rid of the free() it all worked out. Thanks!!

This question has already been answered. Start a new discussion instead.