Commit 1e4883b9 authored by reed's avatar reed Committed by Commit bot

fix leak of hbitmap

use SaveDC/RestoreDC to track the original/default hbitmap in the DC. This way we can recover the actual hbitmap using GetCurrentObject, use RestoreDC to delect it and restore the original, and then delete our hbitmap and the dc.

BUG=680971

Review-Url: https://codereview.chromium.org/2629913002
Cr-Commit-Position: refs/heads/master@{#443583}
parent 07db4ac1
...@@ -20,10 +20,22 @@ ...@@ -20,10 +20,22 @@
namespace { namespace {
static void DeleteHDCCallback(void*, void* context) { static void DeleteHDCCallback(void*, void* context) {
DCHECK_NE(context, nullptr);
HDC hdc = static_cast<HDC>(context); HDC hdc = static_cast<HDC>(context);
HBITMAP hbitmap = static_cast<HBITMAP>(SelectObject(hdc, nullptr));
DeleteObject(hbitmap); // We must get this before we call RestoreDC, as that will drop hbitmap and
DeleteDC(hdc); // reselect the original/default bitmap.
HGDIOBJ hbitmap = GetCurrentObject(hdc, OBJ_BITMAP);
DCHECK_NE(hbitmap, nullptr);
bool success = RestoreDC(hdc, -1); // effectly performs the deselect of hbitmap
DCHECK(success);
// Now we are the only owner, so we can delete our bitmap
success = DeleteObject(hbitmap);
DCHECK(success);
success = DeleteDC(hdc);
DCHECK(success);
} }
// Allocate the layer and fill in the fields for the Rec, or return false // Allocate the layer and fill in the fields for the Rec, or return false
...@@ -52,6 +64,13 @@ static bool Create(int width, ...@@ -52,6 +64,13 @@ static bool Create(int width,
return false; return false;
} }
SetGraphicsMode(hdc, GM_ADVANCED); SetGraphicsMode(hdc, GM_ADVANCED);
int saveIndex = SaveDC(hdc); // so we can Restore in the delete callback
DCHECK_NE(saveIndex, 0);
// Be sure to select *after* we called SaveDC.
// Because we're using save/restore, we don't need to explicitly track the
// returned "previous" value.
SelectObject(hdc, hbitmap); SelectObject(hdc, hbitmap);
rec->fReleaseProc = DeleteHDCCallback; rec->fReleaseProc = DeleteHDCCallback;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment