{"action":"created","issue":{"url":"https://api.github.com/repos/log4cplus/log4cplus/issues/102","labels_url":"https://api.github.com/repos/log4cplus/log4cplus/issues/102/labels{/name}","comments_url":"https://api.github.com/repos/log4cplus/log4cplus/issues/102/comments","events_url":"https://api.github.com/repos/log4cplus/log4cplus/issues/102/events","html_url":"https://github.com/log4cplus/log4cplus/issues/102","id":98983173,"number":102,"title":"Application using log4cplus hangs on exit","user":{"login":"serlin74","id":13644035,"avatar_url":"https://avatars.githubusercontent.com/u/13644035?v=3","gravatar_id":"","url":"https://api.github.com/users/serlin74","html_url":"https://github.com/serlin74","followers_url":"https://api.github.com/users/serlin74/followers","following_url":"https://api.github.com/users/serlin74/following{/other_user}","gists_url":"https://api.github.com/users/serlin74/gists{/gist_id}","starred_url":"https://api.github.com/users/serlin74/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/serlin74/subscriptions","organizations_url":"https://api.github.com/users/serlin74/orgs","repos_url":"https://api.github.com/users/serlin74/repos","events_url":"https://api.github.com/users/serlin74/events{/privacy}","received_events_url":"https://api.github.com/users/serlin74/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/log4cplus/log4cplus/labels/bug","name":"bug","color":"fc2929"}],"state":"open","locked":false,"assignee":{"login":"wilx","id":1098563,"avatar_url":"https://avatars.githubusercontent.com/u/1098563?v=3","gravatar_id":"","url":"https://api.github.com/users/wilx","html_url":"https://github.com/wilx","followers_url":"https://api.github.com/users/wilx/followers","following_url":"https://api.github.com/users/wilx/following{/other_user}","gists_url":"https://api.github.com/users/wilx/gists{/gist_id}","starred_url":"https://api.github.com/users/wilx/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/wilx/subscriptions","organizations_url":"https://api.github.com/users/wilx/orgs","repos_url":"https://api.github.com/users/wilx/repos","events_url":"https://api.github.com/users/wilx/events{/privacy}","received_events_url":"https://api.github.com/users/wilx/received_events","type":"User","site_admin":false},"milestone":null,"comments":3,"created_at":"2015-08-04T14:27:40Z","updated_at":"2015-08-04T17:49:57Z","closed_at":null,"body":"With relatively latest log4cplus version (commit from 04122015) I have encountered unexpected hang in my application upon exit. After two days of digging, I have found that ThreadPool destructor is called twice:\r\n```\r\nBreakpoint 6, ~ThreadPool (this=0x6816d0, __in_chrg=<optimized out>)\r\n    at threadpool/ThreadPool.h:178\r\n178            std::unique_lock<std::mutex> lock(queue_mutex);\r\n(gdb) bt\r\n#0  ~ThreadPool (this=0x6816d0, __in_chrg=<optimized out>)\r\n    at threadpool/ThreadPool.h:178\r\n#1  operator() (this=<optimized out>, __ptr=0x6816d0)\r\n    at /usr/include/c++/5/bits/unique_ptr.h:76\r\n#2  ~unique_ptr (this=0x6815a8, __in_chrg=<optimized out>)\r\n    at /usr/include/c++/5/bits/unique_ptr.h:236\r\n#3  ~DefaultContext (this=0x6812b0, __in_chrg=<optimized out>)\r\n    at src/global-init.cxx:127\r\n#4  log4cplus::(anonymous namespace)::destroy_default_context::~destroy_default_context (\r\n    this=0x7ffff71562d6 <log4cplus::(anonymous namespace)::destroy_default_context_>, __in_chrg=<optimized out>) at src/global-init.cxx:162\r\n#5  0x00007ffff512a21f in __cxa_finalize (d=0x7ffff7151710)\r\n    at cxa_finalize.c:55\r\n#6  0x00007ffff6f05873 in __do_global_dtors_aux ()\r\n   from /usr/lib64/liblog4cplus-2.0.so.3\r\n#7  0x00007fffffffe010 in ?? ()\r\n#8  0x00007ffff7dea31f in _dl_fini () at dl-fini.c:253\r\n(gdb) c\r\nContinuing.\r\n[Thread 0x7fffeeb44700 (LWP 6151) exited]\r\n[Thread 0x7fffef345700 (LWP 6150) exited]\r\n```\r\n\r\nAs you can see, all worker threads have gone. And the second one:\r\n\r\n```\r\nProgram received signal SIGINT, Interrupt.\r\n__lll_lock_wait () at ../nptl/sysdeps/unix/sysv/linux/x86_64/lowlevellock.S:135\r\n135    2:    movl    %edx, %eax\r\n(gdb) bt\r\n#0  __lll_lock_wait ()\r\n    at ../nptl/sysdeps/unix/sysv/linux/x86_64/lowlevellock.S:135\r\n#1  0x00007ffff5a40640 in pthread_cond_broadcast@@GLIBC_2.3.2 ()\r\n    at ../nptl/sysdeps/unix/sysv/linux/x86_64/pthread_cond_broadcast.S:136\r\n#2  0x00007ffff576abb9 in __gthread_cond_broadcast (__cond=<optimized out>)\r\n    at /usr/src/debug/gcc-5.1.1-20150612/obj-x86_64-alt-linux/x86_64-alt-linux/libstdc++-v3/include/x86_64-alt-linux/bits/gthr-default.h:852\r\n#3  std::condition_variable::notify_all (this=<optimized out>)\r\n    at ../../../../../libstdc++-v3/src/c++11/condition_variable.cc:73\r\n#4  0x00007ffff6f1c8f3 in ~ThreadPool (this=0x6826d0, \r\n    __in_chrg=<optimized out>) at threadpool/ThreadPool.h:180\r\n#5  operator() (this=<optimized out>, __ptr=0x6826d0)\r\n    at /usr/include/c++/5/bits/unique_ptr.h:76\r\n#6  reset (__p=0x6826d0, this=<optimized out>)\r\n    at /usr/include/c++/5/bits/unique_ptr.h:344\r\n#7  log4cplus::shutdownThreadPool () at src/global-init.cxx:282\r\n#8  0x00007ffff6f2116b in log4cplus::Hierarchy::shutdown (\r\n    this=this@entry=0x682358) at src/hierarchy.cxx:244\r\n#9  0x00007ffff6f21357 in log4cplus::Hierarchy::~Hierarchy (this=0x682358, \r\n    __in_chrg=<optimized out>) at src/hierarchy.cxx:81\r\n#10 0x00007ffff6f1cfa5 in ~DefaultContext (this=0x6822b0, \r\n    __in_chrg=<optimized out>) at src/global-init.cxx:127\r\n#11 log4cplus::(anonymous namespace)::destroy_default_context::~destroy_default_---Type <return> to continue, or q <return> to quit--- \r\ncontext (this=<optimized out>, __in_chrg=<optimized out>)\r\n    at src/global-init.cxx:162\r\n#12 0x00007ffff512a21f in __cxa_finalize (d=0x7ffff7151710)\r\n    at cxa_finalize.c:55\r\n#13 0x00007ffff6f05873 in __do_global_dtors_aux ()\r\n   from /usr/lib64/liblog4cplus-2.0.so.3\r\n#14 0x00007fffffffdff0 in ?? ()\r\n#15 0x00007ffff7dea31f in _dl_fini () at dl-fini.c:253\r\n```\r\n\r\nAs you can see log4cplus::Hierarchy::shutdown tries to destruct already destructed ThreadPool instance. It happens because of fields order in struct DefaultContext (from global-init.cxx):\r\n\r\n```\r\nstruct DefaultContext\r\n{\r\n    log4cplus::thread::Mutex console_mutex;\r\n    helpers::LogLog loglog;\r\n    LogLevelManager log_level_manager;\r\n    helpers::Time TTCCLayout_time_base;\r\n    NDC ndc;\r\n    MDC mdc;\r\n    Hierarchy hierarchy;\r\n    spi::AppenderFactoryRegistry appender_factory_registry;\r\n    spi::LayoutFactoryRegistry layout_factory_registry;\r\n    spi::FilterFactoryRegistry filter_factory_registry;\r\n    spi::LocaleFactoryRegistry locale_factory_registry;\r\n#if ! defined (LOG4CPLUS_SINGLE_THREADED)\r\n    std::unique_ptr<progschj::ThreadPool> thread_pool {new progschj::ThreadPool};\r\n#endif\r\n};\r\n```\r\n\r\nthread_pool member is destructed before hierarchy member. As result, we are in trouble. Several solutions possible:\r\n1) change member order\r\n2) remove explicit ThreadPool shutdown from Hierarchy"},"comment":{"url":"https://api.github.com/repos/log4cplus/log4cplus/issues/comments/127690508","html_url":"https://github.com/log4cplus/log4cplus/issues/102#issuecomment-127690508","issue_url":"https://api.github.com/repos/log4cplus/log4cplus/issues/102","id":127690508,"user":{"login":"serlin74","id":13644035,"avatar_url":"https://avatars.githubusercontent.com/u/13644035?v=3","gravatar_id":"","url":"https://api.github.com/users/serlin74","html_url":"https://github.com/serlin74","followers_url":"https://api.github.com/users/serlin74/followers","following_url":"https://api.github.com/users/serlin74/following{/other_user}","gists_url":"https://api.github.com/users/serlin74/gists{/gist_id}","starred_url":"https://api.github.com/users/serlin74/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/serlin74/subscriptions","organizations_url":"https://api.github.com/users/serlin74/orgs","repos_url":"https://api.github.com/users/serlin74/repos","events_url":"https://api.github.com/users/serlin74/events{/privacy}","received_events_url":"https://api.github.com/users/serlin74/received_events","type":"User","site_admin":false},"created_at":"2015-08-04T17:49:57Z","updated_at":"2015-08-04T17:49:57Z","body":"Below is excerpt from my code (works fine with log4cplus 1.x.x)  to demonstrate the problem:\r\n\r\n```\r\n#include <log4cplus/logger.h>\r\n#include <log4cplus/loggingmacros.h>\r\n#include <log4cplus/consoleappender.h>\r\n\r\n/**\r\n * Initialize logging framework.\r\n */\r\nstatic void\r\ninitLogging()\r\n{\r\n    using namespace log4cplus;\r\n\r\n    // Initialize log4cplus framework.\r\n    initialize();\r\n\r\n    LogLevelManager& log_man = getLogLevelManager();\r\n\r\n    Logger root_logger = Logger::getRoot();\r\n\r\n    // Manage log level.\r\n    root_logger.setLogLevel(DEBUG_LOG_LEVEL);\r\n\r\n    // Configure log output layout.\r\n    tstring pattern = LOG4CPLUS_TEXT(\"%D{%Y-%m-%d %H:%M:%S.%q} [%t] %-5p %c - %m%n\");\r\n\r\n    // Manage logging to console.\r\n    SharedAppenderPtr console_ptr(new ConsoleAppender(\"test\"));\r\n\r\n    console_ptr->setLayout(std::auto_ptr<Layout>(new PatternLayout(pattern)));\r\n    root_logger.addAppender(console_ptr);\r\n\r\n    LOG4CPLUS_INFO(root_logger, LOG4CPLUS_TEXT(\"Logger initialized\"));\r\n}\r\n\r\n/**\r\n *\r\n */\r\nint main(int     argc, char*   argv[])\r\n{\r\n    initLogging();\r\n\r\n    LOG4CPLUS_INFO(log4cplus::Logger::getRoot(), LOG4CPLUS_TEXT(\"test exited\"));\r\n\r\n    return (0);\r\n}\r\n```\r\n\r\nMakefile:\r\n```\r\ntest1: test.cpp\r\n\t gcc -o test1 -std=gnu++11 -g test.cpp -lpthread -llog4cplus -lstdc++\r\n\r\n```"}}