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.
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.