From 76cb1fcf0b5b9d8415e2517c482bab0c6c6602fb Mon Sep 17 00:00:00 2001 From: Nat Goodspeed Date: Mon, 29 Jun 2015 15:37:32 -0400 Subject: MAINT-4952: Add IntrusivePtr wrapper for boost::intrusive_ptr. For a RefCounted subclass T, boost::intrusive_ptr must be instantiated as boost::intrusive_ptr(raw ptr, false) to avoid immortal instances. Forgetting that final bool parameter is both easy and extremely hard to spot with desk checking or code review. IntrusivePtr provides constructors that Do The Right Thing, so we can typedef a subclass T's ptr_t to IntrusivePtr rather than directly to boost::intrusive_ptr. --- indra/llcorehttp/_refcounted.h | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'indra/llcorehttp') diff --git a/indra/llcorehttp/_refcounted.h b/indra/llcorehttp/_refcounted.h index cd16e2e2b4..7f713f2298 100755 --- a/indra/llcorehttp/_refcounted.h +++ b/indra/llcorehttp/_refcounted.h @@ -32,6 +32,7 @@ #include "fix_macros.h" #include +#include #include "llapr.h" @@ -120,6 +121,24 @@ inline void RefCounted::destroySelf() delete this; } +/** + * boost::intrusive_ptr may be used to manage RefCounted classes. + * Unfortunately RefCounted and boost::intrusive_ptr use different conventions + * for the initial refcount value. To avoid leaky (immortal) objects, you + * should really construct boost::intrusive_ptr(rawptr, false). + * IntrusivePtr encapsulates that for you. + */ +template +struct IntrusivePtr: public boost::intrusive_ptr +{ + IntrusivePtr(): + boost::intrusive_ptr() + {} + IntrusivePtr(T* p): + boost::intrusive_ptr(p, false) + {} +}; + inline void intrusive_ptr_add_ref(RefCounted* p) { p->addRef(); -- cgit v1.2.3 From 80d17b2dd9cdd7a9445480fdb0e12774396751eb Mon Sep 17 00:00:00 2001 From: Nat Goodspeed Date: Mon, 29 Jun 2015 17:19:51 -0400 Subject: MAINT-4952: Use IntrusivePtr for BufferArray,HttpHeaders,HttpOptions. Specifically, change the ptr_t typedefs for these LLCore classes to use IntrusivePtr rather than directly using boost::intrusive_ptr. This allows us to use a simple ptr_t(raw ptr) constructor rather than having to remember to code ptr_t(raw ptr, false) everywhere. In fact, the latter form is now invalid: remove the now-extraneous 'false' constructor parameters. --- indra/llcorehttp/bufferarray.h | 2 +- indra/llcorehttp/httpheaders.h | 2 +- indra/llcorehttp/httpoptions.h | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) (limited to 'indra/llcorehttp') diff --git a/indra/llcorehttp/bufferarray.h b/indra/llcorehttp/bufferarray.h index 076f341736..320adf2b8b 100755 --- a/indra/llcorehttp/bufferarray.h +++ b/indra/llcorehttp/bufferarray.h @@ -74,7 +74,7 @@ public: BufferArray(); - typedef boost::intrusive_ptr ptr_t; + typedef LLCoreInt::IntrusivePtr ptr_t; protected: virtual ~BufferArray(); // Use release() diff --git a/indra/llcorehttp/httpheaders.h b/indra/llcorehttp/httpheaders.h index 51bd76a01d..8f14568fa3 100755 --- a/indra/llcorehttp/httpheaders.h +++ b/indra/llcorehttp/httpheaders.h @@ -92,7 +92,7 @@ public: /// the instance. HttpHeaders(); - typedef boost::intrusive_ptr ptr_t; + typedef LLCoreInt::IntrusivePtr ptr_t; protected: virtual ~HttpHeaders(); // Use release() diff --git a/indra/llcorehttp/httpoptions.h b/indra/llcorehttp/httpoptions.h index 21ecff85af..2fe05a65ff 100755 --- a/indra/llcorehttp/httpoptions.h +++ b/indra/llcorehttp/httpoptions.h @@ -60,7 +60,7 @@ class HttpOptions : public LLCoreInt::RefCounted public: HttpOptions(); - typedef boost::intrusive_ptr ptr_t; + typedef LLCoreInt::IntrusivePtr ptr_t; protected: virtual ~HttpOptions(); // Use release() -- cgit v1.2.3