Skip to content

Commit

Permalink
Fix deinitialize called more than once (#1647)
Browse files Browse the repository at this point in the history
* Fix deinitialize called more than once

* Also set instance to nil
  • Loading branch information
thegrb93 authored Feb 28, 2024
1 parent b94e25b commit bf6248a
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 16 deletions.
7 changes: 3 additions & 4 deletions lua/entities/starfall_processor/cl_init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,15 @@ function ENT:Initialize()
end

function ENT:OnRemove()
if self.instance then
self.instance:runScriptHook("removed")
end

-- This is required because snapshots can cause OnRemove to run even if it wasn't removed.
local instance = self.instance
if instance then
instance:runScriptHook("removed")

timer.Simple(0, function()
if not self:IsValid() then
instance:deinitialize()
self.instance = nil
end
end)
end
Expand Down
12 changes: 5 additions & 7 deletions lua/entities/starfall_processor/shared.lua
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,11 @@ function ENT:Compile()
end

function ENT:Destroy()
if self.instance then
self.instance:runScriptHook("removed")
--removed hook can cause instance to become nil
if self.instance then
self.instance:deinitialize()
self.instance = nil
end
local instance = self.instance
if instance then
instance:runScriptHook("removed")
instance:deinitialize()
self.instance = nil
end
end

Expand Down
12 changes: 7 additions & 5 deletions lua/starfall/instance.lua
Original file line number Diff line number Diff line change
Expand Up @@ -624,7 +624,6 @@ end
-- @return True if it executed ok, false if not or if there was no hook
-- @return If the first return value is false then the error message or nil if no hook was registered
function SF.Instance:runScriptHook(hook, ...)
if self.error then return {} end
local hooks = self.hooks[hook]
if not hooks then return {} end
local tbl
Expand All @@ -646,7 +645,6 @@ end
-- @return If the first return value is false then the error message or nil if no hook was registered. Else any values that the hook returned.
-- @return The traceback if the instance errored
function SF.Instance:runScriptHookForResult(hook, ...)
if self.error then return {} end
local hooks = self.hooks[hook]
if not hooks then return {} end
local tbl
Expand All @@ -671,8 +669,6 @@ end
-- @param func Function to run
-- @param ... Arguments to pass to func
function SF.Instance:runFunction(func, ...)
if self.error then return {} end

local tbl = self:run(func, ...)
if not tbl[1] then
tbl[2].message = "Callback errored with: " .. tbl[2].message
Expand Down Expand Up @@ -709,7 +705,14 @@ function SF.Instance:deinitialize()
SF.playerInstances[self.player] = nil
end
end

self.error = true
local noop = function() end
self.runScriptHook = noop
self.runScriptHookForResult = noop
self.runFunction = noop
self.deinitialize = noop
self.Error = noop
end

hook.Add("Think", "SF_Think", function()
Expand Down Expand Up @@ -767,7 +770,6 @@ end)

--- Errors the instance. Should only be called from the tips of the call tree (aka from places such as the hook library, timer library, the entity's think function, etc)
function SF.Instance:Error(err)
if self.error then return end
if self.runOnError then -- We have a custom error function, use that instead
self.runOnError(err)
else
Expand Down

0 comments on commit bf6248a

Please sign in to comment.