Fixing Memory Leaks with Smart Pointer Implementation in Message System

Introduction

In the initial implementation, every invocation of the ‘CreateWhisper’ function with the same argument ‘c_szName’ resulted in the creation of a new ‘CWhisper’ object. Subsequently, this object was added to a map. However, this practice posed the risk of potentially overwriting existing objects without prior deletion, thereby creating the possibility of memory leaks.

Solution: Implementation of Smart Pointers

I opted to utilize smart pointers, specifically std::unique_ptr<CWhisper>, albeit exclusively within the context of the m_WhisperMap. These smart pointers are not utilized as return values from the function. By integrating std::unique_ptr within the map, memory management of CWhisper objects is now automatic and significantly safer. Upon removal of an element from the map, the corresponding std::unique_ptr automatically deallocates the memory it occupies. While the return value of the CreateWhisper function remains a raw pointer, its memory management is now controlled by the std::unique_ptr stored within the map. This approach effectively mitigates the risk of memory leaks.

Implementation:

📁 File Location: ‘UserInterface -> PythonChat.cpp’

🔍 Find

CWhisper * CPythonChat::CreateWhisper(const char * c_szName)
{
	CWhisper * pWhisper = CWhisper::New();
	m_WhisperMap.insert(TWhisperMap::value_type(c_szName, pWhisper));
	return pWhisper;
}

♻️ Replace

CWhisper* CPythonChat::CreateWhisper(const char* targetName)
{
	auto whisperIterator = m_WhisperMap.find(targetName);

	if (whisperIterator == m_WhisperMap.end())
	{
		auto newWhisper = std::make_unique<CWhisper>();
		CWhisper* rawWhisperPointer = newWhisper.get();
		m_WhisperMap.emplace(targetName, std::move(newWhisper));
		return rawWhisperPointer;
	}
	else
	{
		return whisperIterator->second.get();
	}
}

🔍 Find

void CPythonChat::AppendWhisper(int iType, const char * c_szName, const char * c_szChat)

pWhisper = itor->second;

♻️ Replace

pWhisper = itor->second.get();

🔍 Find

BOOL CPythonChat::GetWhisper(const char * c_szName, CWhisper ** ppWhisper)

*ppWhisper = itor->second;

♻️ Replace

*ppWhisper = itor->second.get();

🔍 Find

void CPythonChat::ClearWhisper(const char * c_szName)
{
	TWhisperMap::iterator itor = m_WhisperMap.find(c_szName);

	if (itor != m_WhisperMap.end())
	{
		CWhisper * pWhisper = itor->second;
		CWhisper::Delete(pWhisper);
		
		m_WhisperMap.erase(itor);
	}
}

♻️ Replace

void CPythonChat::ClearWhisper(const char * c_szName)
{
	TWhisperMap::iterator itor = m_WhisperMap.find(c_szName);

	if (itor != m_WhisperMap.end())
	{
		m_WhisperMap.erase(itor);
	}
}

🔍 Find

void CPythonChat::__DestroyWhisperMap()
{
	TWhisperMap::iterator itor = m_WhisperMap.begin();
	for (; itor != m_WhisperMap.end(); ++itor)
	{
		CWhisper::Delete(itor->second);
	}
	m_WhisperMap.clear();
}

♻️ Replace

void CPythonChat::__DestroyWhisperMap()
{
	m_WhisperMap.clear();
}

📁 File Location: ‘UserInterface -> PythonChat.h’

🔍 Find

CWhisper * CreateWhisper(const char * c_szName);

♻️ Replace

CWhisper* CreateWhisper(const char* targetName);

🔍 Find

typedef std::map<std::string, CWhisper*> TWhisperMap;

♻️ Replace

using TWhisperMap = std::map<std::string, std::unique_ptr<CWhisper>>;

Share this article
Shareable URL
Prev Post

Metin2: LoginKey FIX

Next Post

New Feature for Generating Unique Identifiers

Dodaj komentarz

Twój adres e-mail nie zostanie opublikowany. Wymagane pola są oznaczone *

Read next