From dffb4439ad438c76002d6b7798e5e3b651a2ac19 Mon Sep 17 00:00:00 2001 From: Christian Bauer Date: Fri, 20 Aug 2004 20:13:09 +0000 Subject: [PATCH] cache other.p in assignment; this should fix the "assignment of sub-expression" problem with operator[] --- ginac/ptr.h | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/ginac/ptr.h b/ginac/ptr.h index 7f305fe1..4c4ea71d 100644 --- a/ginac/ptr.h +++ b/ginac/ptr.h @@ -80,11 +80,14 @@ public: 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; - p = other.p; + p = otherp; return *this; } -- 2.44.0