From 4f85dfc9e97a5771aa711aae85c2d9c60e1c296e Mon Sep 17 00:00:00 2001 From: Maximilian Wittfeld Date: Sun, 12 Jan 2025 18:24:17 +0100 Subject: [PATCH] fix(server/gamestate): memory leak --- .../src/state/ServerGameState.cpp | 9 +++-- .../src/state/ServerSetters.cpp | 36 ++++++++++++++++--- .../server/CreateVehicleServerSetter.md | 2 +- 3 files changed, 40 insertions(+), 7 deletions(-) diff --git a/code/components/citizen-server-impl/src/state/ServerGameState.cpp b/code/components/citizen-server-impl/src/state/ServerGameState.cpp index 25c9c1e986..cb2ffa4a4e 100644 --- a/code/components/citizen-server-impl/src/state/ServerGameState.cpp +++ b/code/components/citizen-server-impl/src/state/ServerGameState.cpp @@ -3246,21 +3246,26 @@ void ServerGameState::FinalizeClone(const fx::ClientSharedPtr& client, const fx: auto ServerGameState::CreateEntityFromTree(sync::NetObjEntityType type, const std::shared_ptr& tree) -> fx::sync::SyncEntityPtr { - bool hadId = false; - int id = fx::IsLengthHack() ? (MaxObjectId - 1) : 8191; { + bool valid = false; std::unique_lock objectIdsLock(m_objectIdsMutex); for (; id >= 1; id--) { if (!m_objectIdsSent.test(id) && !m_objectIdsUsed.test(id)) { + valid = true; break; } } + if (!valid) + { + return {}; + } + m_objectIdsSent.set(id); m_objectIdsUsed.set(id); m_objectIdsStolen.set(id); diff --git a/code/components/citizen-server-impl/src/state/ServerSetters.cpp b/code/components/citizen-server-impl/src/state/ServerSetters.cpp index 8c9589f492..4426260c63 100644 --- a/code/components/citizen-server-impl/src/state/ServerSetters.cpp +++ b/code/components/citizen-server-impl/src/state/ServerSetters.cpp @@ -330,7 +330,14 @@ static InitFunction initFunction([]() auto sgs = ref->GetComponent(); auto entity = sgs->CreateEntityFromTree(sync::NetObjEntityType::Automobile, tree); - ctx.SetResult(sgs->MakeScriptHandle(entity)); + uint32_t guid = 0; + + if (entity) + { + guid = sgs->MakeScriptHandle(entity); + } + + ctx.SetResult(guid); }); fx::ScriptEngine::RegisterNativeHandler("CREATE_VEHICLE_SERVER_SETTER", [ref](fx::ScriptContext& ctx) @@ -426,7 +433,14 @@ static InitFunction initFunction([]() auto sgs = ref->GetComponent(); auto entity = sgs->CreateEntityFromTree(typeId, tree); - ctx.SetResult(sgs->MakeScriptHandle(entity)); + uint32_t guid = 0; + + if (entity) + { + guid = sgs->MakeScriptHandle(entity); + } + + ctx.SetResult(guid); }); fx::ScriptEngine::RegisterNativeHandler("CREATE_PED", [=](fx::ScriptContext& ctx) @@ -450,7 +464,14 @@ static InitFunction initFunction([]() auto sgs = ref->GetComponent(); auto entity = sgs->CreateEntityFromTree(sync::NetObjEntityType::Ped, tree); - ctx.SetResult(sgs->MakeScriptHandle(entity)); + uint32_t guid = 0; + + if (entity) + { + guid = sgs->MakeScriptHandle(entity); + } + + ctx.SetResult(guid); }); fx::ScriptEngine::RegisterNativeHandler("CREATE_OBJECT_NO_OFFSET", [=](fx::ScriptContext& ctx) @@ -474,7 +495,14 @@ static InitFunction initFunction([]() auto sgs = ref->GetComponent(); auto entity = sgs->CreateEntityFromTree(sync::NetObjEntityType::Object, tree); - ctx.SetResult(sgs->MakeScriptHandle(entity)); + uint32_t guid = 0; + + if (entity) + { + guid = sgs->MakeScriptHandle(entity); + } + + ctx.SetResult(guid); }); }); }); diff --git a/ext/native-decls/server/CreateVehicleServerSetter.md b/ext/native-decls/server/CreateVehicleServerSetter.md index 85938e64cb..0f9df26dc1 100644 --- a/ext/native-decls/server/CreateVehicleServerSetter.md +++ b/ext/native-decls/server/CreateVehicleServerSetter.md @@ -22,7 +22,7 @@ Unlike CREATE_AUTOMOBILE, this supports other vehicle types as well. * **heading**: Heading to face towards, in degrees. ## Return value -A script handle for the vehicle. +A script handle for the vehicle, or 0 if the vehicle failed to be created. ## Examples ```lua