*** OpenSSLPluginI.cpp 27 Oct 2004 19:53:33 -0000 1.46 --- OpenSSLPluginI.cpp 20 Jan 2005 14:12:02 -0000 *************** *** 54,59 **** --- 54,62 ---- void IceInternal::incRef(OpenSSLPluginI* p) { p->__incRef(); } void IceInternal::decRef(OpenSSLPluginI* p) { p->__decRef(); } + static IceUtil::StaticMutex staticMutex = ICE_STATIC_MUTEX_INITIALIZER; + static int instanceCount = 0; + // // Plugin factory function // *************** *** 186,207 **** _clientContext(new TraceLevels(protocolPluginFacade), protocolPluginFacade->getCommunicator()), _randSeeded(0) { ! if(_memDebug != 0) ! { ! CRYPTO_malloc_debug_init(); ! CRYPTO_set_mem_debug_options(V_CRYPTO_MDEBUG_ALL); ! CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON); ! } ! else { ! CRYPTO_set_mem_debug_functions(0, 0, 0, 0, 0); ! } ! SSL_library_init(); ! SSL_load_error_strings(); ! OpenSSL_add_ssl_algorithms(); } IceSSL::OpenSSLPluginI::~OpenSSLPluginI() --- 189,220 ---- _clientContext(new TraceLevels(protocolPluginFacade), protocolPluginFacade->getCommunicator()), _randSeeded(0) { ! // ! // It is possible for multiple instances of OpenSSLPluginI to be created ! // (one for each communicator). We use a mutex-protected counter to know ! // when to initialize and clean up OpenSSL. ! // ! IceUtil::StaticMutex::Lock sync(staticMutex); ! if(instanceCount == 0) { ! if(_memDebug != 0) ! { ! CRYPTO_malloc_debug_init(); ! CRYPTO_set_mem_debug_options(V_CRYPTO_MDEBUG_ALL); ! CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON); ! } ! else ! { ! CRYPTO_set_mem_debug_functions(0, 0, 0, 0, 0); ! } ! SSL_library_init(); ! SSL_load_error_strings(); ! OpenSSL_add_ssl_algorithms(); ! } ! ++instanceCount; } IceSSL::OpenSSLPluginI::~OpenSSLPluginI() *************** *** 209,231 **** _serverContext.cleanUp(); _clientContext.cleanUp(); #if OPENSSL_VERSION_NUMBER >= 0x0090700fL ! ENGINE_cleanup(); ! CRYPTO_cleanup_all_ex_data(); #endif ! // TODO: Introduces a 72byte memory leak, if we kidnap the code from OpenSSL 0.9.7a for ! // ENGINE_cleanup(), we can fix that. ! ERR_free_strings(); ! unregisterThreads(); ! ERR_remove_state(0); ! EVP_cleanup(); ! if(_memDebug != 0) ! { ! CRYPTO_mem_leaks_fp(stderr); } } --- 222,249 ---- _serverContext.cleanUp(); _clientContext.cleanUp(); + unregisterThreads(); + + IceUtil::StaticMutex::Lock sync(staticMutex); + if(--instanceCount == 0) + { #if OPENSSL_VERSION_NUMBER >= 0x0090700fL ! ENGINE_cleanup(); ! CRYPTO_cleanup_all_ex_data(); #endif ! // TODO: Introduces a 72byte memory leak, if we kidnap the code from OpenSSL 0.9.7a for ! // ENGINE_cleanup(), we can fix that. ! ERR_free_strings(); ! ERR_remove_state(0); ! EVP_cleanup(); ! if(_memDebug != 0) ! { ! CRYPTO_mem_leaks_fp(stderr); ! } } }