Joe Seigh schreef:
The dtor for newPT is deleting the input object which is still
being used apparently by the caller. You should only use
auto_ptr for objects created in the same scope as the auto_ptr.
There is no reason for that. In fact, a common use for auto_ptr<>
is to implement source and sink interfaces. A 'source' is a function
returning a std::auto_ptr<T>. If a caller doesn't save the return
value, the T object returned is deleted immediately. However, the
caller can copy the returned auto_ptr in its own auto_ptr. That
transfers ownership to another auto_ptr, so we again know the T will
be deleted.
Sinks work the other way around. A function that has a std::auto_ptr<T>
interface will take care of destroying that T, automatically.
Of course, calling sink(source()); is legal and sensible. It transfers
ownership from source() to sink() without difficult code or risks.
Even exceptions can't interfere. It's really hard to "lose" objects
that
aren't on the heap.
HTH,
Michiel Salters