CRefCountedClass* rcc = new CRefCountedClass();
rcc->grab(); // ref count = 2
rcc->grab();
safeDrop( rcc ); // drops it
rcc->grab(); // ref count = 2
rcc->grab(); // ref count = 3
rcc->grab(); // ref count = 4
while( rcc ) safeDrop( rcc ); // drop until deleted, if deleted rcc is set to NULL
if( rcc ) // check if rcc is a valid pointer to prefend access violation
{
// will not be executed
};
EDIT: Bugfix that Seven pointed out to me
Last edited by Anthony on Sat Feb 05, 2011 1:50 pm, edited 1 time in total.
No I am no noob, just checking if your not one too
Thanks to Niko and all others that made Irrlicht possible.
however your safe drop isn't quite safe as it will nil it even when the ref counter isn't 0, or isn't this for the irrlicht IReferenceCounter?
#define CS_SAFE_DROP(x) if(x) x->drop(); x=0;
No I am no noob, just checking if your not one too
Thanks to Niko and all others that made Irrlicht possible.
took me a minute to see what you were asking. yes, you are right about it. not sure why it never showed up in my code though. thanks for showing, i will investigate to see if i have some dangling objects around.
Sometimes I see (around the net) people writing they never have to check for it as memory leaks won't happen to them.
Can never believe this as even the most clean coder can forget just one bit - times infinity - and there you have it. And that would clearify the amount of tedious applications on the internet.
No I am no noob, just checking if your not one too
Thanks to Niko and all others that made Irrlicht possible.
#define SAFE_DELETE(x) {if(x) delete(x),x=NULL;}
#define IRR_SAFE_REMOVE(x) {if(x) while(!x->remove());x=NULL;}
#define IRR_SAFE_DROP(x) {if(x->drop()) x=NULL;}
/// Special drops:
/// As we can see drop() method just delete an object when ref. counter is 0.
/// But this method DO NOT remove an object from SceneGraph so this structure have grows and memory leak..
/// Other hand remove() method do the same WITH erasing an object from Children AND (*object)->Parent = 0;
#define IRR_SAFE_NODE_DROP(x) {if(x->getReferenceCount()>1) x->drop(); else IRR_SAFE_REMOVE(x);}
#define IRR_SAFE_GUIE_DROP(x) IRR_SAFE_NODE_DROP(x)