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]);
}

Dani AI

Generated

Likely causes: heap exhaustion/fragmentation from many small allocations, missing frees, or malformed reads (BMP row padding or wrong header handling). was right to question how files are passed; pointed toward checking allocation results. Two practical paths: (A) fix the loader so each image uses one contiguous buffer and validates every API call, or (B) keep only a few images in memory (cache/stream) and unload the rest.

Quick debug checklist

  • Check every return value: fopen_s (check errno_t), fopen/fread/malloc (NULL or bytes read != expected).
  • Verify bmih.biWidth/biHeight and reject absurd headers. biHeight can be negative (top‑down BMP).
  • Compute worst‑case memory: widthheightchannels per image, then multiply by number_of_images. Example: 1024×768×3 bytes × 800 ≈ 1.8 GB — easily exhausts a 32‑bit process.
  • Watch BMP row padding: rowStride = (width*bpp + 3) & ~3; you must skip padding bytes at row end or read per‑row.

Safer loader sketch (use one contiguous buffer and validate reads):

int bpp = bmih.biBitCount / 8;
size_t rowStride = (bmih.biWidth * bpp + 3) & ~3;
std::vector<unsigned char> pixels((size_t)bmih.biWidth * bmih.biHeight * bpp);
for (int y = 0; y < bmih.biHeight; ++y) {
    unsigned char* dst = pixels.data() + (size_t)y * bmih.biWidth * bpp;
    size_t want = (size_t)bmih.biWidth * bpp;
    if (fread(dst, 1, want, f) != want) { /* handle read error */ }
    if (rowStride > want) fseek(f, rowStride - want, SEEK_CUR); // skip padding
}

Practical remedies

  • Free image buffers when not needed or reuse a single buffer for repeated loads.
  • Prefer std::vector/RAII over raw malloc to avoid leaks.
  • If the app truly needs hundreds of images resident, run 64‑bit or implement on‑demand loading and a small memory cache.
    Use the above checks and tools (CRT debug heap / Task Manager or a profiler) to identify whether allocations fail or memory simply grows without being freed.

Recommended Answers

All 3 Replies

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

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.