Skip to content

Commit

Permalink
Merge pull request #1203 from Crystalwarrior/fix-smoke-memory
Browse files Browse the repository at this point in the history
Fix clear-smoke memory leaks
  • Loading branch information
myk002 authored Jun 24, 2024
2 parents c223fc0 + c07a432 commit e9dc66e
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 17 deletions.
1 change: 1 addition & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ Template for new versions:
- `quickfort`: allow farm plots to be built on muddy stone
- `suspend`: remove broken ``--onlyblocking`` option; restore functionality to ``suspend all``
- `gui/create-item`: allow creation of adamantine thread, wool, and yarn
- `clear-smoke`: fix memory leak caused by the smoke flow not being deleted from memory

## Misc Improvements
- `gui/launcher`: "space space to toggle pause" behavior is skipped if the game was paused when `gui/launcher` came up to prevent accidental unpausing
Expand Down
46 changes: 30 additions & 16 deletions clear-smoke.lua
Original file line number Diff line number Diff line change
@@ -1,25 +1,39 @@
-- Removes all smoke from the map
--@module = true

--[====[
clear-smoke
===========
Removes all smoke from the map. Note that this can leak memory and should be
used sparingly.
function removeFlow(flow) --have DF remove the flow
if not flow then
return
end
flow.flags.DEAD = true

]====]
local block = dfhack.maps.getTileBlock(flow.pos)
if block then
block.flow_pool.flags.active = true
else
df.global.world.orphaned_flow_pool.flags.active = true
end
end

function clearSmoke(flows)
for i = #flows - 1, 0, -1 do
if flows[i].type == df.flow_type.Smoke then
flows:erase(i)
function removeFlows(flow_type) --remove all if flow_type is nil
local count = 0
for _,flow in ipairs(df.global.flows) do
if not flow.flags.DEAD and (flow_type == nil or flow.type == flow_type) then
removeFlow(flow)
count = count + 1
end
end

return count
end

clearSmoke(df.global.flows)
function clearSmoke()
if dfhack.isWorldLoaded() then
print(('%d smoke flows removed.'):format(removeFlows(df.flow_type.Smoke)))
else
qerror('World not loaded!')
end
end

for _, block in pairs(df.global.world.map.map_blocks) do
clearSmoke(block.flows)
if not dfhack_flags.module then
clearSmoke()
end
1 change: 0 additions & 1 deletion docs/clear-smoke.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ clear-smoke
:summary: Removes all smoke from the map.
:tags: fort armok fps map

Note that this can leak memory and should be used sparingly.

Usage
-----
Expand Down

0 comments on commit e9dc66e

Please sign in to comment.