C++ calls the wrong function
I have this weird bug, where C++ calls the wrong function:
So this bit of code get called:
class FmeGrid
{
// ....
virtual void saveGridParameters() const;
virtual void setCellSignalValue(int row, int col, double double_value, const std::string& string_value);
// ....
}
void EnfClientFrame::saveGridParameters()
{
this->grid->saveGridParameters();
}
And the next function in the stack that is called is:
void FmeGrid::setCellSignalValue(int row, int col, double double_value, const std::string& string_value)
{
this->setCellString(row, col, string_value, wxALIGN_RIGHT);
this->setCellBackground(row, col, GetSignalColour(double_value));
}
With totally random values, here is t开发者_运维问答he stack:
enf_client.exe!ui::FmeGrid::setCellSignalValue(int row=1239452, int col=1239236, double double_value=-9.2559592117431994e+061, const std::basic_string<char,std::char_traits<char>,std::allocator<char> > & string_value={...}) Line 468 + 0x23 bytes C++
enf_client.exe!ui::EnfClientFrame::saveGridParameters() Line 170 + 0x20 bytes C++
So the "grid" pointer points to a class that inherits from FmeGrid (and only from FmeGrid). saveGridParameters is a virtual function, so it may be because of that.
Most common causes for this are:
- Dirty build (i.e. interface changed, but objects that used those interfaces weren't rebuilt)
- Stack corruption (you overwrote something on the stack that's causing the wrong function to be called and/or the correct function with invalid arguments)
Try fixing with a clean rebuild and if it still happens, then try a memory debugging tool (like Valgrind) to see where you're overwriting the stack.
To add to @brandx answer:
- this->grid->saveGridParameters() has been inlined by a compiler and calls directly or indirectly (via other inlined functions) ui::FmeGrid::setCellSignalValue.
精彩评论