Virtual methods of chaos, how can I find the reason for this?

My colleague had a problem with some C ++ code. He debugged the strange behavior of the virtual method of an object. Whenever the method being executed (when debugging Visual Studio 2005), everything went wrong, and the debugger will not go into this method, but into the object's destructor! In addition, the virtual table of the object only indicated its destructor, no other methods.

I hadn’t seen this before, and a run-time error was printed talking about case ESP. I would like to give you the correct error message, but I do not remember it correctly.

Anyway, have any of you ever come across? What can cause this behavior? How will this be fixed? We tried many times to rebuild the project, restarted the IDE, nothing helped. We also used the function _CrtCheckMemorybefore calling this method to make sure the memory is in good condition and it returned true(which means “OK”). I have no more ideas. You?

+3
source share
5 answers

I have seen this before. Typically, this is due to the fact that I use the class from the generated Release.LIB file when I'm in Debug mode. Someone probably saw a better example, and I will answer their answer.

+2
source

, C-style casts, static_cast<>? , , , :

class Base1 {};
class Base2 {};
class Derived : public Base1, public Base2 {};

Derived *d = new Derived;
Base2* b2_1 = (Base2*)d; // wrong!
Base2* b2_2 = static_cast<Base2*>(d); // correct
assert( b2_1 == b2_2 ); // assertion may fail, because b2_1 != b2_2

: , (, , , , ).

: . , . CrtCheckMemory , (, ).

+1

- , undefined. , , , MFC , , , , (, , , ). , , , .

+1

ESP .

, , , .

, DLL-? , dll , LoadLibrary .

, , , , , , . , - vptr vtables , , .

" ", , , , , .

Visual Studio , . , Visual Studios .

. , .

+1

-fest , :

, _CrtCheckMemory . :

  • Calling conditional inconsistencies already mentioned (I don’t know how to pass a callback to the wrong convention about calling the WinAPI function, which static or dynamic libraries are you linking to?)
  • string like printf("%d");
+1
source

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


All Articles