7

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

brijesh
  • 120
  • 1
  • 7
  • does this object implement at least one `virtual` function? even if it don't, there is little chance that the vptr will be `NULL` as it is (often) used to store the `typeinfo`. – Vijay Mathew Oct 19 '10 at 07:16
  • Are you sure that the memory is being cleaned? It might be due to [this behavior](http://www.artima.com/cppsource/nevercall.html) you have implemented. – Keynslug Oct 19 '10 at 07:26
  • You got the whole range of _Undefined Behavior_ to pick from - and that's a lot. – sbi Oct 19 '10 at 07:31
  • 1
    @Vijay: That's wrong, because `typeid` only works polymorphically if the class in question is polymorphic, i.e., it has at least one virtual function. That was defined this way exactly to avoid compilers having to include a vptr in _every_ class. – sbi Oct 19 '10 at 07:32
  • @sbi yes, but aren't we talking about polymorphic types here? – Vijay Mathew Oct 19 '10 at 09:18
  • @Vijay: Ok, my reference was dangling. I was referring to your "even if it don't...". It would be a funny compiler where objects without virtual functions would have a vptr. – sbi Oct 19 '10 at 10:02

3 Answers3

7
  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
  • 76,846
  • 14
  • 164
  • 167
5

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
  • 167,383
  • 100
  • 513
  • 979
  • This construct should not be used on any class that has non-POD data, not just those with v-tables. – CashCow Oct 19 '10 at 09:28
0

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
  • 30,981
  • 5
  • 61
  • 92