C++ assertion error while deleting object

前端 未结 3 439
伪装坚强ぢ
伪装坚强ぢ 2020-12-22 06:28

I have strange assertion error and I can not find what is wrong with this code.

Assertion expression is _BLOCK_TYPE_IS_VALID(pHead->nBlockUse).

I simplified

相关标签:
3条回答
  • 2020-12-22 06:43

    Problem is that you try to delete A object via MyObject pointer and MyObject destructor is not virtual. You could make MyObject's destructor virtual and then you can delete subclasses objects via pointer to MyObject. For more details on this issue see this question

    0 讨论(0)
  • 2020-12-22 06:45

    I think the issue is with multiple inheritance. Here's a simplified way to reproduce the problem. It can be fixed by

    • casting it to the most derived type OR
    • having the destructor of the base class be virtual.

    In your case, the virtual function approach is best as it is recommended to have base class destructor(s) to be virtual to get the destruction calls through the inheritance hierarchy.

    class A 
    {
    };
    
    class B
    {
    };
    
    class C : public A, public B
    {
    };
    
    int main()
    {
        // Fails with memory heap error
        B* pB = new C();
        delete pB;
    }
    

    To fix it

    int main()
    {
        B* pB = new C();
        // Casting it to the "full" type will fix it
        C* pC = static_cast<C*>(pB);
        delete pC;
    }
    

    The second program works because it is similar to this below.

    int main()
    {
        // Pointer to the "full" type works
        C* pC = new C();
        delete pC;
    }
    
    0 讨论(0)
  • 2020-12-22 06:53

    The issue is that your MyObject class lacks a virtual destructor, and you're attempting to call delete on a pointer to the derived class using a pointer to the base class MyObject. Issuing a delete on a derived object through a base class pointer is undefined behavior if the base class destructor is not virtual.

    5.3.5 Delete (Paragraph 3)

    In the first alternative (delete object), if the static type of the operand is different from its dynamic type, the static type shall be a base class of the operand’s dynamic type and the static type shall have a virtual destructor or the behavior is undefined.

    Once the destructor is made virtual in the base class MyClass, the following works correctly in Visual Studio 2013:

    #include <list>
    struct MyObject 
    {
        virtual ~MyObject() {}
    };
    
    class Creator
    {
    public:
        virtual ~Creator()
        {
            for (MyObject* item : _list)
            {
                delete item; 
                item = 0;
            }
            _list.clear();
        }
    
        template <class T>
        T& create()
        {
            T * item = new T();
            _list.push_back(item);
            return *item;
        }
    
    private:
        std::list<MyObject*> _list;
    };
    
    class A : public MyObject, public Creator
    {
    };
    
    class B : public MyObject, public Creator
    {
    };
    
    int main()
    {
        A a;
        a.create<A>();
    } 
    
    0 讨论(0)
提交回复
热议问题