From ba67eaab578e52af90f5284fc701db086879b867 Mon Sep 17 00:00:00 2001 From: chatty Date: Wed, 6 Jul 1994 10:59:37 +0000 Subject: ----State -> State -= 2 New technique for finding dynamic objects --- utils/SmartPointer.cc | 44 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) (limited to 'utils') diff --git a/utils/SmartPointer.cc b/utils/SmartPointer.cc index 9775b49..d83dbbc 100644 --- a/utils/SmartPointer.cc +++ b/utils/SmartPointer.cc @@ -17,9 +17,14 @@ #include #include "SmartPointer.h" +#include "List.h" CcuSmartData::check_type CcuSmartData::check = doWarn; +#ifdef OLD int CcuSmartData::NextCreatedIsDynamic = 0; +#else +CcuList* CcuSmartData::LastDynamics; +#endif /*?class CcuSmartData The class \typ{CcuSmartData} is the base class for objects that you want to reference through smart pointers. @@ -43,20 +48,47 @@ want to define your own \fun{new} and \fun{delete} in derived classes. If you do should take care that \fun{new} sets the flag \var{CcuSmartData::NextCreatedIsDynamic} to a non-null value. ?*/ +/*! +One big problem with smart pointers lies in making a distinction between +objects created in the stack (that we should not delete) and objects created +dynamically, that we should delete when no more referenced. There's no way +to make that distinction in constructors, sowe try to use operator new. This +works well only if the constructor is called right after operator new, which is not +always the case... +!*/ void* CcuSmartData :: operator new (unsigned long size) { +#ifdef OLD NextCreatedIsDynamic = 1; return ::operator new (size); +#else + int sz = (int) size; + void* p = ::operator new (sz); + if (!LastDynamics) + LastDynamics = new CcuList; + LastDynamics->Prepend (p); + return p; +#endif + } /*? Create a data, ie. initialize its reference count to 0. ?*/ CcuSmartData :: CcuSmartData () +#ifdef OLD : State (NextCreatedIsDynamic ? 0 : 1) +#else +: State (1) +#endif { +#ifdef OLD NextCreatedIsDynamic = 0; +#else + if (LastDynamics && LastDynamics->Remove (this)) + State = 0; +#endif } /*? @@ -66,9 +98,18 @@ all derived class will have a copy constructor defined implicitly, unless you specify one explicitely. ?*/ CcuSmartData :: CcuSmartData (const CcuSmartData&) +#ifdef OLD : State (NextCreatedIsDynamic ? 0 : 1) +#else +: State (1) +#endif { +#ifdef OLD NextCreatedIsDynamic = 0; +#else + if (LastDynamics && LastDynamics->Remove (this)) + State = 0; +#endif } /*? @@ -99,7 +140,8 @@ CcuSmartData :: ~CcuSmartData () void CcuSmartData :: DecrRef () { - if (----State == 0) + State -=2; + if (State == 0) delete this; } -- cgit v1.1