Hi all, i'm working on some exercises which i have to load data from Image library. I write a load function and it work fine except when the number of images in the library becomes large.

At this moment, the limited number if around 700 images. When the number reaches to a larger number, there will be error like this

http://img248.imageshack.us/f/error1gv.png/

Belove is my function:

void Image::load_Image_From_BMP()
{
    FILE *ImageFile;
    
    if (FAILED(fopen_s(&ImageFile,fileLink,"rb")))
    {
        MessageBox(NULL,L"File doesn't exist",L"ImageLoader.exe",MB_OK);
        return;
    }

    
    fread(&bmfh,sizeof(BITMAPFILEHEADER),1,ImageFile);
    fread(&bmih,sizeof(BITMAPINFOHEADER),1,ImageFile);

    
    dataR = (unsigned char**)malloc(sizeof(unsigned char*)* bmih.biHeight);
    dataG = (unsigned char**)malloc(sizeof(unsigned char*)* bmih.biHeight);
    dataB = (unsigned char**)malloc(sizeof(unsigned char*)* bmih.biHeight);
    dataA = (unsigned char**)malloc(sizeof(unsigned char*)* bmih.biHeight);

    for (int i = 0; i < bmih.biHeight; i++)
    {
        dataR[i] = (unsigned char*)malloc(sizeof(unsigned char) * bmih.biWidth);
        dataG[i] = (unsigned char*)malloc(sizeof(unsigned char) * bmih.biWidth);
        dataB[i] = (unsigned char*)malloc(sizeof(unsigned char) * bmih.biWidth);
        dataA[i] = (unsigned char*)malloc(sizeof(unsigned char) * bmih.biWidth);

    }


      for (int row = 0; row < bmih.biHeight; row++)
      {
          for (int colum = 0; colum < bmih.biWidth; colum++)
          {
              theta[row][colum] = 0.0f;
              r[row][colum] = 0.0f;
          }
      }


    if (bmih.biBitCount  == 24)
    {
        for (int row = 0; row < bmih.biHeight; row++)
        {
            for (int columm = 0; columm < bmih.biWidth; columm++)
            {
                 fread(&dataR[row][columm],sizeof(unsigned char),1,ImageFile);
                  fread(&dataG[row][columm],sizeof(unsigned char),1,ImageFile);
                 fread(&dataB[row][columm],sizeof(unsigned char),1,ImageFile);
            }
        }
    }

    else if (bmih.biBitCount == 32)
    {
        for (int row = 0; row < bmih.biHeight; row++)
        {
            for (int columm = 0; columm < bmih.biWidth; columm++)
            {

                fread(&dataR[row][columm],sizeof(unsigned char),1,ImageFile);
                fread(&dataG[row][columm],sizeof(unsigned char),1,ImageFile);
                fread(&dataB[row][columm],sizeof(unsigned char),1,ImageFile);
                fread(&dataA[row][columm],sizeof(unsigned char),1,ImageFile);

            }
        }
    }

    fclose(ImageFile);
}

and if i use the function like this, error will happen if the number_of_image becomes large (for example 800 images)

for (int i=0; i < number_of_image; i++)
{
    load_Image_From_BMP(fileLink[i]);
}

>>void Image::load_Image_From_BMP()
Why does that function not have a parameter?

>>load_Image_From_BMP(fileLink);

Check the contents of fileLink list. Maybe the problem is bad data in the list. Change your program to just display all the nodes of that linked list (or whatever it is) instead of actually calling the load_Image... function. That will let you visually verify whether the list is ok or not.

@Ancient Dragon: Actually the code

for (int i=0; i < number_of_image; i++)
    {
    load_Image_From_BMP(fileLink[i]);
    }

is just the example code. Even if the load_Image_From_BMP() runs on the same image for > 800 times, the same error still occurs.

The data list for each image is all right, i guarantee that because i print out to check for each image.

My problem is "When i use that load_image_from_BMP() function to load images from the library, if the library has large number of images (for example 800 images), then the error occurs like in the picture above"

I guess there is bug related to the memory, buffer or something like that.

This article has been dead for over six months. Start a new discussion instead.