C ++内存泄漏 - 需要同行评审

时间:2016-07-09 11:20:19

标签: c++ memory-leaks

我在这个屏幕截图代码中有一个非常大的泄漏。

我的C ++非常糟糕,所以如果有人能指出那些非常棒的问题!

以下是有问题的代码:

FString AWindow::CaptureWindow(HWND hwnd) {

HDC hdcSrc = GetWindowDC(hwnd);
RECT rawRect;
LPRECT rect = &rawRect;

GetWindowRect(hwnd, rect);

int width = rect->right - rect->left;
int height = rect->bottom - rect->top;

HDC hdcDest = CreateCompatibleDC(hdcSrc);
HBITMAP hBitmap = CreateCompatibleBitmap(hdcSrc, width, height);

HGDIOBJ h0ld = SelectObject(hdcDest, hBitmap);
BitBlt(hdcDest, 0, 0, width, height, hdcSrc, 0, 0, SRCCOPY);
SelectObject(hdcDest, h0ld);
DeleteDC(hdcDest);

    char* pImage = NULL;
    pImage = (char*)GlobalLock(hBitmap);


BITMAP bmp;
PBITMAPINFO pbmi;
WORD cClrBits;

GetObject(hBitmap, sizeof(BITMAP), (LPSTR)&bmp);

//Convert the color format to a count of bits
cClrBits = (WORD)(bmp.bmPlanes * bmp.bmBitsPixel);

if (cClrBits == 1)
    cClrBits = 1;
else if (cClrBits <= 4)
    cClrBits = 4;
else if (cClrBits <= 8)
    cClrBits = 8;
else if (cClrBits <= 16)
    cClrBits = 16;
else if (cClrBits <= 24)
    cClrBits = 24;
else cClrBits = 32;

//Allocate memory for the BITMAPINFO structure.
if (cClrBits < 24) {
    pbmi = (PBITMAPINFO)LocalAlloc(LPTR, sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD) * (1i64 << cClrBits));
}
else {
    pbmi = (PBITMAPINFO)LocalAlloc(LPTR, sizeof(BITMAPINFOHEADER));
}

//Initialize the field in the BITMAPINFO structure
pbmi->bmiHeader.biWidth = bmp.bmWidth;
pbmi->bmiHeader.biHeight = bmp.bmHeight;
pbmi->bmiHeader.biPlanes = bmp.bmPlanes;
pbmi->bmiHeader.biBitCount = bmp.bmBitsPixel;
pbmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
pbmi->bmiHeader.biClrUsed = (1i64 << cClrBits);

pbmi->bmiHeader.biCompression = BI_RGB;

//Computer the number of bytes in the array of color
//indices and store the result in biSizeImage.
pbmi->bmiHeader.biSizeImage = ((pbmi->bmiHeader.biWidth * cClrBits + 31) & ~31) / 8
    * pbmi->bmiHeader.biHeight;

//Set biClrImportant to 0, indicating that all of the device
//colors are important
pbmi->bmiHeader.biClrImportant = 0;

FString file = FPaths::Combine(*FPaths::GameDir(), TEXT("ScreenGrab/"), TEXT("Desktop.bmp"));

std::string mystring(TCHAR_TO_UTF8(*file));
std::wstring lpstring = std::wstring(mystring.begin(), mystring.end());
LPCWSTR realfile = lpstring.c_str();

FString error = CreateBMPFile(hwnd, realfile, pbmi, hBitmap, hdcSrc);

ReleaseDC(hwnd, hdcSrc);
DeleteObject(hBitmap);
return error;

}

我猜这里有一些物品我忘了删除但我不知道它们是哪些。

在我这里发布时,我会一次评论代码行,看看我是否可以查明有问题的东西。

由于 -Paul

:: EDIT ::

进一步检查后,我将严重泄漏范围缩小到保存BMP的功能。

这就是:

