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 clear-smoke memory leaks #1203

Merged
merged 6 commits into from
Jun 24, 2024
Merged

Conversation

Crystalwarrior
Copy link
Contributor

Delete the instances of smoke that are already erased from the flows vectors

thanks @Bumber64 for the help!

@Crystalwarrior
Copy link
Contributor Author

Could potentially expand this script to be general-purpose clear-flows at a later date

clear-smoke.lua Outdated
flows:erase(i)
flows_to_delete[flow] = true
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why can't this be deleted inline? other tools successfully do it that way, e.g. https://github.com/DFHack/dfhack/blob/develop/plugins/lua/dwarfvet.lua#L129-L137

@Bumber64
Copy link
Contributor

Bumber64 commented Jun 23, 2024

Given Discord discussion, the script should now be something like this:

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 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

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

clearSmoke()

I think we could probably turn this into a @module to allow the removeFlow functions to be reused elsewhere. Otherwise, we could just implement those in Maps and add them to the Lua API.

add to module
clear-smoke.lua Outdated
end

clearSmoke()
Copy link
Contributor

@Bumber64 Bumber64 Jun 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be

if not dfhack_flags.module then
    clearSmoke()
end

so it doesn't run when used as a module.

Also, there's an issue with your end of file. Post a comment on this PR that says pre-commit.ci autofix to fix it.

@myk002
Copy link
Member

myk002 commented Jun 24, 2024

pre-commit.ci autofix

clear-smoke.lua Outdated Show resolved Hide resolved
@myk002 myk002 merged commit e9dc66e into DFHack:master Jun 24, 2024
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
No open projects
Status: Done
Development

Successfully merging this pull request may close these issues.

3 participants