1. Strictly speaking, both are incorrect. Destructors need to be explicitly written when the class owns resources that not managed elsewhere and need to be released explicitly. Those resources may be memory, but they could also be file handles, network connections, concurrency locks, etc.
2. The compiler-generated destructor calls destructors for all members of the class. It's important to note that whether a destructor on a member is called depends on the
type of the member, and not
how the object in question was allocated. For example,
1 2 3 4
|
class A{
int *foo;
A(): foo(new int){}
};
|
Here, the compiler will not generate a destructor that does
delete this->foo;
. The compiler cannot keep track of raw pointer ownership for you. For example, it might be that you actually intended for another piece of code to release the pointer. If the compiler generated a delete in the destructor, that would break your code. If you use raw pointers you need to manage them yourself.
Don't use raw pointers, by the way.
3. The operating system is capable of reclaiming all memory from a process when that process terminates. This is something dangerous to rely on, however. For long-lived processes, memory leaks can be disastrous.