我的代码是这样的:
// a function that new a char pointer and return it
Unsigned char* RGB2GS(Unsigned char* pRgb, int nw, int nh, int nbpp) {
unsigned char* pGs = new unsigned char[nw*nh*nbpp];
// code for rgb to gs...//
return pGs;
}
int main() {
unsigned char* pmyRgb = ReadBmp(filename);//size 1024x1024, RGB
unsigned char* pMyGs = NULL;
pMyGs = RGB2GS(pmyRgb, 1024, 1024, 24);
delete[] pMyGs ;
delete[] pmyRgb ; // correct typo
我发现有内存泄漏(来自 VS2010 日志)。我在函数内部创建了一个指针并返回它。但是我删除了函数外的指针。这种用法有问题吗?谢谢