c++ - Is a destructor called when an object goes out of scope? -


for example:

int main() {     foo *leedle = new foo();      return 0; }  class foo { private:     somepointer* bar;  public:     foo();     ~foo(); };  foo::~foo() {     delete bar; } 

would destructor implicitly called compiler or there memory leak?

i'm new dynamic memory, if isn't usable test case, i'm sorry.

yes, automatic variables destroyed @ end of enclosing code block. keep reading.

your question title asks if destructor called when variable goes out of scope. presumably meant ask was:

will foo's destructor called @ end of main()?

given code provided, answer question no since foo object has dynamic storage duration, shall see shortly.

note here automatic variable is:

foo* leedle = new foo(); 

here, leedle automatic variable destroyed. leedle pointer. thing leedle points not have automatic storage duration, , not destroyed. so, if this:

void doit() {   foo* leedle = new leedle; } 

you leak memory allocated new leedle.


you must delete has been allocated new:

void doit() {   foo* leedle = new leedle;   delete leedle; } 

this made simpler , more robust using smart pointers. in c++03:

void doit() {   std::auto_ptr <foo> leedle (new foo); } 

or in c++11:

void doit() {   std::unique_ptr <foo> leedle = std::make_unique <foo> (); } 

smart pointers used automatic variables, above, , when go out of scope , destroyed, automatically (in destructor) delete object being pointed to. in both cases above, there no memory leak.


let's try clear bit of language here. in c++, variables have storage duration. in c++03, there 3 storage durations:

1: automatic: variable automatic storage duration destroyed @ end of enclosing code block.

consider:

void foo() {   bool b = true;   {     int n = 42;   } // line 1   double d = 3.14; } // line 2 

in example, variables have automatic storage duration. both b , d destroyed @ line 2. n destroyed @ line 1.

2: static: variable static storage duration allocated before program begins, , destroyed when program ends.

3: dynamic: variable dynamic storage duration allocated when allocate using dynamic memory allocation functions (eg, new) , destroyed when destroy using dynamic memory allocation functions (eg, delete).

in original example above:

void doit() {   foo* leedle = new leedle; } 

leedle variable automatic storage duration , destroyed @ end brace. thing leedle points has dynamic storage duration , not destroyed in code above. must call delete deallocate it.

c++11 adds fourth storage duration:

4: thread: variables thread storage duration allocated when thread begins , deallocated when thread ends.


Comments

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -

java - Digest auth with Spring Security using javaconfig -