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

Fix onNetworkMessage event memory leak #4887

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

maattch
Copy link
Contributor

@maattch maattch commented Jan 24, 2025

Pull Request Prelude

  • I have followed [proper The Forgotten Server code styling][code].
  • I have read and understood the [contribution guidelines][cont] before making this PR.
  • I am aware that this PR may be closed if the above-mentioned criteria are not fulfilled.

Issues addressed:
Fix memory leak

How to test:
Disable Player:onNetworkMessage event and send packets with unknown opcode to server, such as from store.

@MillhioreBT
Copy link
Contributor

MillhioreBT commented Feb 4, 2025

In the best-case scenario, Lua will clean up the object; in the worst-case scenario, there will be a memory leak. That is correct—thank you for catching this issue.

However, I would prefer if you made some adjustments to the code.

  1. Construct the unique_ptr directly in the lambda's scope by calling std::make_unique.
  2. Make the msg parameter passed by reference in the methods: parsePlayerNetworkMessage and playerOnNetworkMessage.

Please do not forget to use CCLANG format, so we can merge it.

src/events.cpp Outdated Show resolved Hide resolved
src/events.h Outdated Show resolved Hide resolved
src/game.cpp Show resolved Hide resolved
src/game.cpp Outdated Show resolved Hide resolved
src/game.h Show resolved Hide resolved
src/protocolgame.cpp Show resolved Hide resolved
src/protocolgame.cpp Show resolved Hide resolved
@MillhioreBT
Copy link
Contributor

MillhioreBT commented Feb 4, 2025

You are right, anyway you can not pass a mutable lambda to the addTask method, so its solution seems to be the only option for now, it only fixes the cclang format.

Although you can still pass by reference to the method tfs::events::player::onNetworkMessage, doing so saves a call to std::move.

src/events.h Show resolved Hide resolved
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants