Jump to content

Safebox Memory Leak Fix


Mali

Recommended Posts

  • Honorable Member

Find in ClientManager.cpp(x2):

				pkPeer->EncodeHeader(HEADER_DG_SAFEBOX_WRONG_PASSWORD, dwHandle, 0);

Add(x2):

				delete pSafebox;

cc183db7fd.png

 

 

safebox.cpp Find:

	if (pkOldGrid)
		m_pkGrid = M2_NEW CGrid(pkOldGrid, 5, m_iSize);
	else
		m_pkGrid = M2_NEW CGrid(5, m_iSize);

Change:

	if (pkOldGrid) {
		m_pkGrid = M2_NEW CGrid(pkOldGrid, 5, m_iSize);
		delete pkOldGrid;
	}
	else
		m_pkGrid = M2_NEW CGrid(5, m_iSize);

 

 

 

Edited by Metin2 Dev
Core X - External 2 Internal
  • Metin2 Dev 1
  • Confused 1
  • Love 24

 

Link to comment
Share on other sites

Nice shot. For better code management and reusability you could remove the delete from the if (pkOldGrid) check and put it into the override method CGrid::CGrid(CGrid * pkGrid, int w, int h) : m_iWidth(w), m_iHeight(h) like this:
 

CGrid::CGrid(int w, int h) : m_iWidth(w), m_iHeight(h)					
{
	m_pGrid = new char[m_iWidth * m_iHeight];						
	memset(m_pGrid, 0, sizeof(char) * m_iWidth * m_iHeight);
}

CGrid::CGrid(CGrid * pkGrid, int w, int h) : m_iWidth(w), m_iHeight(h)		//<- if you add delete pkGrid here you can call M2_NEW CGrid(CGrid*, width, height) and it will delete the old pointer of safebox function at the end of this method 
{
	m_pGrid = new char[m_iWidth * m_iHeight];
	int iSize = std::MIN(w * h, pkGrid->m_iWidth * pkGrid->m_iHeight);
	thecore_memcpy(m_pGrid, pkGrid->m_pGrid, sizeof(char) * iSize);
  	//here
	delete pkGrid;
}

 

  • Love 1
Link to comment
Share on other sites

  • Honorable Member
49 minutes ago, OtherChoice said:

Nice shot. For better code management and reusability you could remove the delete from the if (pkOldGrid) check and put it into the override method CGrid::CGrid(CGrid * pkGrid, int w, int h) : m_iWidth(w), m_iHeight(h) like this:
 


CGrid::CGrid(int w, int h) : m_iWidth(w), m_iHeight(h)					
{
	m_pGrid = new char[m_iWidth * m_iHeight];						
	memset(m_pGrid, 0, sizeof(char) * m_iWidth * m_iHeight);
}

CGrid::CGrid(CGrid * pkGrid, int w, int h) : m_iWidth(w), m_iHeight(h)		//<- if you add delete pkGrid here you can call M2_NEW CGrid(CGrid*, width, height) and it will delete the old pointer of safebox function at the end of this method 
{
	m_pGrid = new char[m_iWidth * m_iHeight];
	int iSize = std::MIN(w * h, pkGrid->m_iWidth * pkGrid->m_iHeight);
	thecore_memcpy(m_pGrid, pkGrid->m_pGrid, sizeof(char) * iSize);
  	//here
	delete pkGrid;
}

 

Actually I wouldn't prefer this method

 

Link to comment
Share on other sites

  • Honorable Member
29 minutes ago, masodikbela said:

The second one is totally wrong and causes double deletion.
5YSB2Yt.png

I didn't see thanks

Edited by Metin2 Dev
Core X - External 2 Internal
  • Love 1

 

Link to comment
Share on other sites

On 3/2/2020 at 8:44 AM, OtherChoice said:

Nice shot. For better code management and reusability you could remove the delete from the if (pkOldGrid) check and put it into the override method CGrid::CGrid(CGrid * pkGrid, int w, int h) : m_iWidth(w), m_iHeight(h) like this:
 


CGrid::CGrid(int w, int h) : m_iWidth(w), m_iHeight(h)					
{
	m_pGrid = new char[m_iWidth * m_iHeight];						
	memset(m_pGrid, 0, sizeof(char) * m_iWidth * m_iHeight);
}

CGrid::CGrid(CGrid * pkGrid, int w, int h) : m_iWidth(w), m_iHeight(h)		//<- if you add delete pkGrid here you can call M2_NEW CGrid(CGrid*, width, height) and it will delete the old pointer of safebox function at the end of this method 
{
	m_pGrid = new char[m_iWidth * m_iHeight];
	int iSize = std::MIN(w * h, pkGrid->m_iWidth * pkGrid->m_iHeight);
	thecore_memcpy(m_pGrid, pkGrid->m_pGrid, sizeof(char) * iSize);
  	//here
	delete pkGrid;
}

 

you are just doing excess code.

  • Love 1
Link to comment
Share on other sites

  • Honorable Member
On 3/15/2020 at 1:53 AM, Mafuyu said:

u already edited it? is the guide now correct or is the second fix still the double deletion?

correct...

I have already deleted the wrong one.

  • Love 1

 

Link to comment
Share on other sites

Announcements



×
×
  • Create New...

Important Information

Terms of Use / Privacy Policy / Guidelines / We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.