D
dragoncoder
Hi all,
I am trying to understanding std::auto_ptr<T> class implementation from
"The C++ standard library" by Nicolai Josuttis. He gives a sample
implementation of auto_ptr class template in section 4.2. The copy
constructor is defined as:
auto_ptr (auto_ptr& rhs) throw()
: ap (rhs. release()) {
}
I was wondering, is there not a leak here ? I mean before taking
ownership of the memory pointed to by rhs, shouldn't the memory pointed
to by ap be relased (freed) ? I am expecting the copy constructor to be
this way:
auto_ptr(auto_ptr& rhs) throw() {
delete ap; // Isn't this required ?
ap = rhs.release();
}
If the delete ap is removed from the definition, I think the memory
which is already being pointed to by ap (before the copy constructor is
called) will leak. Please comment.
Thanks in advance.
/P
I am trying to understanding std::auto_ptr<T> class implementation from
"The C++ standard library" by Nicolai Josuttis. He gives a sample
implementation of auto_ptr class template in section 4.2. The copy
constructor is defined as:
auto_ptr (auto_ptr& rhs) throw()
: ap (rhs. release()) {
}
I was wondering, is there not a leak here ? I mean before taking
ownership of the memory pointed to by rhs, shouldn't the memory pointed
to by ap be relased (freed) ? I am expecting the copy constructor to be
this way:
auto_ptr(auto_ptr& rhs) throw() {
delete ap; // Isn't this required ?
ap = rhs.release();
}
If the delete ap is removed from the definition, I think the memory
which is already being pointed to by ap (before the copy constructor is
called) will leak. Please comment.
Thanks in advance.
/P