From 24902b409ffcb473e28d629f3174ec6a3dd24c65 Mon Sep 17 00:00:00 2001
From: Dimitri van Heesch <doxygen@gmail.com>
Date: Sat, 14 Nov 2020 15:27:28 +0100
Subject: Fix for crash when using members of a partially constructed object

---
 src/classdef.cpp     |  4 ++--
 src/definition.cpp   | 15 +++++++++++----
 src/definitionimpl.h |  7 +++++++
 src/memberdef.cpp    |  4 ++--
 src/namespacedef.cpp |  4 ++--
 5 files changed, 24 insertions(+), 10 deletions(-)

diff --git a/src/classdef.cpp b/src/classdef.cpp
index 45d57ed..9f04897 100644
--- a/src/classdef.cpp
+++ b/src/classdef.cpp
@@ -287,8 +287,8 @@ class ClassDefAliasImpl : public DefinitionAliasMixin<ClassDef>
 {
   public:
     ClassDefAliasImpl(const Definition *newScope,const ClassDef *cd)
-      : DefinitionAliasMixin(newScope,cd) {}
-    virtual ~ClassDefAliasImpl() {}
+      : DefinitionAliasMixin(newScope,cd) { init(); }
+    virtual ~ClassDefAliasImpl() { deinit(); }
     virtual DefType definitionType() const { return TypeClass; }
 
     const ClassDef *getCdAlias() const { return toClassDef(getAlias()); }
diff --git a/src/definition.cpp b/src/definition.cpp
index 0d301c0..8b6e6fa 100644
--- a/src/definition.cpp
+++ b/src/definition.cpp
@@ -2179,15 +2179,22 @@ void DefinitionImpl::writeSummaryLinks(OutputList &) const
 //---------------------------------------------------------------------------------
 
 DefinitionAliasImpl::DefinitionAliasImpl(Definition *def,const Definition *alias)
-      : m_def(def)
+      : m_def(def), m_alias(alias)
 {
-  //printf("%s::addToMap(%s)\n",qPrint(name()),qPrint(alias->name()));
-  addToMap(alias->name(),m_def);
 }
 
 DefinitionAliasImpl::~DefinitionAliasImpl()
 {
-  //printf("~DefinitionAliasImpl()\n");
+}
+
+void DefinitionAliasImpl::init()
+{
+  //printf("%s::addToMap(%s)\n",qPrint(name()),qPrint(alias->name()));
+  addToMap(m_alias->name(),m_def);
+}
+
+void DefinitionAliasImpl::deinit()
+{
   removeFromMap(m_def);
 }
 
diff --git a/src/definitionimpl.h b/src/definitionimpl.h
index a5fc66d..0c2f713 100644
--- a/src/definitionimpl.h
+++ b/src/definitionimpl.h
@@ -294,8 +294,11 @@ class DefinitionAliasImpl
   public:
     DefinitionAliasImpl(Definition *def,const Definition *alias);
     virtual ~DefinitionAliasImpl();
+    void init();
+    void deinit();
   private:
     Definition *m_def;
+    const Definition *m_alias;
 };
 
 template<class Base>
@@ -304,6 +307,10 @@ class DefinitionAliasMixin : public Base
   public:
     DefinitionAliasMixin(const Definition *scope,const Definition *alias)
       : m_impl(this,alias), m_scope(scope), m_alias(alias), m_cookie(0) {}
+
+    void init() { m_impl.init(); }
+    void deinit() { m_impl.deinit(); }
+
     virtual ~DefinitionAliasMixin() = default;
 
     virtual bool isAlias() const { return TRUE; }
diff --git a/src/memberdef.cpp b/src/memberdef.cpp
index 95c6613..b7ac968 100644
--- a/src/memberdef.cpp
+++ b/src/memberdef.cpp
@@ -376,8 +376,8 @@ class MemberDefAliasImpl : public DefinitionAliasMixin<MemberDef>
 {
   public:
     MemberDefAliasImpl(const Definition *newScope,const MemberDef *md)
-    : DefinitionAliasMixin(newScope,md), m_memberGroup(0) {}
-    virtual ~MemberDefAliasImpl() {}
+    : DefinitionAliasMixin(newScope,md), m_memberGroup(0) { init(); }
+    virtual ~MemberDefAliasImpl() { deinit(); }
     virtual DefType definitionType() const { return TypeMember; }
 
     const MemberDef *getMdAlias() const           { return toMemberDef(getAlias()); }
diff --git a/src/namespacedef.cpp b/src/namespacedef.cpp
index 1671362..f0a63d2 100644
--- a/src/namespacedef.cpp
+++ b/src/namespacedef.cpp
@@ -157,8 +157,8 @@ class NamespaceDefAliasImpl : public DefinitionAliasMixin<NamespaceDef>
 {
   public:
     NamespaceDefAliasImpl(const Definition *newScope,const NamespaceDef *nd)
-      : DefinitionAliasMixin(newScope,nd) {}
-    virtual ~NamespaceDefAliasImpl() {}
+      : DefinitionAliasMixin(newScope,nd) { init(); }
+    virtual ~NamespaceDefAliasImpl() { deinit(); }
     virtual DefType definitionType() const { return TypeNamespace; }
 
     const NamespaceDef *getNSAlias() const { return toNamespaceDef(getAlias()); }
-- 
cgit v0.12

