954,496 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

[Reading file] The number of file is limited

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]);
}
hl1202
Newbie Poster
4 posts since Oct 2010
Reputation Points: 10
Solved Threads: 0
 

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

>>load_Image_From_BMP(fileLink[i]);

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
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

@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.

hl1202
Newbie Poster
4 posts since Oct 2010
Reputation Points: 10
Solved Threads: 0
 

You seem to be running out of memory; verify that malloc()s return a non-NULL pointer.

vijayan121
Posting Virtuoso
1,606 posts since Dec 2006
Reputation Points: 1,159
Solved Threads: 287
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: