tags:

views:

110

answers:

4

Hi Guys,

While debugging one of the program's core dump I came across the scenario where its contained object which is polymorphic loses its VPTr and I can see its pointing to NULL.

What could be the scenario when an object loses its VPTr.

Thanks in advance, Brijesh

+4  A: 
  1. The memory has been trashed, i.e. something overwrote the memory.

  2. You destroyed it by calling delete or by invoking the destructor directly. This typically does not NULL out the vptr, it will just end up having it point to the vtable of the base class, but that depends on your implementation.

Most likely, case 1. If you have a debugger that has memory breakpoints and if you can reproduce the problem reliably, set a memory breakpoint on the vptr and see what's modifying it.

EboMike
+2  A: 

Likely something overwrote the whole object. Something like this:

memset( object, 0, sizeof( *object ) );

which is fine until it is used on an object with vptr.

sharptooth
This construct should not be used on any class that has non-POD data, not just those with v-tables.
CashCow
A: 

Maybe some slicing?

Johann Gerell
Slicing would not chcnge the vptr value - it would either crop it entirely or leave unchanged.
sharptooth
@sharptooth: Good point. I'll let my answer (actually, your comment) stay put as a reference.
Johann Gerell
A: 

It may be that you are trying to use the v-table during your object's destructor. The v-table is not available at this time.

CashCow