Repository navigation
fix: free model backend, buffer and context when the model is dropped - #23
Merged
Merged
Conversation
sam3_load_model returned std::make_shared<sam3_model>(), and sam3_model has no destructor. Nothing ever called sam3_free_model, so each loaded model leaked its weight buffer, ggml context and backend (~42 MB per load for SAM2.1-tiny q8_0, on CPU and Metal). It also leaked on every early-return failure path inside the loader. Give the shared_ptr a deleter that calls sam3_free_model. The function nulls what it frees, so an explicit call before release stays safe. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01M2qLmQ9ag49hT61V6i9qAq
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Bug
sam3_load_modelreturnsstd::make_shared<sam3_model>(), andsam3_modelhas no destructor. Nothing in the library or examples callssam3_free_model, so dropping the lastshared_ptrleaked the weight buffer, the ggml context and the backend. The loader's earlyreturn nullptrpaths (after the backend/buffer were created) leaked the same way.Fix
Construct the
shared_ptrwith a deleter that callssam3_free_modeland thendeletes the model.sam3_free_modelnulls every pointer it frees, so a caller that already freed the model manually is unaffected.Lifetimes are safe: every example declares
modelbeforestate/tracker, so their buffers are released before the backend.Verification
A throwaway program loads
sam2.1_hiera_tiny_q8_0plus a state 5 times, drops them each time, and prints resident memory:Output is also unchanged: a parity tool runs encode, PVS point + box, and a 3-frame visual track, then compares the output byte for byte with
origin/main:A failing load (missing file) still returns
nullptrcleanly.🤖 Generated with Claude Code
https://claude.ai/code/session_01M2qLmQ9ag49hT61V6i9qAq