Skip to content

UnitDefinition::areIdentical no longer leaks its temporary copies (#503) - #504

Merged
fbergmann merged 5 commits into
sbmlteam:developmentfrom
matthiaskoenig:fix-areidentical-leak
Nov 3, 2026
Merged

fbergmann merged 5 commits into
sbmlteam:developmentfrom
matthiaskoenig:fix-areidentical-leak

Conversation

@matthiaskoenig

Copy link
Copy Markdown
Contributor

Fixes #503.

UnitDefinition::areIdentical copies both unit definitions to simplify and reorder them, on the heap, and deletes the copies at the end. The early return identical; for overall multipliers which differ skipped the deletes, so every call which compares unit definitions with different multipliers leaked both copies with their units (about 11 kB per call). As suggested in the issue, the copies are allocated on the stack now, so every return releases them; the logic is unchanged.

Check (a C++ reproduction of the issue, linked against the shared library built from development and from this branch, under valgrind --leak-check=full, 100 iterations of areIdentical(mass, rate) with different multipliers and areIdentical(rate, rate2) with equal overall multipliers):

result valgrind
development 100 of 200 identical definitely lost 451,200 bytes in 200 blocks, indirectly lost 640,000 bytes
this branch 100 of 200 identical all heap blocks were freed, no leaks are possible

areEquivalent and areIdenticalSIUnits already delete their copies on every return.

The copies of the two unit definitions were allocated on the heap and
deleted at the end of the function, but the early return for overall
multipliers which differ skipped the deletes, so every such call leaked
both copies (about 11 kB). The copies are on the stack now and released on
every return. Fixes sbmlteam#503.

@fbergmann fbergmann left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@matthiaskoenig thank you so much for the PR, this solves the issue.

the test failures are unrelated to this issue, I'll leave this PR open, so I can add the necessary commits and merge at the end of the week with those issues solved.

Do you need a new release right away?

@matthiaskoenig

Copy link
Copy Markdown
Contributor Author

It would be great to have a release as soon as possible (but this is not blocking anything at the moment).

You and Sarah fixed a ton of issues in the last week for which I have a lot of work around code in my code base (e.g. I have a own areIdentical for units in python, but calling this 100.000 of times which is very slow but I cannot afford the memory loss ;).

I would really like to get rid of all the unnecessary code and checks on my side, because things have been fixed upstream in libsbml.

Best

@fbergmann

Copy link
Copy Markdown
Member

@matthiaskoenig I'll make it a priority and aim for a release Wed, Thursday at the latest.

@skeating that would be fine with you, right?

@fbergmann
fbergmann merged commit 67d23bb into sbmlteam:development Nov 3, 2026
25 checks passed
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.

UnitDefinition.areIdentical leaks memory for units with a multiplier other than 1

2 participants