ATSlooking4things
ATSlooking4things

Reputation: 145

Exception thrown at (...) ucrtbased.dll - at the return;

I get this error Exception thrown at 0x00007FFAA9FEC49C (ucrtbased.dll) at the end of a function. The funcion basically receives 8 arrays and 4 arraysizes and draws an image and saves it in a .bmp.

The image gets drawn (I assume that correctly, at least it looks like it should) and saved. So, it works. But when it the execution reaches the return 0; command, it breaks and pops up the error.

I'll leave here the code, just in case (and I know, it looks bad).

int border_rep(int width, int height, int tolerance, int degrade_pixels, int local_id, int threshold, int num_type,
    float* Y_left_tar, float* X_left_tar, int arraysizeX_left_tar,
    float* Y_right_tar, float* X_right_tar, int arraysizeX_right_tar, 
    float* Y_left_ref, float* X_left_ref, int arraysizeX_left_ref, 
    float* Y_right_ref, float* X_right_ref, int arraysizeX_right_ref)
{
    int l, j, k;
    char filename2[32] = "";

    unsigned char* img_slopes = (unsigned char*)malloc(width * height * sizeof(unsigned char));

    for (l = 0; l < width; l++) {            //get a SizeX * Size Y white image
        for (j = 0; j < height; j++) {
            *(img_slopes + j * width + l) = 255;
        }
    }

    for (l = 0; l < arraysizeX_left_tar; l++) {         //infill the left target edge
        *(img_slopes + (int)(Y_left_tar[l] * width + X_left_tar[l])) = 0;
    }
    for (l = 0; l < arraysizeX_left_ref; l++) {         //infill the left reference edge
        *(img_slopes + (int)(Y_left_ref[l] * width + X_left_ref[l])) = 0;
    }
    for (l = 0; l < arraysizeX_right_tar; l++) {        //infill the right target edge
        *(img_slopes + (int)(Y_right_tar[l] * width + X_right_tar[l])) = 0;
    }
    for (l = 0; l < arraysizeX_right_ref; l++) {        //infill the right reference edge
        *(img_slopes + (int)(Y_right_ref[l] * width + X_right_ref[l])) = 0;
    }
    l = 845 + tolerance;
    for (j = 0; j < height; j++) {
        *(img_slopes + j * width + l) = 0;  //print the right vertical line on the middle
    }
    l = 574 - tolerance;
    for (j = 0; j < height; j++) {
        *(img_slopes + j * width + l) = 0;  //print the left vertical line on the middle
    }
    /* PRINT DEGRADE ON LEFT TAR */
    l = (int)(X_left_tar[0]);
    k = (int)(X_left_tar[0]) + degrade_pixels - 1;
    for (j = 0; j < ((int)(Y_left_tar[0]) + ((int)Y_left_ref[0])) / 2; j++) {
        *(img_slopes + j * width + l) = 0;
        *(img_slopes + j * width + k) = 0;
    }
    /* PRINT DEGRADE ON RIGHT TAR */
    l = (int)(X_right_tar[arraysizeX_right_tar - 1]);
    k = (int)(X_right_tar[arraysizeX_right_tar - 1]) - degrade_pixels - 1;
    for (j = 0; j < ((int)(Y_right_tar[0]) + ((int)Y_right_ref[0])) / 2; j++) {
        *(img_slopes + j * width + l) = 0;
        *(img_slopes + j * width + k) = 0;
    }
    /* PRINT DEGRADE ON LEFT REF */
    l = (int)(X_left_ref[0]);
    k = (int)(X_left_ref[0]) + degrade_pixels - 1;
    for (j = ((int)(Y_left_tar[0]) + (int)(Y_left_ref[0])) / 2; j < height; j++) {
        *(img_slopes + j * width + l) = 0;
        *(img_slopes + j * width + k) = 0;
    }

    /* PRINT DEGRADE ON RIGHT REF */
    l = (int)(X_right_ref[arraysizeX_right_ref - 1]);
    k = (int)(X_right_ref[arraysizeX_right_ref - 1]) - degrade_pixels - 1;
    for (j = ((int)(Y_right_ref[0]) + (int)(Y_right_tar[0])) / 2; j < height; j++) {
        *(img_slopes + j * width + l) = 0;
        *(img_slopes + j * width + k) = 0;
    }

    if (num_type == 0) {
        snprintf(filename2, sizeof filename2, "Border_slopes_%d_T%d_raw.png", local_id, threshold);
    }
    else if (num_type == 1) {
        snprintf(filename2, sizeof filename2, "Border_slopes_%d_T%d_dec.png", local_id, threshold);
    }
    else if(num_type == 2) {
        snprintf(filename2, sizeof filename2, "Border_slopes_%d_T%d_int.png", local_id, threshold);
    }
    
    stbi_write_bmp(filename2, width, height, 1, img_slopes);
    stbi_image_free(img_slopes);
    free(img_slopes);
    return 0;

I know that these exceptions come from dereferencing pointers or incorrectly accessing memory but.. if the function works up until the end.. what's the problem? It doesn't make any sense to me. I'm freeing the memory from the malloc and the stb_image

Upvotes: 0

Views: 79

Answers (1)

nielsen
nielsen

Reputation: 7644

At least one problem is that you are freeing img_slopes twice. Since img_slopes was allocated with malloc(), it should (only) be freed with free().

Hence, removing the line

stbi_image_free(img_slopes);

should solve the problem.

Upvotes: 2

Related Questions