lizarisk
lizarisk

Reputation: 7820

Is the destructor of the local variable always guaranteed to be called when it goes out of scope?

Compilers can make a lot of optimizations (like inlining some functions), and I'm slightly suspicious that not all memory allocated for local variables is cleared after the function call in my program (based on the system monitor of the OS X), so that's why I'm asking: is it guaranteed by the standard that all destructors of the local variables would be called exactly at the moment they go out of scope?

Upvotes: 7

Views: 5205

Answers (5)

Vaughn Cato
Vaughn Cato

Reputation: 64298

It will if you have code like this:

void f()
{
  A a;  // create a local instance of A
        // memory will be allocated on the stack, 
        // and the constructor for `a` will be called.

  // various code here

  // here at the end of the scope, 
  // the destructor for `a` will be called,
  // and the memory on the stack will be freed.
}

Upvotes: 1

bash.d
bash.d

Reputation: 13207

Yes, this will always occur. It is guaranteed by C++. From C++ FAQ lite:

[11.5] Should I explicitly call a destructor on a local variable?

No!

The destructor will get called again at the close } of the block in which the local was created. This is a guarantee of the language; it happens automagically; there's no way to stop it from happening. But you can get really bad results from calling a destructor on the same object a second time! Bang! You're dead!


It goes a little beyond your question, but the base is the same.

Upvotes: 1

nogard
nogard

Reputation: 9706

Yes, it's guaranteed.

Monitoring memory usage with system monitor might be not precise, because application does not return memory to the system. Once allocated it belongs to the application and even if your objects are destroyed you might not see any difference.

If you want to guarantee that your application does not have memory leaks you may want to use such tools as valgrind, or drMemory from google, or several others (google for "memory leak detection"). In this case you will have the most precise information about allocations, deallocations, leaks, memory access violations etc.

Upvotes: 7

Andy Prowl
Andy Prowl

Reputation: 126412

Yes. Per paragraph 3.7.3 of the C++11 Standard:

Block-scope variables explicitly declared register or not explicitly declared static or extern have automatic storage duration. The storage for these entities lasts until the block in which they are created exits.

Notice, however, that this concerns variables with automatic storage duration. If you created an object dynamically with new and assigned the result to a local raw pointer, then only the raw pointer will be destroyed, and not the pointed object:

{
    int* foo = new int(42);
} // Here you have a memory leak: the foo pointer is destroyed,
  // but not the object foo pointed to

Upvotes: 13

alexrider
alexrider

Reputation: 4463

Yes it is guaranteed that every variable that goes out of scope will get it's destructor called.

6.7 Declaration statement

2 Variables with automatic storage duration (3.7.3) are initialized each time their declaration-statement is executed. Variables with automatic storage duration declared in the block are destroyed on exit from the block (6.6).

Upvotes: 4

Related Questions