Useless checks in Irrlicht source

Discuss about anything related to the Irrlicht Engine, or read announcements about any significant features or usage changes.
Post Reply
slavik262
Posts: 753
Joined: Sun Nov 22, 2009 9:25 pm
Location: Wisconsin, USA

Useless checks in Irrlicht source

Post by slavik262 »

I was going through the source code of the Direct3D9 driver for something I'm working on, and I found this:

Code: Select all

ITexture* tex = new CD3D9Texture(this, size, name, format);
if (tex)
{
	checkDepthBuffer(tex);
	addTexture(tex);
	tex->drop();
}
return tex;
the if(tex) is completely useless. Unless the new keyword has been overwritten on CD3D9Texture (which it hasn't been), it is impossible for C++ to return a null pointer from new. If allocation fails, it will throw std::bad_alloc, but tex will never be null. Why check?
Nalin
Posts: 194
Joined: Thu Mar 30, 2006 12:34 am
Location: Lacey, WA, USA
Contact:

Post by Nalin »

That is assuming that you are not compiling with C++ exceptions disabled, or you are not using an older compiler that returns 0 instead of throws std::bad_alloc.

But yeah, it is unneeded. Somebody was probably in the habit of always checking for null pointers when they wrote that. It doesn't really hurt anyways.
alexionne
Posts: 55
Joined: Fri Jun 22, 2007 9:55 am
Location: Novi Sad, Serbia

Post by alexionne »

Checking for null-pointers is never bad habit. In one of engines I used, they had custom new and delete operators which could return 0 in some cases (the reason is that they pre-allocated memory and new and delete were really fast, but you couldn't allocate always).

In the end, computer memory is limited, and who knows what other applications might be active at the time user runs the game (and he really doesn't care about null pointers and exceptions, but only about nasty crash he would get).
slavik262
Posts: 753
Joined: Sun Nov 22, 2009 9:25 pm
Location: Wisconsin, USA

Post by slavik262 »

I'm not saying it's awful, or a bad habit, but it is useless in this case. The new operator is not overloaded, and any compiler that follows the C++ standard (which is 7+ years old) will never return NULL from new. If you want to talk about always checking your pointers, there should be a try {} catch(std::bad_alloc ex) {} block. I'm not saying that I think that's necessary, but it would accomplish what the NULL check is trying to do.
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

The depth texture thing has changed a lot in the last versions. I'd bet that this was just a reminicent to the old code. Something which changed evolutionary. Could be as well that I wrote that with some other thing in mind, just as Nalin said. Moreover, I find these locations indeed easier to skip over. Otherwise, I'd always stop at this location, checking if the pointer could be wrong.
slavik262
Posts: 753
Joined: Sun Nov 22, 2009 9:25 pm
Location: Wisconsin, USA

Post by slavik262 »

By all means, we'd rather you work on useful code instead of stripping out a bunch of checks. I was just curious if I was not understanding something. :P
Post Reply