有人可以帮我找到这里发生的内存泄漏吗?我只是尝试使用我设计的 Image 类将 1600x960 24 位 RAW 图像(46,08,000 字节)加载到内存中。正如我在任务管理器中看到的那样,在内存中它占用了 30MB。
即使在调用析构函数(超出范围)之后,它仍然占用 2M。请帮忙!
#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();
}