cache other.p in assignment; this should fix the "assignment of
authorChristian Bauer <Christian.Bauer@uni-mainz.de>
Fri, 20 Aug 2004 20:13:09 +0000 (20:13 +0000)
committerChristian Bauer <Christian.Bauer@uni-mainz.de>
Fri, 20 Aug 2004 20:13:09 +0000 (20:13 +0000)
sub-expression" problem with operator[]

ginac/ptr.h

index 7f305fe1bcc41af012ef84e90380e90006632c20..4c4ea71d6bbfde7d70a1f0e563f29d9ef3c1d164 100644 (file)
@@ -80,11 +80,14 @@ public:
 
        ptr &operator=(const ptr & other)
        {
 
        ptr &operator=(const ptr & other)
        {
-               // NB: must first add reference to "other", since other might be *this.
-               other.p->add_reference();
+               // NB1: Must first add reference to "other", since other might be *this.
+               // NB2: Cache other.p, because if "other" is a subexpression of p,
+               //      deleting p will also invalidate "other".
+               T *otherp = other.p;
+               otherp->add_reference();
                if (p->remove_reference() == 0)
                        delete p;
                if (p->remove_reference() == 0)
                        delete p;
-               p = other.p;
+               p = otherp;
                return *this;
        }
 
                return *this;
        }