8 Comments
User's avatar
Atulya Sharma's avatar

But why would one want to exit without invoking destructors? No Security issue here?

Expand full comment
Šimon Tóth's avatar

The prototypical example is when invariants are violated. In that case, running destructors might be harmful (e.g. writing corrupted data).

Expand full comment
Atulya Sharma's avatar

So basically the data left out is anyway useless to anyone and doesn't matter even if it stays in memory?

Expand full comment
Šimon Tóth's avatar

Well, it doesn't stay in memory. When a process finishes, its resources are cleaned up by the operating system.

Expand full comment
Atulya Sharma's avatar

But the memory is not overwritten right? OS just does the metadata handling for the pages no longer in use. So it could be a security breach? Am I missing something?

Expand full comment
Šimon Tóth's avatar

If you hold allocated memory and then free that memory in a destructor, it's the same operation as what the OS will do when the process exits.

Expand full comment
Atulya Sharma's avatar

Got it thanks. Conversely, is there a problem if all program exits are done without calling any destructors?

Expand full comment
Šimon Tóth's avatar

Yes, most I/O is buffered, meaning that when you write something, it might only be written to a buffer not to the device. If you exit without flushing, that data will be lost.

Expand full comment