Skip to content

Commit

Permalink
Merge Fixed memory leak (pr-3063)
Browse files Browse the repository at this point in the history
4f85dfc - fix(server/gamestate): memory leak
  • Loading branch information
prikolium-cfx committed Jan 21, 2025
2 parents d2c551f + 4f85dfc commit 0d108a3
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3246,21 +3246,26 @@ void ServerGameState::FinalizeClone(const fx::ClientSharedPtr& client, const fx:

auto ServerGameState::CreateEntityFromTree(sync::NetObjEntityType type, const std::shared_ptr<sync::SyncTreeBase>& 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);
Expand Down
36 changes: 32 additions & 4 deletions code/components/citizen-server-impl/src/state/ServerSetters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,14 @@ static InitFunction initFunction([]()
auto sgs = ref->GetComponent<fx::ServerGameState>();
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)
Expand Down Expand Up @@ -426,7 +433,14 @@ static InitFunction initFunction([]()
auto sgs = ref->GetComponent<fx::ServerGameState>();
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)
Expand All @@ -450,7 +464,14 @@ static InitFunction initFunction([]()
auto sgs = ref->GetComponent<fx::ServerGameState>();
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)
Expand All @@ -474,7 +495,14 @@ static InitFunction initFunction([]()
auto sgs = ref->GetComponent<fx::ServerGameState>();
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);
});
});
});
Expand Down
2 changes: 1 addition & 1 deletion ext/native-decls/server/CreateVehicleServerSetter.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 0d108a3

Please sign in to comment.