tags:

views:

52

answers:

3

Hi, I need to run an application in an embedded system continuously. While implementing this application, i need to allocate lot of memory from Heap. If i use auto_ptr for those, when will that be freed ? As per my understanding, memory pointed by auto pointers would be freed upon exiting/terminating the application. It would not be freed automatically if application is not exited. I can use reset() to free the memory like below.

auto_ptr<Test> testPtr(new Test);
testPtr.reset();

But then, the purpose of auto_ptr would be defeated if i do this. If i can do reset() at all places, i can do delete also at all the places.

Pls let me know whether memory pointed by auto_ptr would get freed even though application is not exited. If not, is there any otherway to freeup the memory, other than calling reset() ?

EDIT: In my case, auto_ptr is inside a class. That class object will be destroyed only upon exiting the application. As the application runs infinitely, class object will not be destroyed. Hence the issue. Pls consider this also.

EDIT: Answer: As i am using same auto_ptr for storing other objects, everytime i do assignment, auto_ptr assignment operator would take care of deleting previous object. So no leak even without call to reset.

+3  A: 

Memory pointed by auto_ptr is released when auto_ptr instance is destroyed. For local auto_ptr instance, defined in a function, this happens when function exits. If auto_ptr instance is class member, it is destroyed when container class instance is destroyed. Global auto_ptr instance is destroyed when the program exits.

Alex Farber
@Alex: In my case, auto_ptr is inside a class. That class object will be destroyed only upon exiting the application. As the application runs infinitely, class object will not be destroyed. Hence the issue.
bjskishore123
@bj: What's the problem then? The class needs the resource until the program ends. Nothing to change.
GMan
@GMan: I want to point the auto_ptr to a new object each time some external event reaches my application. So i think, i need to do reset() and use the pointer again.
bjskishore123
Yes, this is correct.
Alex Farber
@bjskishore: It sounds like you want to use the `auto_ptr` assignment operator to achieve this. It will cause the previous pointer to be deleted and replaced with the new pointer.
Henk
@GMan, @Alex: I tested just now. Yes, it works that way. So no leak even without call to reset().
bjskishore123
+4  A: 

auto_ptr gives you two things:

  1. assignment transfers ownership
  2. when an owning auto_ptr is destroyed, so is the data it owns

The application exiting only becomes relevant if/when you have global auto_ptr objects -- which rarely makes sense in the first place. A global auto_ptr referring to a dynamically allocated object can usually be replaced by by making the object itself global (but you generally want to avoid both).

For most normal uses of auto_ptr, exiting the application is irrelevant -- it's exiting the scope at which the auto_ptr is defined that matters. At that point, the object it owns gets deleted.

Jerry Coffin
A: 

The destructor for the auto_ptr class will invoke the delete operator on the pointer you specified when calling the constructor i.e. new Test. Invoking auto_ptr::reset() has the same effect.

The benefit is that you can use scope rules to automate the destruction of the object being pointed to by an auto_ptr instance.

In your example testPtr is being instantiated on the stack and will be destroyed once it is out of scope.

Consider this example:

void foo()
{
  //... do some work

  {
    auto_ptr<Test> testPtr(new Test);

    //... do something with testPtr

  } // *at this point testPtr is no longer in scope and the pointer held by this object
    // will be deleted

  //... do some additional work
}

Now obviously you could have not bothered using scope (i.e. {}) and just called auto_ptr::reset() where I marked * but this method has the benefit of working in the presence of exceptions and return statements and so on.

Henk