How can this not be?

S

Steven T. Hatton

I had the most bizarre C++ experience yet. I was traversing a tree using a
visitor pattern which I had mistakenly given bad information about where to
go in the tree. I'm pretty sure it was climbing out of the bottom. That
doesn't surprize me. The fact that it caused the program to crash is to be
expected. But when I looked at the stack in the debugger, it was saying
that this=0x0, and then showing me the value of a member of the object that
had a null pointer for /this/.

What the way the program works is that I pass a visitor object to a member
function on the root node of the tree. Then nodes pass the visitor to one
of their child nodes until it reaches a leaf. When it gets to a leaf, the
traversal function behaves differently, in that it calls a method on the
visitor like this visitorObject->visit(this). I could trace it past the
call to visit(this). That tells me that the functions were invoked for a
non-existant object. Does anybody else find that strange?
 
B

Bill Thompson

Steven T. Hatton said:
I had the most bizarre C++ experience yet. I was traversing a tree using a
visitor pattern which I had mistakenly given bad information about where to
go in the tree. I'm pretty sure it was climbing out of the bottom. That
doesn't surprize me. The fact that it caused the program to crash is to be
expected. But when I looked at the stack in the debugger, it was saying
that this=0x0, and then showing me the value of a member of the object that
had a null pointer for /this/.

What the way the program works is that I pass a visitor object to a member
function on the root node of the tree. Then nodes pass the visitor to one
of their child nodes until it reaches a leaf. When it gets to a leaf, the
traversal function behaves differently, in that it calls a method on the
visitor like this visitorObject->visit(this). I could trace it past the
call to visit(this). That tells me that the functions were invoked for a
non-existant object. Does anybody else find that strange?


--
STH
Hatton's Law: "There is only One inviolable Law"
KDevelop: http://www.kdevelop.org SuSE: http://www.suse.com
Mozilla: http://www.mozilla.org

Not really, code is not stored in the object, just the data.
 
J

John Harrison

Steven T. Hatton said:
I had the most bizarre C++ experience yet. I was traversing a tree using a
visitor pattern which I had mistakenly given bad information about where to
go in the tree. I'm pretty sure it was climbing out of the bottom. That
doesn't surprize me. The fact that it caused the program to crash is to be
expected. But when I looked at the stack in the debugger, it was saying
that this=0x0, and then showing me the value of a member of the object that
had a null pointer for /this/.

When C++ is implemented, this is generally a parameter to a member function
much like any other. You just managed to pass 0 as that parameter.

john
 
V

Victor Bazarov

John said:
When C++ is implemented, this is generally a parameter to a member function
much like any other. You just managed to pass 0 as that parameter.

It used to be allowed, even required in some cases, to check 'this' for
being NULL because it sometimes was possible to call a member function for
a non-existent object through a null pointer. Not anymore, fortunately.

Victor
 
M

Marco Manfredini

Steven said:
But when I looked at the stack in the debugger, it was
saying that this=0x0, and then showing me the value of a member of the
object that had a null pointer for /this/.

A reckless debugger.

[...]
That tells me that the functions were invoked
for a
non-existant object. Does anybody else find that strange?

Are you referring to the fact that

struct A
{
void foo() { cout << this << endl; }
};

int main()
{
A*a=0;
a->foo();
}

prints 0 with most compilers? Well dereferencing 0 is undefined, anyway
it seems that the actual condition isn't easy to recognize without
runtime overhead, an implementation that doesn't need to dereference
anything (read:access memory) might work... And usually:

struct A
{
virtual void foo() { cout << this << endl; }
};

will crashboom, 'cause most Compilers store the vtbl in the first bytes
of the object, coming to access adress 0 here[1].

Marco

This isn't relevant anymore, but MFC had a couple of *virtual* methods
that had a "if (this==NULL) {...}" statement inside..Don't know if that
was a kind of voodoo or direct qualified call paranoia.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,175
Messages
2,570,942
Members
47,489
Latest member
BrigidaD91

Latest Threads

Top