diff --git a/src/memory/SharedPtr.cpp b/src/memory/SharedPtr.cpp index b4f5ef48bb..2530360a5a 100644 --- a/src/memory/SharedPtr.cpp +++ b/src/memory/SharedPtr.cpp @@ -32,11 +32,12 @@ namespace Sass { bool SharedObj::taint = false; SharedObj::SharedObj() - : refcounter(0) + : detached(false) #ifdef DEBUG_SHARED_PTR , dbg(false) #endif { + refcounter = 0; #ifdef DEBUG_SHARED_PTR if (taint) all.push_back(this); #endif @@ -62,7 +63,9 @@ namespace Sass { // AST_Node_Ptr ast = dynamic_cast(node); if (node->dbg) std::cerr << "DELETE NODE " << node << "\n"; #endif - delete(node); + if (!node->detached) { + delete(node); + } } } } @@ -70,6 +73,7 @@ namespace Sass { void SharedPtr::incRefCount() { if (node) { ++ node->refcounter; + node->detached = false; #ifdef DEBUG_SHARED_PTR if (node->dbg) { std::cerr << "+ " << node << " X " << node->refcounter << " (" << this << ") " << "\n"; @@ -107,4 +111,4 @@ namespace Sass { incRefCount(); } -} +} \ No newline at end of file diff --git a/src/memory/SharedPtr.hpp b/src/memory/SharedPtr.hpp index fbfd003125..7e13bf06c7 100644 --- a/src/memory/SharedPtr.hpp +++ b/src/memory/SharedPtr.hpp @@ -49,6 +49,8 @@ namespace Sass { #endif static bool taint; long refcounter; + // long refcount; + bool detached; #ifdef DEBUG_SHARED_PTR bool dbg; #endif @@ -80,7 +82,7 @@ namespace Sass { virtual const std::string to_string() const = 0; virtual ~SharedObj(); - long getRefCount() const { + long getRefCount() { return refcounter; } }; @@ -121,10 +123,11 @@ namespace Sass { bool isNull () const { return node == NULL; }; - SharedObj* detach() { - SharedObj* result = node; - node = NULL; - return result; + SharedObj* detach() const { + if (node) { + node->detached = true; + } + return node; }; operator bool() const { return node != NULL; @@ -194,7 +197,8 @@ namespace Sass { T* ptr () const { return static_cast(this->obj()); }; - T* detach() { + T* detach() const { + if (this->obj() == NULL) return NULL; return static_cast(SharedPtr::detach()); } bool isNull() const { @@ -210,4 +214,4 @@ namespace Sass { } -#endif +#endif \ No newline at end of file