Why does my C++ object loses its VPTr

不羁岁月 提交于 2019-12-07 02:15:47

问题


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


回答1:


  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.




回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/3966039/why-does-my-c-object-loses-its-vptr

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!