From 55704b4e65b70a943926fc050a3425dc418dd291 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89milie=20Feral?= Date: Tue, 19 Feb 2019 14:00:58 +0100 Subject: [PATCH] [poincare] TreeHandle: the constructor from a identifier retains the node if it has one --- poincare/include/poincare/expression.h | 2 +- poincare/include/poincare/tree_handle.h | 6 +++++- poincare/src/integer.cpp | 4 +--- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/poincare/include/poincare/expression.h b/poincare/include/poincare/expression.h index f87d66aa1..b250a00cb 100644 --- a/poincare/include/poincare/expression.h +++ b/poincare/include/poincare/expression.h @@ -248,6 +248,7 @@ public: protected: static bool SimplificationHasBeenInterrupted(); Expression(const ExpressionNode * n) : TreeHandle(n) {} + Expression(int nodeIdentifier) : TreeHandle(nodeIdentifier) {} template static Expression UntypedBuilderOneChild(Expression children) { assert(children.type() == ExpressionNode::Type::Matrix); @@ -284,7 +285,6 @@ protected: } /* Hierarchy */ - Expression(int nodeIdentifier) : TreeHandle(nodeIdentifier) {} Expression parent() const; // TODO try to inline void defaultSetChildrenInPlace(Expression other); void addChildAtIndexInPlace(TreeHandle t, int index, int currentNumberOfChildren) = delete; diff --git a/poincare/include/poincare/tree_handle.h b/poincare/include/poincare/tree_handle.h index abd3008d6..881d95206 100644 --- a/poincare/include/poincare/tree_handle.h +++ b/poincare/include/poincare/tree_handle.h @@ -103,7 +103,11 @@ public: protected: /* Constructor */ TreeHandle(const TreeNode * node); - TreeHandle(int nodeIndentifier = TreeNode::NoNodeIdentifier) : m_identifier(nodeIndentifier) {} + TreeHandle(int nodeIndentifier = TreeNode::NoNodeIdentifier) : m_identifier(nodeIndentifier) { + if (hasNode(nodeIndentifier)) { + node()->retain(); + } + } // WARNING: if the children table is the result of a cast, the object downcasted has to be the same size as a TreeHandle. static TreeHandle BuildWithBasicChildren(TreeNode * node, TreeHandle * children = nullptr, int numberOfChildren = 0); diff --git a/poincare/src/integer.cpp b/poincare/src/integer.cpp index 675bf0b44..67fd3e295 100644 --- a/poincare/src/integer.cpp +++ b/poincare/src/integer.cpp @@ -107,9 +107,7 @@ Integer::Integer(native_uint_t * digits, uint16_t numberOfDigits, bool negative) TreeHandle h = TreeHandle::BuildWithBasicChildren(node); /* Integer is a TreeHandle that keeps an extra integer. We cannot just cast * the TreeHandle in Integer, we have to build a new Integer. To do so, we - * pilfer the TreeHandle identifier. We thus have to increment the node - * reference counter to prevent its destruction when we leave this scope. */ - node->retain(); + * pilfer the TreeHandle identifier. */ new (this) Integer(h.identifier(), negative); }