Virtual destructors are needed when an object is (potentially) destructed from a base class pointer.
Consider a program without dynamic memory as often found in embedded systems. Here, using new
or delete
triggers a linker error, because the required underlying allocators are not implemented. Thus, the developers use only statically allocated objects (in bss/data section) or automatically allocated objects (usually on stack).
In such a system, is there any situation where a virtual destructor is truly needed? (Let's say nobody is bored and calls a destructor manually on some pointer.)
It seems to me that static and automatic allocation always invoke the correct destructor anyway. Do I miss anything? Are there any corner cases? What about static object pools in conjunction with unique_ptr and a custom deleter?