when to call auto_ptr release?

Joined
Feb 12, 2008
Messages
108
Reaction score
0
Hello everyone,


Auto_ptr is convenient because of it saves our work and provides a framework to auto-management life cycle to reduce potential resource leak.

But why do we sometimes need to call release method on auto_ptr to go back to the style of manual management? Remember, when we call release, we need to delete the object instance manually later.

(here is a sample I modified from MSDN auto_ptr sample code)

Code:
// auto_ptr_release.cpp
// compile with: /EHsc
#include <memory>
#include <iostream>
#include <vector>
using namespace std;

class Int 
{
public:
   Int( int i ) 
   {
      x = i;
      cout << "Constructing " << ( void* )this << " Value: " << x << endl; 
   };
   ~Int( ) {
      cout << "Destructing " << ( void* )this << " Value: " << x << endl; 
   };

   int x;

};

int main( ) 
{
   auto_ptr<Int> pi ( new Int( 5 ) );
   Int* pi3 = pi.release ();
   delete pi3;
}


thanks in advance,
George
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top