Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix crash when unloading the library #108

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 12 additions & 19 deletions src/Memory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@


#include "Memory.h"
#include "Init.h"


Memory::Memory()
Expand All @@ -30,15 +31,15 @@ void Memory::Reset()

void Memory::ResetThread(const unsigned thrId)
{
memory[thrId]->transTable->ResetMemory(TT_RESET_FREE_MEMORY);
memory[thrId]->memUsed = Memory::MemoryInUseMB(thrId);
memory[thrId].transTable->ResetMemory(TT_RESET_FREE_MEMORY);
memory[thrId].memUsed = Memory::MemoryInUseMB(thrId);
}


void Memory::ReturnThread(const unsigned thrId)
{
memory[thrId]->transTable->ReturnAllMemory();
memory[thrId]->memUsed = Memory::MemoryInUseMB(thrId);
memory[thrId].transTable->ReturnAllMemory();
memory[thrId].memUsed = Memory::MemoryInUseMB(thrId);
}


Expand All @@ -53,13 +54,6 @@ void Memory::Resize(

if (nThreads > n)
{
// Downsize.
for (unsigned i = n; i < nThreads; i++)
{
memory[i]->transTable->ReturnAllMemory();
delete memory[i]->transTable;
delete memory[i];
}
memory.resize(static_cast<unsigned>(n));
threadSizes.resize(static_cast<unsigned>(n));
}
Expand All @@ -70,22 +64,21 @@ void Memory::Resize(
threadSizes.resize(n);
for (unsigned i = nThreads; i < n; i++)
{
memory[i] = new ThreadData;
if (flag == DDS_TT_SMALL)
{
memory[i]->transTable = new TransTableS;
memory[i].transTable = std::unique_ptr<TransTableS>(new TransTableS);
threadSizes[i] = "S";
}
else
{
memory[i]->transTable = new TransTableL;
memory[i].transTable = std::unique_ptr<TransTableL>(new TransTableL);
threadSizes[i] = "L";
}

memory[i]->transTable->SetMemoryDefault(memDefault_MB);
memory[i]->transTable->SetMemoryMaximum(memMaximum_MB);
memory[i].transTable->SetMemoryDefault(memDefault_MB);
memory[i].transTable->SetMemoryMaximum(memMaximum_MB);

memory[i]->transTable->MakeTT();
memory[i].transTable->MakeTT();
}
}

Expand All @@ -106,13 +99,13 @@ ThreadData * Memory::GetPtr(const unsigned thrId)
cout << "Memory::GetPtr: " << thrId << " vs. " << nThreads << endl;
exit(1);
}
return memory[thrId];
return &memory[thrId];
}


double Memory::MemoryInUseMB(const unsigned thrId) const
{
return memory[thrId]->transTable->MemoryInUse() +
return memory[thrId].transTable->MemoryInUse() +
8192. * sizeof(relRanksType) / static_cast<double>(1024.);
}

Expand Down
5 changes: 3 additions & 2 deletions src/Memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#ifndef DDS_MEMORY_H
#define DDS_MEMORY_H

#include <memory>
#include <vector>

#include "TransTable.h"
Expand Down Expand Up @@ -78,7 +79,7 @@ struct ThreadData
// 960 KB
relRanksType rel[8192];

TransTable * transTable;
std::unique_ptr<TransTable> transTable;

Moves moves;

Expand Down Expand Up @@ -116,7 +117,7 @@ class Memory
{
private:

vector<ThreadData *> memory;
vector<ThreadData> memory;
unsigned nThreads;

vector<string> threadSizes;
Expand Down
6 changes: 0 additions & 6 deletions src/dds.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ extern "C" BOOL APIENTRY DllMain(
SetMaxThreads(0);
else if (ul_reason_for_call == DLL_PROCESS_DETACH)
{
CloseDebugFiles();
FreeMemory();
#ifdef DDS_MEMORY_LEAKS_WIN32
_CrtDumpMemoryLeaks();
#endif
Expand All @@ -58,8 +56,6 @@ void DDSInitialize(void)

void DDSFinalize(void)
{
CloseDebugFiles();
FreeMemory();
}

#elif defined(USES_CONSTRUCTOR)
Expand All @@ -72,8 +68,6 @@ static void __attribute__ ((constructor)) libInit(void)

static void __attribute__ ((destructor)) libEnd(void)
{
CloseDebugFiles();
FreeMemory();
}

#endif
Expand Down