zoukankan      html  css  js  c++  java
  • C++程序在debug模式下遇到Run-Time Check Failure #0

    今天遇到一个Access Violation的crash,只看crash call stack没有找到更多的线索,于是在debug模式下又跑了一遍,遇到了如下的一个debug的错误提示框:

    runtimecheckfailureESP

    这个是什么原因呢?我们来看一个简单的例子来重现这个错误。

    假设我们有2个父类,分别是BaseA和BaseB。有一个子类Child,继承自BaseA和BaseB。

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    class BaseA
    {
    public:
        virtual void foo()=0;
    };
    
    class BaseB
    {
    public:
        virtual void bar(int a)=0;
    };
    
    class Child: public BaseA, public BaseB
    {
    public:
        void foo()
        {
            cout<<"i'm foo in Child!"<<endl;
        };
        void bar(int a)
        {
            cout<<"i'm bar in Child!"<<endl;
        };
    };

    假设我们有如下的main函数:

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    int main() {
    
        BaseB* b = new Child();
        BaseA* a = (BaseA*)b;
        BaseA* a2 = dynamic_cast<BaseA*> (b);
        // This is actually calling bar(),
        // and will cause Runtime check failure about ESP if the foo and bar have different signature.
        a->foo(); 
        a2->foo();
    }

    在这个main函数里a是通过C-Style的强转转来的,a2是通过dynamic_cast转换来的。如果我们运行这个程序,在release下就会报access violation的crash,在debug下就会出上面列的ESP错误的对话框。

    函数的输出如下图所示:

    espoutput

    这就能解释为什么会出这个ESP的错误了,因为我们想调用foo,但是实际上调用的bar,但是foo和bar的函数签名不一样,所以导致了出现那个debug版本下的ESP的错误提示。但是加入我们把bar(int a)中的参数去掉,这样foo和bar就有一样的函数签名了,这样及时在debug版本下也不会出这个ESP的提示,在release或者debug模式下都不会报任何错误,但是我们在runtime调用了错误的函数!!!

    可以看看下图回忆一下vtable是怎么回事:)

    image

    也可以参考怎么看C++对象的内存结构 和 怎么解密C++的name mangling - fresky - 博客园来看看C++对象的内存结构。

    对于ESP的错误,可以参考stackoverflow上的这个问题:c++ - Run-Time Check Failure #0 - The value of ESP was not properly saved across a function call - Stack Overflow

  • 相关阅读:
    bzoj 1054
    bzoj 1047
    bzoj 2761
    bzoj 1191
    bzoj 2748
    bzoj_1003 物流运输
    新的开始( [USACO08OCT]打井Watering Hole)
    map
    Generic Cow Protests-G——60分做法
    逆序对
  • 原文地址:https://www.cnblogs.com/fresky/p/3314588.html
Copyright © 2011-2022 走看看