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(server/gamestate): memory leak #3063

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading