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

Side by Side 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: Fix nullptr issue in ScopedGDIObject. 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 unified diff | Download patch
« no previous file with comments | « no previous file | chrome/browser/ui/views/frame/glass_browser_frame_view.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef BASE_WIN_SCOPED_GDI_OBJECT_H_ 5 #ifndef BASE_WIN_SCOPED_GDI_OBJECT_H_
6 #define BASE_WIN_SCOPED_GDI_OBJECT_H_ 6 #define BASE_WIN_SCOPED_GDI_OBJECT_H_
7 7
8 #include <windows.h> 8 #include <windows.h>
9 9
10 #include "base/basictypes.h" 10 #include "base/basictypes.h"
11 #include "base/logging.h" 11 #include "base/logging.h"
12 #include "base/move.h"
12 13
13 namespace base { 14 namespace base {
14 namespace win { 15 namespace win {
15 16
16 // Like ScopedHandle but for GDI objects. 17 // Like ScopedHandle but for GDI objects.
17 template<class T> 18 template<class T>
18 class ScopedGDIObject { 19 class ScopedGDIObject {
grt (UTC plus 2) 2015/11/06 19:49:20 can you implement this in terms of ScopedGeneric?
anpol 2015/11/06 20:28:16 Looking at ScopedGeneric briefly: * ScopedGDIObje
20 MOVE_ONLY_TYPE_WITH_MOVE_CONSTRUCTOR_FOR_CPP_03(ScopedGDIObject)
21
19 public: 22 public:
20 ScopedGDIObject() : object_(NULL) {} 23 ScopedGDIObject() : object_(nullptr) {}
24 ScopedGDIObject(decltype(nullptr)) : object_(nullptr) {}
25
26 // Constructor that takes ownership of an object.
21 explicit ScopedGDIObject(T object) : object_(object) {} 27 explicit ScopedGDIObject(T object) : object_(object) {}
22 28
29 // Constructor that transfers ownership from another ScopedGDIObject.
30 ScopedGDIObject(ScopedGDIObject&& other) : object_(nullptr) {
31 Set(other.release());
32 }
33
23 ~ScopedGDIObject() { 34 ~ScopedGDIObject() {
24 Close(); 35 Close();
25 } 36 }
26 37
27 T Get() { 38 T Get() {
28 return object_; 39 return object_;
29 } 40 }
30 41
31 void Set(T object) { 42 void Set(T object) {
32 if (object_ && object != object_) 43 if (object_ && object != object_)
33 Close(); 44 Close();
34 object_ = object; 45 object_ = object;
35 } 46 }
36 47
48 // operator= that deletes the currently owned object, if any.
49 ScopedGDIObject& operator=(decltype(nullptr)) {
50 Set(nullptr);
51 return *this;
52 }
53
54 // operator= that takes ownership of an object.
37 ScopedGDIObject& operator=(T object) { 55 ScopedGDIObject& operator=(T object) {
38 Set(object); 56 Set(object);
39 return *this; 57 return *this;
40 } 58 }
41 59
60 // operator= that transfers ownership from another ScopedGDIObject.
61 ScopedGDIObject& operator=(ScopedGDIObject&& rhs) {
62 Set(rhs.release());
63 return *this;
64 }
65
42 T release() { 66 T release() {
43 T object = object_; 67 T object = object_;
44 object_ = NULL; 68 object_ = nullptr;
45 return object; 69 return object;
46 } 70 }
47 71
48 operator T() { return object_; } 72 operator T() { return object_; }
49 73
50 private: 74 private:
51 void Close() { 75 void Close() {
52 if (object_) 76 if (object_)
53 DeleteObject(object_); 77 DeleteObject(object_);
54 } 78 }
55 79
56 T object_; 80 T object_;
57 DISALLOW_COPY_AND_ASSIGN(ScopedGDIObject);
58 }; 81 };
59 82
60 // An explicit specialization for HICON because we have to call DestroyIcon() 83 // An explicit specialization for HICON because we have to call DestroyIcon()
61 // instead of DeleteObject() for HICON. 84 // instead of DeleteObject() for HICON.
62 template<> 85 template<>
63 void inline ScopedGDIObject<HICON>::Close() { 86 void inline ScopedGDIObject<HICON>::Close() {
64 if (object_) 87 if (object_)
65 DestroyIcon(object_); 88 DestroyIcon(object_);
66 } 89 }
67 90
68 // Typedefs for some common use cases. 91 // Typedefs for some common use cases.
69 typedef ScopedGDIObject<HBITMAP> ScopedBitmap; 92 typedef ScopedGDIObject<HBITMAP> ScopedBitmap;
70 typedef ScopedGDIObject<HRGN> ScopedRegion; 93 typedef ScopedGDIObject<HRGN> ScopedRegion;
71 typedef ScopedGDIObject<HFONT> ScopedHFONT; 94 typedef ScopedGDIObject<HFONT> ScopedHFONT;
72 typedef ScopedGDIObject<HICON> ScopedHICON; 95 typedef ScopedGDIObject<HICON> ScopedHICON;
73 96
74 } // namespace win 97 } // namespace win
75 } // namespace base 98 } // namespace base
76 99
77 #endif // BASE_WIN_SCOPED_GDI_OBJECT_H_ 100 #endif // BASE_WIN_SCOPED_GDI_OBJECT_H_
OLDNEW
« no previous file with comments | « no previous file | chrome/browser/ui/views/frame/glass_browser_frame_view.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698