FString AWindow::CreateBMPFile(HWND hwnd, LPCWSTR pszFile, PBITMAPINFO pbi, HBITMAP hBMP, HDC hDC) {
HANDLE hf;                 // file handle  
BITMAPFILEHEADER hdr;       // bitmap file-header  
PBITMAPINFOHEADER pbih;     // bitmap info-header  
LPBYTE lpBits;              // memory pointer  
DWORD dwTotal;              // total count of bytes  
DWORD cb;                   // incremental count of bytes  
BYTE *hp;                   // byte pointer  
DWORD dwTmp;

pbih = (PBITMAPINFOHEADER)pbi;
lpBits = (LPBYTE)GlobalAlloc(GMEM_FIXED, pbih->biSizeImage);

if (!lpBits) {
    return FString("global alloc failed");
}


// Retrieve the color table (RGBQUAD array) and the bits  
// (array of palette indices) from the DIB.  
if (!GetDIBits(hDC, hBMP, 0, (WORD)pbih->biHeight, lpBits, pbi,
    DIB_RGB_COLORS))
{
    return FString("get di bits failed");
}

// Create the .BMP file.  
hf = CreateFile(pszFile,
    GENERIC_READ | GENERIC_WRITE,
    (DWORD)0,
    NULL,
    CREATE_ALWAYS,
    FILE_ATTRIBUTE_NORMAL,
    (HANDLE)NULL);
if (hf == INVALID_HANDLE_VALUE)
{
    return FString("invalid file handle");
}
hdr.bfType = 0x4d42;        // 0x42 = "B" 0x4d = "M"  
                            // Compute the size of the entire file.  
hdr.bfSize = (DWORD)(sizeof(BITMAPFILEHEADER) +
    pbih->biSize + pbih->biClrUsed
    * sizeof(RGBQUAD) + pbih->biSizeImage);
hdr.bfReserved1 = 0;
hdr.bfReserved2 = 0;

// Compute the offset to the array of color indices.  
hdr.bfOffBits = (DWORD) sizeof(BITMAPFILEHEADER) +
    pbih->biSize + pbih->biClrUsed
    * sizeof(RGBQUAD);

// Copy the BITMAPFILEHEADER into the .BMP file.  
if (!WriteFile(hf, (LPVOID)&hdr, sizeof(BITMAPFILEHEADER),
    (LPDWORD)&dwTmp, NULL))
{
    return FString("Write header failed");
}

// Copy the BITMAPINFOHEADER and RGBQUAD array into the file.  
if (!WriteFile(hf, (LPVOID)pbih, sizeof(BITMAPINFOHEADER)
    + pbih->biClrUsed * sizeof(RGBQUAD),
    (LPDWORD)&dwTmp, (NULL))) {
    return FString("write info header failed");
}


// Copy the array of color indices into the .BMP file.  
dwTotal = cb = pbih->biSizeImage;
hp = lpBits;
if (!WriteFile(hf, (LPSTR)hp, (int)cb, (LPDWORD)&dwTmp, NULL)) {
    return FString("copy color failed");
}

// Close the .BMP file.  
if (!CloseHandle(hf))


    // Free memory.  
    GlobalFree((HGLOBAL)lpBits);

return FString("Completed ok?");

}

再次感谢您的帮助!数字布朗尼适合任何解决这个问题的人!

2 个答案:

答案 0 :(得分:4)

“pbmi”在当地分配。

您忘记使用“LocalFree”来释放分配。

也许还有更多,但这是我发现的第一个。

答案 1 :(得分:0)

使用RAII(资源获取是初始化)模式,您不必在每次返回之前进行关闭和释放。此外,如果抛出异常(例如,UTF8转换中的字符编码错误),您将防止泄漏。以下是@JesperJuhl在您的代码上下文中引用的习语:

using file_raii_t = std::unique_ptr<std::remove_pointer<HANDLE>::type, decltype(&::CloseHandle)>;
using gmem_raii_t = std::unique_ptr<std::remove_pointer<HGLOBAL>::type, decltype(&::GlobalFree)>;

gmem_raii_t gmem(::GlobalAlloc(GMEM_FIXED, size), ::GlobalFree);
// gmem can be used like (!GetDIBits(..., gmem.get(), ...);

file_raii_t fh(::CreateFile(pszFile, GENERIC_READ | GENERIC_WRITE, (DWORD)0, NULL,
    CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, (HANDLE)NULL), ::CloseHandle);
//fh can be used like FileRead(fh.get()...);

这可以扩展到包括其他API(甚至将资源作为out参数返回的API)。在你的情况下:GetWindowDC,LocalAlloc。使用RAII,您可以编写更少的异常安全代码行。

注意:来自我老化记忆的代码。没有在你的代码中测试过。