We know that it is needed to specify the destructor of a base class as virtual if you intend to use it polymorphically, otherwise you might have a ressource leak in your program, since only the base class destructor will be called and not the derived object destructor.
We also know that constructors / destructors are purely initialization / uninitialization constructs, and operator new / operator delete handle the allocation / unallocation of memory.
In that case, why exactly does the lack of destructor-calling incur a leak, in the case where my C++ class contains only primitive data members? Would it be more accurate to say that the operator delete cannot unallocate the memory, and that is what creates a resource leak?
EDIT: Adding an example, to clarify my question. In the following case, the derived destructor never gets called. Does it mean that the memory for derivedInt never gets unallocated? In that case, is it because the destructor cannot be called, or because the operator delete cannot unallocate the memory for the int?
class Base
{
int baseInt;
public:
Base(){};
~Base(){};
};
class Derived : public Base
{
int derivedInt;
public:
Derived(){};
~Derived(){};
};
int main(int argc, const char * argv[]) {
Base *pb = new Derived();
delete pb;
return 0;
}
Deleting an object through pointer to base invokes undefined behavior unless the destructor in the base class is virtual. Source
This means anything can happen - including a memory leak.