How to delete an object of a polymorphic class type that has no virtual destructor

后端 未结 6 1174
清酒与你
清酒与你 2021-02-07 09:37

I am getting the following error when I try to compile some code from a Third-party SDK.

*Description    Resource    Path    Location    Type
deleting object of          


        
6条回答
  •  醉话见心
    2021-02-07 10:25

    This warning is produced when the base class has virtual member functions but no virtual dtor. This is a bug. If you don't have the code, then there's nothing you can do other than making sure you're manually de-allocating any resources in your subclass. Like in a custom cleanup() member function that you make sure to call manually before deleting the object.

    Another option is to static_cast it to the correct class. Note that dynamic_cast (which incurs runtime overhead and requires RTTI) is not needed. The compiler can derive the type relationship just fine at compile time in this case.

    Of course, if the object is deleted somewhere else that's not part of you code, then you're out of luck. In that case, make sure your subclass doesn't allocate anything. That way it's not possible to leak even when the destructor isn't being called.

提交回复
热议问题