In the following code, when ptr is deleted, the destructor for Base is called, but not the destructor for Derived (due to destructor of Base not being virtual).
class Base { int b; }; class Derived : public Base { int d; }; int main(void) { Base * ptr = new Derived(); delete ptr; return 0; } Valgrind reports that the program contains no memory leaks, which i guess is true in the sense that all newed data is deleted in this particular case. My question is - given that the (default) destructor of Derived is not called, when and how is the memory for d deallocated or reclaimed?
delete ptr, it's just that destructor is not invoked, which could be a potential undefined behavior.MemoryLoggersaysallocated 8 bytes at 0x3b2380; deallocated 4 bytes at 0x3b2380; Error: 4 bytes still allocated!. UB. Potentially this could also sayHave a nice day sir.Derivedmight have been responsible for some further resources that would thus not be released.