C++调用了错误的函数
我有一个奇怪的错误,C++ 调用了错误的函数:
所以这段代码被调用:
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();
}
堆栈中被调用的下一个函数是:
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));
}
对于完全随机的值,这里是堆栈:
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++
所以“网格”指针指向继承自 FmeGrid 的类(且仅继承自 FmeGrid)。 saveGridParameters 是一个虚函数,所以可能是因为这个原因。
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 the 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
最常见的原因是:
,如果仍然发生,则尝试内存调试工具(如 Valgrind)来查看覆盖堆栈的位置。
Most common causes for this are:
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.
添加到 @brandx 答案:
To add to @brandx answer: