Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9162931
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T14:19:51+00:00 2026-06-17T14:19:51+00:00

Can someone please help me find the memory leak which is occurring here? I

  • 0

Can someone please help me find the memory leak which is occurring here? I am just attempting to load a 1600×960 24bit RAW image (46,08,000 bytes) into memory using an Image class I designed. In memory its taking 30MB, as I see in task manager..

Even after the destructor is called (going out of scope) it still takes up 2M. Please help!

#include <cstdio>
#include <iostream>

struct pixel {
    char* color;  // to support various BPP
};

class Image
{
    private:
        pixel** image;
        int     width;
        int     height;
        int     BPP;    // bytes per pixel
        int     size;   // in bytes

    public:
        Image(std::string src, int width, int height, int BPP);
        ~Image();
        pixel** get_matrix(int col, int row, int BPP);
};

pixel** Image :: get_matrix(int col, int row, int BPP)
{
            pixel** matrix = new pixel*[row];
            for(int i=0 ; i<row ; i++)
            {
                matrix[i] = new pixel[col];
                for(int j=0 ; j<col ; j++)
                    matrix[i][j].color = new char[BPP];
            }
            return matrix;
}

Image :: Image(std::string src, int width, int height, int BPP)
{
    FILE *in;
    if( (in = fopen(src.c_str(), "rb")) == NULL )
        image = NULL;
    else
    {
        this->height = height;
        this->width  = width;
        this->BPP    = BPP;
        this->size   = width*BPP*height;

        image = get_matrix(width,height,BPP);
        char* buffer = new char[size];
        fread(buffer, sizeof(char), size, in);

        int l=0;
        for(int i=0 ; i<height ; i++)
        {
            for(int j=0 ; j<width ; j++)
            {
                for(int k=0 ; k<BPP ; k++)
                    image[i][j].color[k] = buffer[l++];
            }
        }
        delete []buffer;
        fclose(in);
    }
}

Image :: ~Image()
{
    for(int i=0 ; i<height ; i++)
    {
        for(int j=0 ; j<width ; j++)
            delete []image[i][j].color;
        delete []image[i];
    }
    delete []image;
}

int main()
{
    {
        getchar();
        Image in("x.raw", 1600, 960, 3);
        getchar();
    }
    getchar();
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T14:19:53+00:00Added an answer on June 17, 2026 at 2:19 pm

    I can not spot a memory leak there, but the program is rather wasteful in terms of memory:

    1. When loading, it loads the entire file into memory and then construct the matrix. At the end of loading it has both the file and the matrix in memory. It could try loading the file iteratively (e.g. line by line) if the format allows.

    2. The image matrix storage format is an array of arrays of arrays. Since arrays on each dimension allocated separately and for each allocated array there is some amount of memory (8-16 bytes normally) used for memory allocator internals, such a way of storing the matrix wastes a lot of memory. Try using a plain std::vector<>, e.g. ideally:

      struct RGB24 { uint8_t r, g, b; }; // one for each pixel format
      std::vector<RGB24> image(width * height); // allocate the matrix in one shot
      RGB24& pixel = image[row * width + col]; // get pixel image[row][col] 
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Please can someone help? I have the following code which uploads a file to
Can someone please help me find a list of flags that are needed by
the following regexp is wrong - can someone please help to find all :-)
Can someone please help me find if there is any option selected I currently
Can someone please help me out, I am just getting into Spring3, Spring3 MVC
Can someone please help me find out an enterprise standard spring application with explanation
Can someone please help me to find out why I can't update Eclipse so
Can someone please help me on this find specific record Edit through MS access
Can someone please help to clarify? Also, please mention if there are other representation
can someone please help me out? I'm trying to create an input dynamically with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.