Note that there are some explanatory texts on larger screens.

plurals
  1. POUnable to find simple memory leak
    primarykey
    data
    text
    <p>Can someone please help me find the memory leak which is occurring here? I am just attempting to load a 1600x960 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..</p> <p>Even after the destructor is called (going out of scope) it still takes up 2M. Please help!</p> <pre><code>#include &lt;cstdio&gt; #include &lt;iostream&gt; 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&lt;row ; i++) { matrix[i] = new pixel[col]; for(int j=0 ; j&lt;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-&gt;height = height; this-&gt;width = width; this-&gt;BPP = BPP; this-&gt;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&lt;height ; i++) { for(int j=0 ; j&lt;width ; j++) { for(int k=0 ; k&lt;BPP ; k++) image[i][j].color[k] = buffer[l++]; } } delete []buffer; fclose(in); } } Image :: ~Image() { for(int i=0 ; i&lt;height ; i++) { for(int j=0 ; j&lt;width ; j++) delete []image[i][j].color; delete []image[i]; } delete []image; } int main() { { getchar(); Image in("x.raw", 1600, 960, 3); getchar(); } getchar(); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload