How do I check for a valid node pointer?

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
agamemnus
Posts: 283
Joined: Sun Jan 31, 2010 6:06 pm

How do I check for a valid node pointer?

Post by agamemnus »

Hi,

Is there any function that I can use to check whether a node pointer is valid? (ie: points to a complete node)
ent1ty
Competition winner
Posts: 1106
Joined: Sun Nov 08, 2009 11:09 am

Post by ent1ty »

Code: Select all

if(node)
{
    //do your funky stuff
}
off course, when you need a node pointer no more, you have to set it to 0
irrRenderer 1.0
Height2Normal v. 2.1 - convert height maps to normal maps

Step back! I have a void pointer, and I'm not afraid to use it!
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Post by CuteAlien »

ent1ty wrote:

Code: Select all

if(node)
{
    //do your funky stuff
}
off course, when you need a node pointer no more, you have to set it to 0
And at least as important - you have to initialize all your pointers with 0 yourself. In c++ you usually do that in the constructor - you initialize all pointers of a class (and usually also all other variables) to 0. This is so important because otherwise your pointers will not be initialized at all and have a random value.
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
Virion
Competition winner
Posts: 2148
Joined: Mon Dec 18, 2006 5:04 am

Post by Virion »

in irrlicht, the engine already initialized the node as 0 by default. so if its an invalid node you will get a zero. so you can do this:

Code: Select all

if (node != 0)
{
  //do your stuff
}
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Post by CuteAlien »

Virion wrote:in irrlicht, the engine already initialized the node as 0 by default
That's wrong. Actually a software library can't(!) initialize your pointers. How should it do that? Those pointers are in your own code.
Initialize your variables in c++ or they will have random values - it does not matter of which type they are.

edit: What Irrlicht often does is to return 0 as value when some function fails. For example if you call addSceneNode() then it will return 0 on failure and if you assign that to a variable it will have a value afterwards. But do not let that stop you from initializing your pointers - it is basically always a very bad idea not doing that. Not doing so will lead to the strangest effects (like code only running in debug...hehe - I just see your other thread)
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
agamemnus
Posts: 283
Joined: Sun Jan 31, 2010 6:06 pm

Post by agamemnus »

I can't set it to 0 when it becomes invalid in some cases. That's why I need to be able to test if a pointer is valid, not if it's 0. :D (test without the program crashing!)
Bate
Posts: 364
Joined: Sun Nov 01, 2009 11:39 pm
Location: Germany

Post by Bate »

As far as I know there is no other way to check for a valid pointer except for checking the address.
However, since you can't fully control your pointers, it's maybe more about general design? :)
Never take advice from someone who likes to give advice, so take my advice and don't take it.
Virion
Competition winner
Posts: 2148
Joined: Mon Dec 18, 2006 5:04 am

Post by Virion »

CuteAlien wrote:it is basically always a very bad idea not doing that. Not doing so will lead to the strangest effects (like code only running in debug...hehe - I just see your other thread)
damn. lol :oops:
agamemnus
Posts: 283
Joined: Sun Jan 31, 2010 6:06 pm

Post by agamemnus »

Bate wrote:As far as I know there is no other way to check for a valid pointer except for checking the address.
However, since you can't fully control your pointers, it's maybe more about general design? :)
Obviously, yeah... :\

Here's how I end my program (in Freebasic):

irrStop (calls various commands in Irrlicht to stop Irrlicht)
system

The system calls all my destructors... since Irrlicht already freed my data (but didn't clear the pointers), the destructors that destroy Irrlicht data would create a segmentation error because they'll be trying to free data that is already freed... only way to fix this atm is to simply remove the Irrlicht-specific destructor. That just gets a bit confusing, though.. what if I want to destroy an object in the middle of my pogram? I need that destructor to stay there!

If the destructor were to stay, I'd have to manually call all the Irrlicht-specific destructors before calling irrStop. Annoying.
Acki
Posts: 3496
Joined: Tue Jun 29, 2004 12:04 am
Location: Nobody's Place (Venlo NL)
Contact:

Post by Acki »

agamemnus wrote:The system calls all my destructors
ehm, you must not call a destructor on your own !!! :shock:
they'll be executed automatically when the instance gets deleted !!!
if you want to clear something manually then best is to create a "cleanup" function and call this, and there delete all the pointers (remove the objects) and then set the pointers to 0...
but never ever call a destructor !!! ;)
while(!asleep) sheep++;
IrrExtensions:Image
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
agamemnus
Posts: 283
Joined: Sun Jan 31, 2010 6:06 pm

Post by agamemnus »

Acki wrote:
agamemnus wrote:The system calls all my destructors
ehm, you must not call a destructor on your own !!! :shock:
they'll be executed automatically when the instance gets deleted !!!
if you want to clear something manually then best is to create a "cleanup" function and call this, and there delete all the pointers (remove the objects) and then set the pointers to 0...
but never ever call a destructor !!! ;)
I think you misunderstand...

I'm talking about my own destructors, not any Irrlicht destructors.
Lonesome Ducky
Competition winner
Posts: 1123
Joined: Sun Jun 10, 2007 11:14 pm

Post by Lonesome Ducky »

No he understands. I think he was under the impression you were actually calling the destructor, rather than deleting the pointer and having the destructor called automatically.
Acki
Posts: 3496
Joined: Tue Jun 29, 2004 12:04 am
Location: Nobody's Place (Venlo NL)
Contact:

Post by Acki »

agamemnus wrote:I'm talking about my own destructors
hmm, maybe you shouldn't call it "destructor" but "cleanup function" !?!?!
constructor/destructor are fixed terms in C++... ;)

a good idea to prevent missunderstandings is to show the code !!!
in this case how your cleanup function and destructor looks like and how/when you call the cleanup... ;)
while(!asleep) sheep++;
IrrExtensions:Image
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
agamemnus
Posts: 283
Joined: Sun Jan 31, 2010 6:06 pm

Post by agamemnus »

It's in Freebasic... doubtful anyone here knows it.

I avoided the problem by simply not calling destructors at the end of the program. I'll let the OS handle that. :P
terier
Posts: 34
Joined: Sun Oct 05, 2008 4:46 pm

Post by terier »

agamemnus wrote:I'll let the OS handle that. :P
You shouldn't rely too much on your OS. Instead, try to understand how destructors work and how to properly delete object instances.
Post Reply