BOOL CScreenGrabber::CaptureAFrame(BYTE * *outBuffer, long * outLength)
{
if (mInited)
{ BitBlt(mMemDC,0,0,mScreenWidth,mScreenHeight,mScreenDC,0,0,SRCCOPY);
LONG lLines = GetDIBits(mMemDC, mMemBmp, 0, mScreenHeight, (PVOID)outBuffer,
(BITMAPINFO *)&mHeader, DIB_RGB_COLORS);
*outLength = mScreenWidth * mScreenHeight * mBitDepth / 8;
HANDLE hDIB = DDBToDIB( mMemBmp, BI_RGB, NULL );
cximage->Enable(1);
if (hDIB)
cximage->CreateFromHANDLE(hDIB); if(!cximage->IsGrayScale())
cximage->IncreaseBpp(16);
cximage->SetJpegQuality(70); //jpg压缩的质量 long size=0;
BYTE* buffer=0;
////////////////////////////////////////////////////////内存溢出
cximage->Encode(buffer,size,CXIMAGE_FORMAT_JPG);
*outLength =size;
*outBuffer = buffer;
////////////////////////////////////////////////////////
::GlobalFree( hDIB );
DeleteObject(mMemBmp);
} return FALSE;
}上面用/////扩起来的三句话,出现内存溢出,每做一次这个函数都溢出几百k,不要这几行代码,就不会出现溢出,怎样解决这个问题?
{
if (mInited)
{ BitBlt(mMemDC,0,0,mScreenWidth,mScreenHeight,mScreenDC,0,0,SRCCOPY);
LONG lLines = GetDIBits(mMemDC, mMemBmp, 0, mScreenHeight, (PVOID)outBuffer,
(BITMAPINFO *)&mHeader, DIB_RGB_COLORS);
*outLength = mScreenWidth * mScreenHeight * mBitDepth / 8;
HANDLE hDIB = DDBToDIB( mMemBmp, BI_RGB, NULL );
cximage->Enable(1);
if (hDIB)
cximage->CreateFromHANDLE(hDIB); if(!cximage->IsGrayScale())
cximage->IncreaseBpp(16);
cximage->SetJpegQuality(70); //jpg压缩的质量 long size=0;
BYTE* buffer=0;
////////////////////////////////////////////////////////内存溢出
cximage->Encode(buffer,size,CXIMAGE_FORMAT_JPG);
*outLength =size;
*outBuffer = buffer;
////////////////////////////////////////////////////////
::GlobalFree( hDIB );
DeleteObject(mMemBmp);
} return FALSE;
}上面用/////扩起来的三句话,出现内存溢出,每做一次这个函数都溢出几百k,不要这几行代码,就不会出现溢出,怎样解决这个问题?
BYTE* buffer=0;
////////////////////////////////////////////////////////内存溢出
cximage->Encode(buffer,size,CXIMAGE_FORMAT_JPG);
这样就等价于:
cximage->Encode(0,0,CXIMAGE_FORMAT_JPG);
应该先分配好缓冲区吧?
bool CxImage::Encode ( BYTE *& buffer,
long & size,
DWORD imagetype
) [inherited] Saves to memory buffer the image in a specific format. Parameters:
buffer,: output memory buffer pointer. Must be NULL, the function allocates and fill the memory, the application must free the buffer, see also FreeMemory().
size,: output memory buffer size.
imagetype,: file format, see ENUM_CXIMAGE_FORMATS Returns:
true if everything is ok
void CxImage::FreeMemory ( void * memblock ) [inherited] simply calls "if (memblock) free(memblock);". Useful when calling Encode for a memory buffer, from a DLL compiled with different memory management options. CxImage::FreeMemory will use the same memory environment used by Encode.
bool CxImage::Encode ( BYTE *& buffer,
long & size,
DWORD imagetype
) [inherited] Saves to memory buffer the image in a specific format. Parameters:
buffer,: output memory buffer pointer. Must be NULL, the function allocates and fill the memory, the application must free the buffer, see also FreeMemory().
size,: output memory buffer size.
imagetype,: file format, see ENUM_CXIMAGE_FORMATS Returns:
true if everything is ok
void CxImage::FreeMemory ( void * memblock ) [inherited] simply calls "if (memblock) free(memblock);". Useful when calling Encode for a memory buffer, from a DLL compiled with different memory management options. CxImage::FreeMemory will use the same memory environment used by Encode.
*outLength =size;
应该是这句有泄露,
我推断你想在encode里面将某些数据通过buffer返回,但很可惜,这在你的代码中无法实现,因为你传入的只是指针的一个拷贝,你修改的只是指针的拷贝,buffer的值还是null,所以指针的拷贝所指向的内存也泄露了,哈哈,泄露就是在这里了。