Losing RTTI info after returning from a function
Given a class and subclass:
class Event {...}
class Note : public Event {...}
A Note is Cloned and stored in a pointer within a function f(). The type-information is preserved in the pointer and can be recovered by dynamic_cast:
void f()
{
pEvent = pNote->Clone(); // create a clone of a Note
ASSERT(dynamic_cast<Note*>(pEvent)); // check the pointer, here it works
}
Now, after returning from f() the type-information is lost:
f();
ASSERT(dynamic_cast<Note*>(pEvent)); // -> "Access violation - no RTTI-dat开发者_高级运维a"
The VS-debugger shows a valid pointer-value (unchanged), but not the derived class,
other than while beeing in the f()
-scope.
How can the RTTI-info for a pointer be lost when returning from a function?
There was a destructor accidently doing harm to the pointer. After removing this error, the RTTI works as expected.
精彩评论