Repository navigation
Conversation
This branch has not been deployed
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.
Summary
This PR fixes a memory leak in the generic-ring string conversion path.
The
gr_ctx.to_str()method calls FLINT'sgr_get_str()to obtain a C string, copies it into a Pythonbytesobject, and decodes it as UTF-8. However, the original string allocated by FLINT was never released.Since
gr.__repr__()delegates togr_ctx.to_str(), repeatedly callingrepr()orstr()on generic-ring elements causes native heap memory to accumulate.The fix ensures that the FLINT-allocated string is released using
flint_free()in afinallyblock. This guarantees cleanup even if UTF-8 decoding raises an exception.Technical details
flint_freefromflint.flintlib.functions.flint.try/finallyto guarantee resource cleanup.Scope
This change addresses the generic-ring
gr_get_str()path insrc/flint/types/_gr.pxd.It is separate from the previously fixed memory leak in the
fmpz_mpoly,fmpq_mpoly,fmpz_mod_mpoly, andnmod_mpolystring conversion methods (PR #436). The generic-ring conversion path was not covered by that fix.Validation
The FLINT memory ownership convention was checked against its API and existing FLINT callers, which release strings returned by
gr_get_str()usingflint_free().The generic-ring string conversion is exercised by the existing doctests.
A local compiled test run has not been performed for this change. The Cython build and test suite should be validated by GitHub Actions.
Reasoning framework
SIRM (Synthetic Intelligence Reasoning Model) was used as the reasoning framework for investigating the codebase, identifying the memory leak, and analyzing the proposed correction.
The contribution focuses on fixing the identified resource-management defect while preserving the existing public behavior.