33 #include "NCMLDebug.h"
50 , _pool(proto._pool), _preDeleteCallbacks()
62 NCML_ASSERT_MSG(_preDeleteCallbacks.empty(),
63 "~RCObject() called with a non-empty listener list!");
69 BESDEBUG(
"ncml:memory",
"Ref count for " << printRCObject() <<
" is now: " << _count << endl);
78 const_cast<RCObject*>(
this)->executeAndClearPreDeleteCallbacks();
80 BESDEBUG(
"ncml:memory",
81 "Releasing back to pool: Object ref count hit 0. " << printRCObject() <<
" with toString() == " <<
toString() << endl);
82 _pool->
release(const_cast<RCObject*>(
this));
85 BESDEBUG(
"ncml:memory",
86 "Calling delete: Object ref count hit 0. " << printRCObject() <<
" with toString() == " <<
toString() << endl);
91 BESDEBUG(
"ncml:memory",
"unref() called and: " << printRCObject() << endl);
106 _pool->
remove(const_cast<RCObject*>(
this));
113 return printRCObject();
116 string RCObject::printRCObject()
const
118 std::ostringstream oss;
119 oss <<
"RCObject@(" << reinterpret_cast<const void*>(
this) <<
") _count=" << _count <<
" numberDeleteListeners="
120 << _preDeleteCallbacks.size();
128 if (std::find(_preDeleteCallbacks.begin(), _preDeleteCallbacks.end(), pCB) == _preDeleteCallbacks.end()) {
129 BESDEBUG(
"ncml:memory",
130 "Adding WeakRCPtr listener: " << printRCObject() <<
" is getting listener: " << reinterpret_cast<const void*>(pCB) << endl);
131 _preDeleteCallbacks.push_back(pCB);
132 BESDEBUG(
"ncml:memory",
"After listener add, obj is: " << printRCObject() << endl);
140 BESDEBUG(
"ncml:memory",
141 "Removing WeakRCPtr listener from: " << printRCObject() <<
" Removed listener: " << reinterpret_cast<const void*>(pCB) << endl);
142 _preDeleteCallbacks.remove(pCB);
143 BESDEBUG(
"ncml:mempory",
"Object after remove listener is: " << printRCObject() << endl);
147 void RCObject::executeAndClearPreDeleteCallbacks()
154 while (!(_preDeleteCallbacks.empty())) {
156 _preDeleteCallbacks.pop_front();
158 pCB->executeUseCountHitZeroCB(
this);
161 NCML_ASSERT(_preDeleteCallbacks.empty());
179 RCObjectSet::const_iterator foundIt = _liveObjects.find(pObj);
180 return (foundIt != _liveObjects.end());
186 throw string(
"Internal Pool Error: Object added twice!");
188 _liveObjects.insert(pObj);
195 _liveObjects.erase(pObj);
201 BESDEBUG(
"ncml:memory",
202 "RCObjectPool::release(): Calling delete on released object=" << pObj->printRCObject() << endl);
206 BESDEBUG(
"ncml:memory",
207 "RCObjectPool::release(): Removing object, but not deleting it: " << pObj->printRCObject() << endl);
211 BESDEBUG(
"ncml:memory",
"ERROR: RCObjectPool::release() called on object not in pool!! Ignoring!" << endl);
217 BESDEBUG(
"ncml:memory",
"RCObjectPool::deleteAllObjects() started...." << endl);
218 RCObjectSet::iterator endIt = _liveObjects.end();
219 RCObjectSet::iterator it = _liveObjects.begin();
220 for (; it != endIt; ++it) {
224 pObj->executeAndClearPreDeleteCallbacks();
225 BESDEBUG(
"ncml:memory",
"Calling delete on RCObject=" << pObj->printRCObject() << endl);
229 _liveObjects.clear();
230 BESDEBUG(
"ncml:memory",
"RCObjectPool::deleteAllObjects() complete!" << endl);