0

I have several places in my program where exit(0) is called. Will the destructors for my unique_ptr's be called, and deallocate the data on the heap before the program terminates?

JensB
  • 839
  • 4
  • 19
  • Did you consider figuring this out yourself by creating a class that prints a message in its destructor, constructing it and making the pointer a `unique_ptr`, and then seeing what happens when you `exit`? Did you have any difficulties doing this experiment? – Sam Varshavchik Aug 06 '20 at 11:07
  • Depends on the scope of the unique_ptr. If it is a globally constructed or static constructed variable, the destructor will be call through the `atexit` mechanism. If the variable is local or a member variable of an object or a dependent member of such, then it will be leaked (but the memory is reclaimed by the operating system on termination). – Eljay Aug 06 '20 at 11:20

0 Answers0