我反复调用此代码,导致内存泄漏:
ULONG_PTR gdiplusToken;
int screen_height;
int screen_width;
CVCamStream::CVCamStream(HRESULT *phr, CVCam *pParent, LPCWSTR pPinName) : CSourceStream(LPCSTR(FILTER_NAME),phr, pParent, pPinName), m_pParent(pParent)
{
hdc = GetDC(NULL);
Gdiplus::GdiplusStartupInput gdiplusStartupInput;
ULONG_PTR gdiplusToken;
GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
screen_height = GetSystemMetrics(SM_CYVIRTUALSCREEN);
screen_width = GetSystemMetrics(SM_CXVIRTUALSCREEN);
}
CVCamStream::~CVCamStream()
{
Gdiplus::GdiplusShutdown(gdiplusToken);
DeleteDC(hdc);
}
HRESULT CVCamStream::FillBuffer(IMediaSample *pms)
{
REFERENCE_TIME rtNow;
REFERENCE_TIME avgFrameTime = ((VIDEOINFOHEADER*)m_mt.pbFormat)->AvgTimePerFrame;
static clock_t refClock = clock();
double elapsed = (clock() - refClock) / (double)CLOCKS_PER_SEC;
rtNow = m_rtLastTime;
m_rtLastTime += avgFrameTime;
pms->SetTime(&rtNow, &m_rtLastTime);
pms->SetSyncPoint(TRUE);
HDC memdc = CreateCompatibleDC(NULL);
CImage image;
image.Create(screen_width, screen_height, 24);
SelectObject(memdc, image);
BYTE *pData;
pms->GetPointer(&pData);
long lDataLen = pms->GetSize();
Gdiplus::Bitmap *bitmap = new Gdiplus::Bitmap(screen_width, screen_height, PixelFormat24bppRGB);
BitBlt(memdc, 0, 0, screen_width, screen_height, hdc, 0, 0, SRCCOPY);
DeleteDC(memdc);
// memdc is already deleted
bitmap->FromHBITMAP(image, NULL);
delete bitmap;
image.Destroy();
DeleteObject(image);
DeleteDC(memdc);
return NOERROR;
}
但是每次RAM使用量增加。令人讨厌的行是
FromHBITMAP()
函数,因为对其进行注释不再有泄漏。我知道
FromHBITMAP()
创建了位图的副本,但我想我释放了所有的回忆!怎么了
最佳答案
FromHBITMAP
不拥有传递的位图句柄的所有权。您可以只传递仍由CImage
实例拥有的位图句柄。也according to MSDN:
不要将当前(或以前)选择到设备上下文中的GDI位图或GDI调色板传递给Bitmap :: FromHBITMAP方法。
您在image
中仍选择memdc
时调用它。