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

Unified Diff: base/win/scoped_gdi_object.h

Issue 1406403007: Eliminate HICON leaks caused by creating icons from bitmap image. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Use ScopedHICON instead of HICON. Created 5 years, 1 month 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
Index: base/win/scoped_gdi_object.h
diff --git a/base/win/scoped_gdi_object.h b/base/win/scoped_gdi_object.h
index 57b013e2fbaae8cf80fc7f32a452b204fc4b923c..9d8465b25bde5bbc952ebedb385dea79519c2ead 100644
--- a/base/win/scoped_gdi_object.h
+++ b/base/win/scoped_gdi_object.h
@@ -7,64 +7,32 @@
#include <windows.h>
-#include "base/basictypes.h"
-#include "base/logging.h"
+#include "base/scoped_generic.h"
namespace base {
namespace win {
-// Like ScopedHandle but for GDI objects.
-template<class T>
-class ScopedGDIObject {
- public:
- ScopedGDIObject() : object_(NULL) {}
- explicit ScopedGDIObject(T object) : object_(object) {}
-
- ~ScopedGDIObject() {
- Close();
- }
-
- T Get() {
- return object_;
- }
-
- void Set(T object) {
- if (object_ && object != object_)
- Close();
- object_ = object;
- }
-
- ScopedGDIObject& operator=(T object) {
- Set(object);
- return *this;
- }
-
- T release() {
- T object = object_;
- object_ = NULL;
- return object;
- }
+namespace internal {
- operator T() { return object_; }
-
- private:
- void Close() {
- if (object_)
- DeleteObject(object_);
- }
-
- T object_;
- DISALLOW_COPY_AND_ASSIGN(ScopedGDIObject);
+template <class T>
+struct ScopedGDIObjectTraits {
+ static T InvalidValue() { return nullptr; }
+ static void Free(T object) { DeleteObject(object); }
};
// An explicit specialization for HICON because we have to call DestroyIcon()
// instead of DeleteObject() for HICON.
-template<>
-void inline ScopedGDIObject<HICON>::Close() {
- if (object_)
- DestroyIcon(object_);
+template <>
+void inline ScopedGDIObjectTraits<HICON>::Free(HICON icon) {
+ DestroyIcon(icon);
}
+} // namespace internal
+
+// Like ScopedHandle but for GDI objects.
+template <class T>
+using ScopedGDIObject = ScopedGeneric<T, internal::ScopedGDIObjectTraits<T>>;
+
// Typedefs for some common use cases.
typedef ScopedGDIObject<HBITMAP> ScopedBitmap;
typedef ScopedGDIObject<HRGN> ScopedRegion;

Powered by Google App Engine
This is Rietveld 408576698