여기에서 발생하는 메모리 누수를 찾을 수있는 사람이 있습니까? 난 그냥 이미지 클래스를 사용하여 메모리에 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();
}
1) 당신이 메모리 누수가 확인되어 있습니까? 어떤 도구를 사용하고 있습니까? 2) 파일 I/O를 생략하고 색상을 {0,0,0}으로 초기화하고 단순화 할 수 있습니다. – Beta
@ 베타 저는 프로세스 탐색기를 사용하여 메모리 사용량을 확인하는 Win7에서 Codeblocks를 사용하고 있습니다. 당신이 말한 것을 시도해 볼게요 – Bruce
누출이 보이지 않습니다. 어쨌든 클래스에서 작업하는 경우 문자열 인수를 생성자에 전달해야합니다 (예 :'const std :: string & src'). 지금 당신은 불필요한 사본을 만들고 있습니다. 참조 대신 사본을 전달하면 신속하게 나쁜 습관이 될 수 있으므로이 부분을 언급합니다. – herzbube