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

Unified Diff: core/fxcrt/cfx_retain_ptr.h

Issue 2560783003: Catch stray Retains() and Releases() outside of RetainPtr<>. (Closed)
Patch Set: override required per chrome style Created 4 years 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 | core/fxcrt/fx_extension.cpp » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: core/fxcrt/cfx_retain_ptr.h
diff --git a/core/fxcrt/cfx_retain_ptr.h b/core/fxcrt/cfx_retain_ptr.h
index 8c7bf289a7142ab6bd3eba2605e339d66b75bf46..bff1b9691cd2d15913ee318df3310366e87efafe 100644
--- a/core/fxcrt/cfx_retain_ptr.h
+++ b/core/fxcrt/cfx_retain_ptr.h
@@ -60,15 +60,24 @@ class CFX_RetainPtr {
// Trivial implementation - internal ref count with virtual destructor.
class CFX_Retainable {
public:
+ bool HasOneRef() const { return m_nRefCount == 1; }
+
+ protected:
+ virtual ~CFX_Retainable() {}
+
+ private:
+ template <typename U>
+ friend struct ReleaseDeleter;
+
+ template <typename U>
+ friend class CFX_RetainPtr;
+
void Retain() { ++m_nRefCount; }
void Release() {
if (--m_nRefCount == 0)
delete this;
}
- bool HasOneRef() const { return m_nRefCount == 1; }
- protected:
- virtual ~CFX_Retainable() {}
intptr_t m_nRefCount = 0;
};
@@ -76,6 +85,8 @@ namespace pdfium {
// Helper to make a CFX_RetainPtr along the lines of std::make_unique<>(),
// or pdfium::MakeUnique<>(). Arguments are forwarded to T's constructor.
+// Classes managed by CFX_RetainPtr should have protected (or private)
+// constructors, and should friend this function.
template <typename T, typename... Args>
CFX_RetainPtr<T> MakeRetain(Args&&... args) {
return CFX_RetainPtr<T>(new T(std::forward<Args>(args)...));
« no previous file with comments | « no previous file | core/fxcrt/fx_extension.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698