Program steps into wrong function [closed]
I have a child class with a bunch of functions that implement pure virtual functions in its parent class.
When debugging, everything works the way it should except for one function.
When I try to step into functionA() the execution instead enters functionB(), and the call stack says I'm actually in functionC().
functionA() works everywhere else I inherit from this parent class.
Chang开发者_Go百科ing the function name does nothing, and cleaning / rebuilding does nothing. I got it to work by adding a dummy parameter to the function, but I would rather a real solution.
I ran into something similar to this some months back.
It turned out that the author was doing some fiendishly clever things during the initialization, and the compiler hadn't set up the derived class vtable yet when the method call occurred.
It was a bear to find it.
You're likely going to have to take a very careful look at the vtable pointers, and the generated assembly language, to figure out exactly where you are in the initialization process.
Alternatively, the vtable pointer in your object may have been overwritten by a wild copy from another object.
It sounds like you have stack corruption, which may be confusing the debugger, corrupting your program, or both.
Check for anything that writes too far in an array, writes unsafely to a pointer, writes to unallocated memory, etc. Various tools (such as ValGrind) may help.
Because you believe you are calling A
, but end up in B
, while the debugger says you're in C
, you need some HARD evidence about which function is actually getting called. Because we suspect the debugger may be confused, it does not count as a reliable source. I recommend using some logging facility (printf
will do just fine) to demonstrate clearly which function is actually getting called.
Because you say that functionA
works when called everywhere else, I'd first suspect the function that calls functionA
before things go sideways. Can that function (I'll term it functionZ
) call other similar functions reliably? or do they all indicate stack corruption? Debug functionZ
(and Y
and X
above it) carefully looking for the problems I described.
精彩评论