Jump to content

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 25

 

Link to comment
https://metin2.dev/topic/22975-safebox-memory-leak-fix/
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
https://metin2.dev/topic/22975-safebox-memory-leak-fix/#findComment-124316
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
https://metin2.dev/topic/22975-safebox-memory-leak-fix/#findComment-124317
Share on other sites

  • Premium

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

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

The one and only UI programming guideline

Link to comment
https://metin2.dev/topic/22975-safebox-memory-leak-fix/#findComment-124350
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
https://metin2.dev/topic/22975-safebox-memory-leak-fix/#findComment-124351
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
https://metin2.dev/topic/22975-safebox-memory-leak-fix/#findComment-124483
Share on other sites

Deleting the old grid in the constructor of the new grid just makes destruction less obvious and when working with raw pointers to heap memory you do not want destruction to be hidden.

As it can easily lead to heap use after free bugs

Link to comment
https://metin2.dev/topic/22975-safebox-memory-leak-fix/#findComment-124698
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
https://metin2.dev/topic/22975-safebox-memory-leak-fix/#findComment-124802
Share on other sites

Don't use any images from : imgur, turkmmop, freakgamers, inforge, hizliresim... Or your content will be deleted without notice...
Use : https://metin2.download/media/add/

Please use https://metin2.download/ when uploading files smaller than 100MB, otherwise the approval will take longer due to manual upload.

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
×
×
  • 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.