Access Violation Exception When Calling a Method

I have a strange problem here. Suppose I have a class with some virtual methods. Under certain circumstances, an instance of this class must call one of these methods. In most cases, there are no problems at this stage, but sometimes it turns out that the virtual method cannot be called because the pointer to this method is NULL (as shown in VS), therefore, an exception to the memory access violation occurs. How could this happen?

The application is quite large and complex, so I don’t know which low-level steps lead to this situation. Posting raw code will not be useful.

UPD: Well, I see that my presentation of the problem is rather vague, so the schematic 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.

+3
source share
7 answers

Heap damage 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 other object that appears next to the object will destroy the v-table pointer. A call to a virtual method, often much later, will hit.

"this", NULL . , , , . , , . , , , . ; .

+5

, ( ) , .

, - (, reinterpret_cast ), vtable .

( ) - vtable.

? , vtable , . , vtable .

+2

, . , .

+1

, "this" SecondMethod?

, SecondMethod ( undefined) , . , , "this" / , , 0xcdcdcdcd 0xfdfdfdfd . ( ), VS alloc/dealloc, .

+1

, , , , . , , .

, , "" , , , , .

0

, Studio , . , , / ( ).

++ , . , , , ..

0

NULL, . , .

, ( ) Release () . , Release build optimizer , . , , 0, . , , . Release, Debug. , ++.

If you are already debugging a non-optimized build, make sure you have a clean rebuild before spending too much time debugging damaged images. Debug builds are usually connected incrementally, and, as you know, incremental linker creates such problems. If you are using a Debug build with a clean build and still can't figure out what went wrong, put a stack dump and more code. I am sure that we can help you figure it out.

0
source

Source: https://habr.com/ru/post/1732411/


All Articles