Usefull tiny snippet

Post those lines of code you feel like sharing or find what you require for your project here; or simply use them as tutorials.
Post Reply
Anthony
Posts: 121
Joined: Sun Jan 09, 2011 12:03 pm

Usefull tiny snippet

Post by Anthony »

The snippet

Code: Select all

#define safeDrop( obj ) if( obj ) if( obj->drop() ) obj = 0
to use this:

Code: Select all

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 :shock: I am no noob, just checking if your not one too :roll:

Thanks to Niko and all others that made Irrlicht possible.
Seven
Posts: 1034
Joined: Mon Nov 14, 2005 2:03 pm

Post by Seven »

very useful.

you missed a 'rcc' when typing though....

while( rcc ) safeDrop(rcc); // drop until deleted, if deleted rcc is set to

I use these also...

Code: Select all

#define CS_INIT(x)		x=0;
#define CS_SAFE_DELETE(x)	if(x) { delete(x);	x=0; }
#define CS_SAFE_DROP(x)		if(x) x->drop();	x=0;
#define CS_SAFE_RELEASE(x)	if(x) x->release(); x=0;
#define CS_SAFE_REMOVE(x)	if(x) x->remove();	x=0;
Anthony
Posts: 121
Joined: Sun Jan 09, 2011 12:03 pm

Post by Anthony »

ever thought rcc was already delete :P, thanks ;)

also thanks for the nice add-ons

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 :shock: I am no noob, just checking if your not one too :roll:

Thanks to Niko and all others that made Irrlicht possible.
Seven
Posts: 1034
Joined: Mon Nov 14, 2005 2:03 pm

Post by Seven »

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.

Code: Select all

#define CS_INIT(x)      x=0; 
#define CS_SAFE_DELETE(x)   if(x) { delete(x);   x=0; } 
#define CS_SAFE_DROP(x)      if(x) (while(!x->drop()) {}   x=0; 
#define CS_SAFE_RELEASE(x)   if(x) (while(!x->release()) {} x=0; 
#define CS_SAFE_REMOVE(x)   if(x) while(!x->remove()) {}   x=0; 
or similar. I will investigate. Thanks for the post which pushed me :)
Anthony
Posts: 121
Joined: Sun Jan 09, 2011 12:03 pm

Post by Anthony »

Memory leaks are tedious creepy crawlers.

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 :shock: I am no noob, just checking if your not one too :roll:

Thanks to Niko and all others that made Irrlicht possible.
Starterz
Posts: 27
Joined: Thu Apr 02, 2009 2:45 am

Special drops

Post by Starterz »

We can use this:

Code: Select all

#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)
See also http://irrlicht.sourceforge.net/phpBB2/ ... ht=#237978
Post Reply