调用方法时访问冲突异常 [英] Access violation exception when calling a method

查看:132
本文介绍了调用方法时访问冲突异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里有一个奇怪的问题。假设我有一个类有一些 virtual 方法。在某些情况下,这个类的实例应该调用这些方法之一。大多数时间没有问题发生在那个阶段,但有时,结果是虚拟方法不能被调用,因为指向该方法的指针是NULL(如VS所示),所以内存访问冲突异常发生。这怎么会发生?

I've got a strange problem here. Assume that I have a class with some virtual methods. Under a certain circumstances an instance of this class should call one of those methods. Most of the time no problems occur on that stage, but sometimes it turns out that virtual method cannot be called, because the pointer to that method is NULL (as shown in VS), so memory access violation exception occurs. How could that happen?

应用程序相当大而复杂,所以我不知道什么低级的步骤导致这种情况。发布原始代码不会有用。

Application is pretty large and complicated, so I don't really know what low-level steps lead to this situation. Posting raw code wouldn't be useful.

UPD:好的,我看到我的问题演示是不确定的,所以示意代码看起来像

UPD: Ok, I see that my presentation of the problem is rather indefinite, so schematically code looks like

void MyClass::FirstMethod() const { /* Do stuff */ }
void MyClass::SecondMethod() const
{
    // This is where exception occurs, 
    // description of this method during runtime in VS looks like 0x000000
    FirstMethod(); 
}

不涉及构造函数或析构函数。

No constructors or destructors involved.

推荐答案

堆损坏是一个可能的候选人。对象中的v表指针是脆弱的,它通常是对象中的第一个字段。对于恰好与对象相邻的某些其他对象的缓冲区溢出将擦除v表指针。

Heap corruption is a likely candidate. The v-table pointer in the object is vulnerable, it is usually the first field in the object. A buffer overflow for some kind of other object that happens to be adjacent to the object will wipe the v-table pointer. The call to a virtual method, often much later, will blow.

另一个典型案例是有一个坏的this指针,通常为NULL或低值。当你调用方法的对象引用是坏的时发生。该方法将照常运行,但一旦它试图访问类成员就炸了。同样,堆损坏或使用已删除的指针将导致此。好运调试这个;它永远不容易。

Another classic case is having a bad "this" pointer, usually NULL or a low value. That happens when the object reference on which you call the method is bad. The method will run as usual but blow up as soon as it tries to access a class member. Again, heap corruption or using a pointer that was deleted will cause this. Good luck debugging this; it is never easy.

这篇关于调用方法时访问冲突异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