when to call auto_ptr release?

G

George3

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
 
C

Cholo Lennon

George3 said:
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

Again with multiposting... It seems that you can understand the wide range
topic answers that people give you, but you are unable to understand a simple
concept.. multiposting.. The question is: Are you testing our patience? FYI,
http://www.blakjak.demon.co.uk/mul_crss.htm
 

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

Top