This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

auto_ptr, new warning? (was: New STL implementation from SGI)


| > Now, for classes that previously did not exist (like auto_ptr) it would
| > be risk-free to add them from the new STL -- it cannot break any old
| > code.  If you only want auto_ptr, then we could do that.

A note on auto_ptr... It's an unsafe class imho.
The problem I have with it is that when it accidently is
used for a temporary, then the ownership is lost (with
memory leak as result, or worse).

Would it be possible to build into the compile a warning,
that warns when an auto_ptr might be used as a temporary?

-Wauto_ptr

-- 
 Carlo Wood  <carlo@runaway.xs4all.nl>

==========
PS I wrote my own auto_ptr and use that :/.  It contains
   a new boolean `frozen' and two new methods
   `freeze()' and `unfreeze()'.  While frozen, the auto_ptr
   does not pass on ownership when passed on as argument in
   the copy constructor.  I then call `freeze()' before the
   temporary is created.  The explicit use of operator=()
   always copies ownership, also when `frozen'.


// $Header: /usr/src/CVS/libr/include/libr/auto_ptr.h,v 1.1.1.1 1998/06/17 16:02:19 carlo Exp $
//
// Author:
//
// 1024/624ACAD5 1997/01/26 Carlo Wood, Run on IRC <carlo@runaway.xs4all.nl>
// Key fingerprint = 32 EC A7 B6 AC DB 65 A6  F6 F6 55 DD 1C DC FF 61
// Get key from pgp-public-keys server or
// finger carlo@runaway.xs4all.nl for public key (dialin, try at 21-22h GMT).
//
// This implementation of `auto_ptr' was originally posted to comp.std.c++
// on March 30, 1996 by Greg Colvin.
//
// The template member functions have been removed because they are not
// supported by most compilers, yet.
//
// I added the boolean `frozen' and the methods `freeze', `unfreeze'
// and `is_frozen' to allow to freeze ownership.
//

#ifndef SUPPORT_AUTO_PTR_H
#ifdef __GNUG__
#pragma interface
#endif
#define SUPPORT_AUTO_PTR_H

RCSTAG_H(support_auto_ptr, "$Id: auto_ptr.h,v 1.1.1.1 1998/06/17 16:02:19 carlo Exp $")

template<class X>
class auto_ptr {
  mutable bool owner;
  bool frozen;
  X* px;
// template<class Y> friend class auto_ptr;
public:
  explicit auto_ptr(X* p=0) : owner(p), frozen(false), px(p) {}
// template<class Y>
    auto_ptr(const auto_ptr<X /*Y*/ >& r) 
    : owner(r.owner && !r.frozen), frozen(false), px(r.frozen ? r.get() : r.release()) {}
// template<class Y>
    auto_ptr& operator=(const auto_ptr<X /*Y*/ >& r) {
     if ((void*)&r != (void*)this)
     {
       if (owner) 
	 delete px;
       owner = r.owner; 
       px = r.release();
     }
     return *this;
   }
   ~auto_ptr() { if (owner) delete px; }
   void reset()	{ bool own = owner; owner = 0; if (own) delete px; px = NULL; }
   bool freeze() { frozen = true; return owner; }
   void unfreeze() { frozen = false; }
   X& operator*()   const { return *px; }
   X* operator->()  const { return px; }
   X* get()         const { return px; }
   X* release()     const { owner = 0; return px; }
   bool strict_owner() const { return frozen; }
#ifdef DEBUG
   bool is_owner(void) const { return owner; }
#endif
};

#endif // SUPPORT_AUTO_PTR_H


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]