Skip to content

Fix memory leaks and bad frees - #180

Merged
xiaoyeli merged 5 commits into
xiaoyeli:masterfrom
gruenich:feature/fix-memory-leaks-and-bad-free
Oct 18, 2026
Merged

xiaoyeli merged 5 commits into
xiaoyeli:masterfrom
gruenich:feature/fix-memory-leaks-and-bad-free

Conversation

@gruenich

Copy link
Copy Markdown
Contributor

Claude Code is amazing. I reworked the suggestions to follow SuperLU's style and make logical commits to have an easier review.

When the factorization reported a singular matrix (info > 0) or only a
memory estimate was requested (lwork == -1), ?gssvx returned
without freeing the permuted matrix AC from sp_preorder() and, for
SLU_NR input, the temporary column-oriented copy AA. ?gsisx had
the same leaks for lwork == -1 and for info > 0 with PivotGrowth set,
and additionally leaked the MC64 permutation for lwork == -1.
* ?gssvx with Fact = DOFACT or SamePattern computes new factors
  on every call, but the factors from the previous transpose option
  were never freed.
* ?gssv always allocates the factors itself, but they were only
  freed when lwork == 0.
* With a user-supplied work array (lwork > 0) only the L and U matrix
  stores of the final factorization were freed.

Free the factors after every factorization; for lwork > 0 only the
matrix stores, as the factors themselves are stored in work[].
With lwork > 0 (option -l), the factors are stored in the user's work
array. After the first solve, ?linsolx2 nevertheless called
Destroy_SuperNode_Matrix() and Destroy_CompCol_Matrix() for lwork >= 0,
freeing pointers into work[] that were not obtained from malloc.
Only destroy the factors when SuperLU allocated them (lwork == 0).
With lwork > 0 (option -l), the factors are stored in the user's work
array, but the L and U matrix stores are still allocated by SuperLU.
The ?linsolx* examples freed work[] but leaked the stores.
@xiaoyeli
xiaoyeli merged commit 449d79d into xiaoyeli:master Oct 18, 2026
5 checks passed
@gruenich
gruenich deleted the feature/fix-memory-leaks-and-bad-free branch October 18, 2026 17:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants