C++罕见的运行时错误
我有一个类 B,它继承了类 A 并带有一些虚函数。 B类还有一个虚函数(foo),似乎没有地址。当我使用调试器时,它指出 foo 有 0x00000000
地址,当我尝试介入时,它将失败,并在 0x00000005
处出现访问冲突。如果我使该函数不是虚拟的,则调试器将介入并正常工作,直到我到达 std::vector
。当我调用 push_back
时,它会在地址 0x000000005
处发生相同的访问冲突,同时在地址 0xabababab
处写入一些内容,并且调用堆栈点插入函数中的互斥锁。
注意:我没有使用任何其他线程,每次编译时增量链接器都会崩溃。只有完整的链接器才能成功创建 exe。编译器来自 Visual Studio 2008 pro,在删除未使用的源文件和源代码时开始出现此问题。
不幸的是,我无法恢复到之前的状态,以便发现造成此情况的更改。
如何在不恢复整个项目的情况下检测问题的根源?还有谁遇到过这种错误,也许可能是同样的原因。
I have a class B that inherits the class A with some virtual functions. Class B also has a virtual function (foo) that seems to have no address. When i walk with the debugger it points that foo has 0x00000000
address and when i try to step in it will fail with access violation at 0x00000005
. If i make that function not virtual the debugger steps in and will work fine until i reach a std::vector
. There when i call push_back
it will fail with the same access violation at address 0x000000005
while writing some stuff at address 0xabababab
, and the call stack points to a mutex lock in insert function.
Note: I'm not using any other thread and the incremental linker will crash every time i compile. Only the full linker will successfully create the exe. The compiler is from Visual Studio 2008 pro and this problem started to occur when stripping out unused source files and source code.
Unfortunately i was unable to revert to the previous state, in order to spot the change that created this.
How can i detect the source of the problem, without reverting the entire project? Also has anyone encountered this kind of error, maybe it might the same cause.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您猜测虚拟表已损坏,但这不太可能,因为虚拟表通常存储在只读内存中。
我可以想到这种行为的两个原因:
我已经通过 printf 调试成功跟踪了此类问题:在析构函数 B 的构造函数中添加几行
printf("XXX %p", this);
、虚函数和失败函数,您将能够推断出发生了什么。是的,我知道,printf 调试并不酷......
You guess that the virtual table is broken, but that's unlikely, because vtables are usually stored in read-only memory.
I can think of two reasons for this behavior:
I have successfully tracked this kind of issues with printf debugging: Add a few lines with
printf("XXX %p", this);
in the constructor of B, the destructor, the virtual functions and the failing function, and you'll be able to deduce what is happening.Yes, I know, printf debugging is not cool...
您正在空指针上调用虚拟函数。编译器添加的代码将使用对象中的隐藏指针来定位最终的重写器,但该操作失败。当您将函数更改为非虚拟函数时,调用会静态分派,但由于
this
指针为空,对成员的访问再次失败。您应该检查在代码中调用该方法的对象的有效性。
You are calling a virtual function on a null pointer. The compiler adds code that will use a hidden pointer in the object to locate what is the final overrider, and that operation is failing. When you change the function to non-virtual, the call is dispatched statically, but again, access to members fail as the
this
pointer is null.You should check the validity of the object on which you are calling the method in your code.