Skip to content
This repository has been archived by the owner on Nov 18, 2022. It is now read-only.

Commit

Permalink
#168: 9da07e1: adjusted finalization
Browse files Browse the repository at this point in the history
of global objects
  • Loading branch information
hugbug committed Feb 19, 2016
1 parent 9dfa66a commit b9076fc
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 35 deletions.
29 changes: 13 additions & 16 deletions daemon/connect/Connection.cpp
Expand Up @@ -35,6 +35,17 @@ std::unique_ptr<Mutex> Connection::m_getHostByNameMutex;
#endif
#endif

class ConnectionFinalizer
{
public:
~ConnectionFinalizer()
{
Connection::Final();
}
};

std::unique_ptr<ConnectionFinalizer> m_connectionFinalizer;

void closesocket_gracefully(SOCKET socket)
{
char buf[1024];
Expand Down Expand Up @@ -93,34 +104,20 @@ void Connection::Init()
}
#endif

#ifndef DISABLE_TLS
TlsSocket::Init();
#endif

#ifndef HAVE_GETADDRINFO
#ifndef HAVE_GETHOSTBYNAME_R
m_getHostByNameMutex = std::make_unique<Mutex>();
#endif
#endif

m_connectionFinalizer = std::make_unique<ConnectionFinalizer>();
}

void Connection::Final()
{
debug("Finalizing global connection data");

#ifdef WIN32
WSACleanup();
#endif

#ifndef DISABLE_TLS
TlsSocket::Final();
#endif

#ifndef HAVE_GETADDRINFO
#ifndef HAVE_GETHOSTBYNAME_R
m_getHostByNameMutex.reset();
#endif
#endif
}

Connection::Connection(const char* host, int port, bool tls)
Expand Down
5 changes: 4 additions & 1 deletion daemon/connect/Connection.h
Expand Up @@ -40,6 +40,10 @@

class Connection
{
private:
static void Final();
friend class ConnectionFinalizer;

public:
enum EStatus
{
Expand Down Expand Up @@ -117,7 +121,6 @@ class Connection
Connection(SOCKET socket, bool tls);
virtual ~Connection();
static void Init();
static void Final();
virtual bool Connect();
virtual bool Disconnect();
bool Bind();
Expand Down
14 changes: 12 additions & 2 deletions daemon/connect/TlsSocket.cpp
Expand Up @@ -31,6 +31,16 @@
#include "Thread.h"
#include "Log.h"

class TlsSocketFinalizer
{
public:
~TlsSocketFinalizer()
{
TlsSocket::Final();
}
};

std::unique_ptr<TlsSocketFinalizer> m_tlsSocketFinalizer;

#ifdef HAVE_LIBGNUTLS
#ifdef NEED_GCRYPT_LOCKING
Expand Down Expand Up @@ -188,12 +198,12 @@ void TlsSocket::Init()
CRYPTO_set_dynlock_lock_callback(openssl_dynlock_lock);

#endif /* HAVE_OPENSSL */

m_tlsSocketFinalizer = std::make_unique<TlsSocketFinalizer>();
}

void TlsSocket::Final()
{
debug("Finalizing TLS library");

#ifdef HAVE_LIBGNUTLS
gnutls_global_deinit();

Expand Down
4 changes: 3 additions & 1 deletion daemon/connect/TlsSocket.h
Expand Up @@ -48,14 +48,16 @@ class TlsSocket

void ReportError(const char* errMsg);

static void Final();
friend class TlsSocketFinalizer;

protected:
virtual void PrintError(const char* errMsg);

public:
TlsSocket(SOCKET socket, bool isClient, const char* certFile, const char* keyFile, const char* cipher);
virtual ~TlsSocket();
static void Init();
static void Final();
bool Start();
void Close();
int Send(const char* buffer, int size);
Expand Down
9 changes: 3 additions & 6 deletions daemon/main/nzbget.cpp
Expand Up @@ -246,6 +246,9 @@ void NZBGet::Init()
{
Thread::Init();
Connection::Init();
#ifndef DISABLE_TLS
TlsSocket::Init();
#endif
}

CreateGlobals();
Expand Down Expand Up @@ -409,12 +412,6 @@ void NZBGet::Cleanup()
FileSystem::DeleteFile(m_options->GetLockFile());
}

if (!m_reloading)
{
Connection::Final();
Thread::Final();
}

g_UrlCoordinator = nullptr;
g_PrePostProcessor = nullptr;
g_Scanner = nullptr;
Expand Down
7 changes: 0 additions & 7 deletions daemon/util/Thread.cpp
Expand Up @@ -86,13 +86,6 @@ void Thread::Init()
m_threadMutex = std::make_unique<Mutex>();
}

void Thread::Final()
{
debug("Finalizing global thread data");

m_threadMutex.reset();
}

Thread::Thread()
{
debug("Creating Thread");
Expand Down
1 change: 0 additions & 1 deletion daemon/util/Thread.h
Expand Up @@ -59,7 +59,6 @@ class Thread
Thread();
virtual ~Thread();
static void Init();
static void Final();

virtual void Start();
virtual void Stop();
Expand Down
1 change: 0 additions & 1 deletion tests/suite/TestMain.cpp
Expand Up @@ -62,7 +62,6 @@ int TestMain(int argc, char * argv[])
int ret = Catch::Session().run(argc, testsargv);

free(testsargv);
Thread::Final();
TestUtil::Final();

return ret;
Expand Down

0 comments on commit b9076fc

Please sign in to comment.