最近在win7用opencv3.0开发软件,发现有内存溢出。到网络上查了很多关于opencv内存溢出的介绍,结果都没有解决问题。最后发现不是opencv本身的问题,而是opencv和win7结合时候出现的问题。
void myfunction()
{......
CImage *dst_img = new CImage();
.......
dst_img->Draw(dst_hdc, dc_offsety, dc_offsetx, dst_img->GetWidth(), dst_img->GetHeight()); //
dst_img->Destroy();
delete dst_img;
return
}
就会内存溢出。
如果改为下面的代码就不会(加入释放windows 窗口DC的语句):
void myfunction()
{......
CImage *dst_img = new CImage();
.......
dst_img->Draw(dst_hdc, dc_offsetx, dc_offsety, dst_img->GetWidth(), dst_img->GetHeight()); //
if (dst_img->GetDC())
{
dst_img->ReleaseDC();
}
dst_img->Destroy();
delete dst_img;
return
}
只要获得windows的DC而没有释放的都会内存溢出,比如:
HDC = ::GetDC(myhwnd);
所以解决问题的关键是要调用函数去释放DC。