From 6af638059b26303a8988cf58809dc03cad2de161ab66ee1b7ecfb1addd2c0270 Mon Sep 17 00:00:00 2001 From: ADAM David Alan Martin Date: Wed, 23 Apr 2025 20:24:50 -0400 Subject: [PATCH] A bit closer to RAII on the doctree. It should be an external vector of ownership... The recursion through the stack for deletion has the potential to be a serious problem. --- src/doctree.hh | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/doctree.hh b/src/doctree.hh index d463255..fac5ea4 100644 --- a/src/doctree.hh +++ b/src/doctree.hh @@ -29,9 +29,9 @@ class DoctreeNode { ~DoctreeNode () { dFree ((void*) id); while (lastChild) { - DoctreeNode *n = lastChild; - lastChild = lastChild->sibling; - delete n; + std::unique_ptr< DoctreeNode > deleter; + deleter.reset( lastChild ); // It's a step toward RAII + lastChild = deleter->sibling; } } }; @@ -57,13 +57,13 @@ class Doctree { ~Doctree ()= default; DoctreeNode *push () { - DoctreeNode *dn = new DoctreeNode (); + auto dn= std::make_unique< DoctreeNode >(); dn->parent = topNode; dn->sibling = dn->parent->lastChild; - dn->parent->lastChild = dn; dn->num = num++; - topNode = dn; - return dn; + dn->parent->lastChild = dn.get(); + topNode = dn.get(); + return dn.release(); }; void pop () {