Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(341)

Unified Diff: skia/ext/raster_handle_allocator_win.cc

Issue 2629913002: fix leak of hbitmap (Closed)
Patch Set: fix dchecks Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: skia/ext/raster_handle_allocator_win.cc
diff --git a/skia/ext/raster_handle_allocator_win.cc b/skia/ext/raster_handle_allocator_win.cc
index 93e1a252159d01b11b9bafc31041737b20676a0e..e78d622e48074d1de95c1abea70a5073dd11aa99 100644
--- a/skia/ext/raster_handle_allocator_win.cc
+++ b/skia/ext/raster_handle_allocator_win.cc
@@ -20,10 +20,22 @@
namespace {
static void DeleteHDCCallback(void*, void* context) {
+ DCHECK_NE(context, nullptr);
HDC hdc = static_cast<HDC>(context);
- HBITMAP hbitmap = static_cast<HBITMAP>(SelectObject(hdc, nullptr));
- DeleteObject(hbitmap);
- DeleteDC(hdc);
+
+ // We must get this before we call RestoreDC, as that will drop hbitmap and
+ // 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
@@ -52,6 +64,13 @@ static bool Create(int width,
return false;
}
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);
rec->fReleaseProc = DeleteHDCCallback;
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698