Second attemmpt at fixing critical bug in Node::unref()

This commit is contained in:
Sam Vervaeck 2023-05-31 13:14:46 +02:00
parent 717a2a663a
commit b64b18216a
Signed by: samvv
SSH key fingerprint: SHA256:dIg0ywU1OP+ZYifrYxy8c5esO72cIKB+4/9wkZj1VaY
2 changed files with 18 additions and 16 deletions

View file

@ -197,12 +197,7 @@ namespace bolt {
++RefCount;
}
inline void unref() {
--RefCount;
if (RefCount == 0) {
delete this;
}
}
void unref();
void setParents();
@ -249,7 +244,7 @@ namespace bolt {
virtual Scope* getScope();
virtual ~Node();
virtual ~Node() {}
};

View file

@ -289,19 +289,26 @@ namespace bolt {
}
Node::~Node() {
void Node::unref() {
struct UnrefVisitor : public CSTVisitor<UnrefVisitor> {
--RefCount;
void visit(Node* N) {
visitEachChild(N);
N->unref();
}
if (RefCount == 0) {
};
// You may be wondering why we aren't unreffing the children in the
// destructor. This is due to a behaviour in Clang where a top-level
// destructor ~Node() wont get access to the fields in derived classes
// because they may already have been destroyed.
struct UnrefVisitor : public CSTVisitor<UnrefVisitor> {
void visit(Node* N) {
N->unref();
}
};
UnrefVisitor V;
V.visitEachChild(this);
UnrefVisitor V;
V.visitEachChild(this);
delete this;
}
}