Skip to content
This repository has been archived by the owner on Jan 3, 2019. It is now read-only.

Memory Leaking Problems #19

Open
jxsl13 opened this issue Mar 12, 2018 · 4 comments
Open

Memory Leaking Problems #19

jxsl13 opened this issue Mar 12, 2018 · 4 comments

Comments

@jxsl13
Copy link

jxsl13 commented Mar 12, 2018

Some commands, even player accessible ones like /top seem to cause memory leaks. Something seems not to be cleaned.

Also some free() function calls seem to be weirdly placed in the ranking system functions.

@Teelevision
Copy link
Owner

I can imagine. PRs are welcome.

@jxsl13
Copy link
Author

jxsl13 commented Mar 12, 2018

GameServer()->AddRankingThread(new std::thread(&CGameController_zCatch::SaveScore,

std::vector<std::thread*> m_RankingThreads;

void AddRankingThread(std::thread *thread) { m_RankingThreads.push_back(thread); };

CGameContext::~CGameContext()
{
/* ranking db */
/* wait for all threads */
for (auto &thread: m_RankingThreads)
{
thread->join();
delete thread;
}
for(int i = 0; i < MAX_CLIENTS; i++)
delete m_apPlayers[i];
if (!m_Resetting)
{
delete m_pVoteOptionHeap;
/* close ranking db */
if (RankingEnabled())
{
sqlite3_close(m_RankingDb);
}
}
}

I think it should be possible to somehow delete the allocated memory of the threads before the destruction of the actual GameContext. It's possible to spam "/top", which spawns endless threads, which are already finished executing, but the thread handles are still in memory.
I don't know when exactly the GameContext is destroyed, but I suppose it's when the server is shutdown.

@jxsl13
Copy link
Author

jxsl13 commented Mar 12, 2018

The solution would be to remove the vector, the AddRankingThread() function and replace every usage of said function with
std::thread *t = new std::thread(&...);
t->detach();
delete t;

and delete the part in the destructor.

@jxsl13
Copy link
Author

jxsl13 commented Mar 19, 2018

/*thread stuff*/
	std::vector<std::thread*> m_Threads;

	void AddThread(std::thread *thread) { m_Threads.push_back(thread); };
	void CleanThreads() {
		while (!m_Threads.empty()) {
			std::thread *t = m_Threads.back();
			t->join();
			delete t;
			m_Threads.pop_back();
		} ;
	};

Put this into Tick and in either OnShutDown or in the Destructor:

if (Tick % (50 * 5) == 0) {
				CleanThreads();
			}

Detached threads are hard to control, thus better keep joining them every 5 seconds, when they are done writing to the DB.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants