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

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

问题描述

我在这里遇到了一个奇怪的问题.假设我有一个带有一些 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-table 指针很容易受到攻击,它通常是对象中的第一个字段.碰巧与该对象相邻的某种其他对象的缓冲区溢出将擦除 v-table 指针.对虚方法的调用通常会在很晚之后失败.

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天全站免登陆