fix SIGSEGV when trying to log with already unloaded log_hook

Program received signal SIGSEGV, Segmentation fault.
0x00000000007d58e0 in ?? ()
(gdb) bt
#0  0x00000000007d58e0 in ?? ()
#1  0x00000000004474a2 in run_log_hooks (level=3, pid=15808, tid=0,
func=0x55f13e <_SipCtrlInterface::cleanup()::__FUNCTION__> "cleanup",
file=0x55ea10 "core/SipCtrlInterface.cpp", line=308,
    msg=0x7fffffffcdd0 " Stopping SIP control interface threads") at
core/log.cpp:193
#2  0x00000000004c570e in _SipCtrlInterface::cleanup (this=0x7bdc50) at
core/SipCtrlInterface.cpp:308
#3  0x0000000000442895 in main (argc=<optimized out>, argv=<optimized
out>) at core/sems.cpp:633
(gdb) frame 1
#1  0x00000000004474a2 in run_log_hooks (level=3, pid=15808, tid=0,
func=0x55f13e <_SipCtrlInterface::cleanup()::__FUNCTION__> "cleanup",
file=0x55ea10 "core/SipCtrlInterface.cpp", line=308,
    msg=0x7fffffffcdd0 " Stopping SIP control interface threads") at
core/log.cpp:193
193           (*it)->log(level, pid, tid, func, file, line, msg);
(gdb) p *it
Attempt to take address of value not located in memory.
(gdb) p it
$1 = {
  _M_current = 0x7c6288


patch that solves the problem in attach

-- 
Michael Furmur

diff --git a/core/AmApi.cpp b/core/AmApi.cpp
index 4a9986e..c7cf660 100644
--- a/core/AmApi.cpp
+++ b/core/AmApi.cpp
@@ -157,3 +157,7 @@ AmLoggingFacility::AmLoggingFacility(const string& name)
   : AmPluginFactory(name) 
 {
 }
+
+void AmLoggingFacility::on_destroy(){
+  unregister_log_hook(this);
+}
diff --git a/core/AmApi.h b/core/AmApi.h
index c5a0198..2a0782f 100644
--- a/core/AmApi.h
+++ b/core/AmApi.h
@@ -222,6 +222,7 @@ class AmLoggingFacility : public AmPluginFactory
    * @param msg   message
    */
   virtual void log(int level, pid_t pid, pthread_t tid, const char* func, const char* file, int line, char* msg) = 0;
+  void on_destroy();
 };
 
 #if  __GNUC__ < 3
diff --git a/core/log.cpp b/core/log.cpp
index 79d706b..2c7d51e 100644
--- a/core/log.cpp
+++ b/core/log.cpp
@@ -28,6 +28,7 @@
 #include <ctype.h>
 #include <stdio.h>
 #include <unistd.h>
+#include <algorithm>
 
 #ifndef DISABLE_SYSLOG_LOG
 # include <syslog.h>
@@ -203,6 +204,14 @@ void register_log_hook(AmLoggingFacility* fac)
 {
   AmLock lock(log_hooks_mutex);
   log_hooks.push_back(fac);
+  inc_ref(fac);
+}
+
+void unregister_log_hook(AmLoggingFacility* fac){
+	AmLock lock(log_hooks_mutex);
+	vector<AmLoggingFacility*>::iterator fac_it = std::find(log_hooks.begin(),log_hooks.end(),fac);
+	if(fac_it!=log_hooks.end())
+		log_hooks.erase(fac_it);
 }
 
 /**
diff --git a/core/log.h b/core/log.h
index 7eb2480..dd65369 100644
--- a/core/log.h
+++ b/core/log.h
@@ -184,6 +184,7 @@ void log_stacktrace(int ll);
 /* ...only for C++ */
 class AmLoggingFacility;
 void register_log_hook(AmLoggingFacility*);
+void unregister_log_hook(AmLoggingFacility*);
 #endif
 
 #endif /* !_log_h_ */
_______________________________________________
Semsdev mailing list
[email protected]
http://lists.iptel.org/mailman/listinfo/semsdev

Reply via email to