zoukankan      html  css  js  c++  java
  • C++ CoreDump.md

    C++ CoreDump的情况记录:

    1. 内存操作错误或失误,如对含容器的结构体使用memset、内存越界
    2. 栈溢出,一般发生在递归函数未收敛情况
    3. 线程访问了已经析构的对象,一般发生在多线程场景下
    4. windows场景下,std::mutex对象被持有线程lock()了两次()
      • 注:linux场景下,第二次只会被阻塞(std::thread对象存在时,lock()操作才有效)
    5. 继承关系下,变量发生同名隐藏,同时遇到多态,被访问的变量为空指针
      • 场景1:仅为子类成员分配了内存,但实际访问的父类同名成员
      • 场景2:仅为父类成员分配了内存,但实际访问的子类同名成员

    场景1举例:

    class base{
        int *m_sameVariable;
    public:
        base(int iNumber = 0)
            : m_sameVariable(nullptr){
        }
        virtual ~base(){
            delete m_sameVariable;
        }
    
        void publicFunc(){
            m_sameFunction();    //调此函数就为模拟工程场景,而增加的复杂度,实际跟直接*m_sameVariable没区别的
        }
        virtual void DynamicCall(){
            //do something
        }
        void m_sameFunction(int val = 10){
            *m_sameVariable = val;
        }
    };
    
    class child : public base{
        int *m_sameVariable;
    public:
        child(int iNumber = 0)
            : m_sameVariable(new int(iNumber)){
        }
        virtual ~child() override{
            delete m_sameVariable;
        }
        virtual void DynamicCall(){
            publicFunc();
        }
        void m_sameFunction(){
            *m_sameVariable = 10;
        }
    };
    
    int main(int argc, char *argv[])
    {
        base *pTest = new child(10);
        pTest->DynamicCall();
    
        return 0;
    }
    

    场景2举例:

    class base
    {
        int *m_sameVariable;
    public:
        void SetData(int iNumber = 0){
            m_sameVariable = new int(iNumber);
        }
        void publicFunc(){
            DynamicCall();
        }
        virtual void DynamicCall(){
            *m_sameVariable = 10;
        }
    };
    
    class child : public base
    {
        int *m_sameVariable;
    public:
        void SetData(int iNumber = 0){
        }
        virtual void DynamicCall(){
            *m_sameVariable = 10;
        }
    };
    
    int main(int argc, char *argv[])
    {
        base *pTest = new child;
        pTest->SetData(0);
        pTest->publicFunc();
        return 0;
    }
    

    实际工程中的代码比示例代码复杂得多,但核心也差不多是这样。这两种都是不熟悉名字查找规则与多态规则导致的。

    原创不易,转载请注明出处,谢谢
  • 相关阅读:
    2017python学习的第四天,生成器,装饰器迭代器
    2017python第三天作业
    2017python学习的第三天函数
    2017python学习的第三天文件的操作
    2017python第二天作业
    python第二天学了列表,字典和嵌套
    [Translation] Introduction to ASP.NET Core
    Create a private nuget server
    Cunningham's Law
    [转]Migrating HTTP handlers and modules to ASP.NET Core middleware
  • 原文地址:https://www.cnblogs.com/Keeping-Fit/p/14584053.html
Copyright © 2011-2022 走看看