Reputation: 19873
I stumbled upon this piece of code which seems totaly broken to me, but it does happen that this
is null
. I just don't get how this can be null
it is inside a normal method call such as
myObject->func();
inside MyObject::func()
we have
if (!this) { return false; }
is there any way I can have the first line to throw a NullPointerException
instead of going inside the null
(?) method?
Upvotes: 10
Views: 2218
Reputation: 41519
A way to trap these kind of errors (by design) is using a pointer-wrapper class (resembling a shared_ptr
) that throws upon creation with a null pointer argument. It may throw also when dereferenced, but that's a little late - better than nothing, I guess.
Upvotes: 2
Reputation: 311634
It's possible for this
to be null. I suspect that this code is trying to (badly) detect a race condition, where the object is not yet finished being initialized, or has been deleted.
Upvotes: 1
Reputation: 13581
(this == NULL) is undefined behaviour according to the standard. I think you should remove this check :)
Assume we make the following call:
((CSomeClass*) 0)->method();
The behaviour is already undefined, so why bother doing the check for this == NULL in CSomeClass::method ?
EDITED: I Assume that your compiler will handle (0 == this) if you don't use member variables, but where would it find the virtual table pointer? In this case, your class can not use polymoprhism.
Upvotes: 1
Reputation: 116704
If you have:
MyObject *o = NULL;
o->func();
What happens next depends on whether func
is virtual. If it is, then it will crash, because it needs an object to get the vtable from. But if it's not virtual the call proceeds with the this pointer set to NULL.
I believe the standard says this is "undefined behaviour", so anything could happen, but typical compilers just generate the code to not check whether the pointer is NULL. Some well known libraries rely on the behaviour I described: MFC has a function called something like SafeGetHandle
that can be called on a null pointer, and returns NULL in that case.
You might want to write a reusable helper function:
void CheckNotNull(void *p)
{
if (p == NULL)
throw NullPointerException();
}
You can then use that at the start of a function to check all its arguments, including this
:
CheckNotNull(this);
Upvotes: 22
Reputation: 73463
this pointer can become null in cases like these:
class Test
{
public:
bool f();
private:
int m_i;
};
bool Test::f()
{
if(!this)
{
return false;
}
m_i = 0;
return true;
}
int main(int argc, char **argv)
{
Test* p = new Test;
delete p;
p = NULL;
p->f();
}
I guess somebody did a quick hack to avoid to the access violation exception.
Upvotes: 0
Reputation: 76710
this == null should only occur if you are calling a method on a deleted object, or if something is writing to memory that it should not (and overwriting the object's this pointer in particular).
You should investigate what is really wrong with your code rather than trying to work around it like this.
Upvotes: -1
Reputation: 58687
if(this == null)
throw new NullPointerException;
if(!this)
return false;
Upvotes: 1