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

Possible memory leak - depots not getting deleted from memory at player deletion #3346

Closed
djseban opened this issue Feb 24, 2021 · 3 comments · Fixed by #3423 or #4879
Closed

Possible memory leak - depots not getting deleted from memory at player deletion #3346

djseban opened this issue Feb 24, 2021 · 3 comments · Fixed by #3423 or #4879
Labels
bug An issue describing unexpected behavior of code

Comments

@djseban
Copy link

djseban commented Feb 24, 2021

Hey guys,

When working on one feature, I was poking around the code of TFS and in Player class destructor I spotted one thing:

Player::~Player()
{
	for (Item* item : inventory) {
		if (item) {
			item->setParent(nullptr);
			item->decrementReferenceCounter();
		}
	}

	for (const auto& it : depotLockerMap) {
		it.second->removeInbox(inbox);
		it.second->decrementReferenceCounter();
	}

	inbox->decrementReferenceCounter();

	storeInbox->setParent(nullptr);
	storeInbox->decrementReferenceCounter();

	setWriteItem(nullptr);
	setEditHouse(nullptr);
}

While in depotLockerMap the inbox is removed, the depot chests of player (depotChests std::map) remain untouched. Theoretically depot chests should get deleted due to
it.second->decrementReferenceCounter();
being called, but quick modification of decrementReferenceCounter in item.h

void decrementReferenceCounter() {
   if (getID() == ITEM_DEPOT) {
      std::cout << "decrement depot " << referenceCounter <<std::endl;
   }
   if (--referenceCounter == 0) {
      delete this;
   }
}

reveals that in fact no depot chest is getting deleted (we don't get any cout in terminal). Moreover, by accessing previously saved pointer to some Item that was in the depot of deleted player, we are able to retrieve it. The easiest way I found to reproduce this is following:

  1. Place one item of choice in depot chest. The item has to remain on first slot of depot chest.
  2. Execute following lua code:
local dpId = 3
local item = player:getDepotChest(dpId):getItem(0) 
local pos = player:getPosition() 
player:remove() 
addEvent(function() Tile(pos):addItemEx(item:clone()) end, 5000)
  1. Lua code will throw you out of a game. Log in into the same or the other character to witness item being created on the tile you logged out with first character.
    Expected behavior of the TFS is to crash - due to accessing the item that doesn't really exist in memory, which it does, if we try the same thing, but with the inbox of a player:
local item = player:getInbox():getItem(0) 
local pos = player:getPosition() 
player:remove() 
addEvent(function() Tile(pos):addItemEx(item:clone()) end, 5000)

It's pretty hard to believe this kind of memory leak was in TFS for such long time. Obviously, it does not eat that much RAM, nor allows cloning of any items without specific code, but nevertheless I guess it's potential memory leak. Am I wrong or missing something?

@ranisalt
Copy link
Member

The whole increment/decrementReferenceCounter smells to me what should have always been a std::shared_ptr, or probably should have been converted to.

@nekiro
Copy link
Member

nekiro commented Feb 26, 2021

@ranisalt true, but the problem is the scale of this code, it's literally remaking whole server

@ghost
Copy link

ghost commented Apr 22, 2021

@djseban can you please review the #3423 ?

@nekiro nekiro mentioned this issue Dec 21, 2024
3 tasks
@nekiro nekiro reopened this Dec 21, 2024
@nekiro nekiro added the bug An issue describing unexpected behavior of code label Dec 21, 2024
@github-project-automation github-project-automation bot moved this to Backlog in TFS 1.8 Dec 21, 2024
@github-project-automation github-project-automation bot moved this from Backlog to Done in TFS 1.8 Dec 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug An issue describing unexpected behavior of code
Projects
Archived in project
3 participants