diff --git a/src/Lib/Script/Ast/IrElement.cc b/src/Lib/Script/Ast/IrElement.cc index 5dd4c02578f4576a490dd47e674571c82ea887af..ecb036ae755d4386a5d81a3d46a333632e32a93a 100644 --- a/src/Lib/Script/Ast/IrElement.cc +++ b/src/Lib/Script/Ast/IrElement.cc @@ -7,10 +7,21 @@ namespace Vivy::Script { IrElement::IrElement(IrElement *p) noexcept { + /* + ** Need to trick the setParent or we might dereference a nullptr... Can't + ** think of a cleaner way for now. + */ + + parentRootIsNotInitialized = false; setParent(p); - parentRoot = parent()->irRoot(); - if (parentRoot == nullptr) - parentRoot = dynamic_cast<IrRoot *>(this); + parentRootIsNotInitialized = true; + + if (parent() != nullptr) { + parentRoot = parent()->irRoot(); + parentRootIsNotInitialized = false; + if (parentRoot == nullptr) + parentRoot = dynamic_cast<IrRoot *>(this); + } } IrRoot * @@ -90,6 +101,8 @@ IrElement::setParent(IrElement *p) noexcept parentElement = p; if (parentElement != nullptr) parentElement->addChild(this); + if (parentRootIsNotInitialized) + parentRoot = parent()->irRoot(); } void diff --git a/src/Lib/Script/Ast/IrElement.hh b/src/Lib/Script/Ast/IrElement.hh index 77d353c234fc09b1f875e500dd2d754f267ebcbd..a496188dae01c6dc1af7574ab40227189e6365bd 100644 --- a/src/Lib/Script/Ast/IrElement.hh +++ b/src/Lib/Script/Ast/IrElement.hh @@ -106,9 +106,10 @@ concept IrElementParsable = std::is_base_of<IrElement, T>::value && requires(T i /* Base class definition: IrElement */ class IrElement { - IrElement *parentElement = nullptr; - IrAttribute *elementAttributes = nullptr; - IrRoot *parentRoot = nullptr; + IrElement *parentElement = nullptr; + IrAttribute *elementAttributes = nullptr; + IrRoot *parentRoot = nullptr; + bool parentRootIsNotInitialized = true; std::vector<IrElement *> childElements; protected: