From 2deb5890dded11ce30a433c1a33d085adf208ab0 Mon Sep 17 00:00:00 2001
From: Tristan Matthews <tristan.matthews@savoirfairelinux.com>
Date: Wed, 11 Apr 2012 16:02:40 -0400
Subject: [PATCH] * #9774: make sure sipvoiplink is destroyed before accounts
 are unloaded

---
 daemon/src/managerimpl.cpp     |  1 +
 daemon/src/sip/sipvoiplink.cpp | 18 ++++++++++++++----
 daemon/src/sip/sipvoiplink.h   |  7 +++++++
 3 files changed, 22 insertions(+), 4 deletions(-)

diff --git a/daemon/src/managerimpl.cpp b/daemon/src/managerimpl.cpp
index 5da9b87c6a..030d6ae3b8 100644
--- a/daemon/src/managerimpl.cpp
+++ b/daemon/src/managerimpl.cpp
@@ -130,6 +130,7 @@ void ManagerImpl::terminate()
 
     saveConfig();
 
+    SIPVoIPLink::destroy();
     // Unload account map AFTER destroying
     // the SIPVoIPLink, the link still needs the accounts for pjsip cleanup
     unloadAccountMap();
diff --git a/daemon/src/sip/sipvoiplink.cpp b/daemon/src/sip/sipvoiplink.cpp
index 7b39a06f9f..b40c9190f6 100644
--- a/daemon/src/sip/sipvoiplink.cpp
+++ b/daemon/src/sip/sipvoiplink.cpp
@@ -1,6 +1,5 @@
 /*
  *  Copyright (C) 2004, 2005, 2006, 2008, 2009, 2010 Savoir-Faire Linux Inc.
- *
  *  Author: Emmanuel Milou <emmanuel.milou@savoirfairelinux.com>
  *  Author: Yun Liu <yun.liu@savoirfairelinux.com>
  *  Author: Pierre-Luc Bacon <pierre-luc.bacon@savoirfairelinux.com>
@@ -65,11 +64,13 @@
 #include <istream>
 #include <utility> // for std::pair
 
-
 #include <map>
 
 using namespace sfl;
 
+SIPVoIPLink *SIPVoIPLink::instance_ = 0;
+bool SIPVoIPLink::destroyed_ = false;
+
 namespace {
 
 /** A map to retreive SFLphone internal call id
@@ -507,8 +508,17 @@ SIPVoIPLink::~SIPVoIPLink()
 
 SIPVoIPLink* SIPVoIPLink::instance()
 {
-    static SIPVoIPLink instance_;
-    return &instance_;
+    assert(!destroyed_);
+    if (!instance_)
+        instance_ = new SIPVoIPLink;
+    return instance_;
+}
+
+void SIPVoIPLink::destroy()
+{
+    delete instance_;
+    destroyed_ = true;
+    instance_ = 0;
 }
 
 // Called from EventThread::run (not main thread)
diff --git a/daemon/src/sip/sipvoiplink.h b/daemon/src/sip/sipvoiplink.h
index 9a6c14eeda..97c912a2da 100644
--- a/daemon/src/sip/sipvoiplink.h
+++ b/daemon/src/sip/sipvoiplink.h
@@ -71,6 +71,11 @@ class SIPVoIPLink : public VoIPLink {
          */
         static SIPVoIPLink* instance();
 
+        /**
+         * Destroy the singleton instance
+         */
+        static void destroy();
+
         /**
          * Event listener. Each event send by the call manager is received and handled from here
          */
@@ -248,6 +253,8 @@ class SIPVoIPLink : public VoIPLink {
         EventThread evThread_;
 
         friend class SIPTest;
+        static bool destroyed_;
+        static SIPVoIPLink *instance_;
 };
 
 #endif // SIPVOIPLINK_H_
-- 
GitLab