Skip to content

Conversation

@jkylling
Copy link

By using recursive_2 or recursive_fold instead of Recursive::declare we can prevent situations where we either keep too many strong references around, leading to memory leaks, or too few strong references around, leading to broken parsers because all the strong references and the parsers are dropped.

We could consider removing Recursive::declare from the public API, since it is hard to use it correctly.

Please see the investigation (for version 0.9) in #486 (comment)

If code is migrated to use recursive_2 or recursive_fold we should be able to resolve #486 I have tested that this patch for 0.9 fixes the memory issue on jaq when repeatedly creating parsers.

@zesterer
Copy link
Owner

zesterer commented Sep 1, 2024

How do you think this solution compares to #494 ?

@jkylling
Copy link
Author

jkylling commented Sep 2, 2024

How do you think this solution compares to #494 ?

I have not looked at #494 in detail, but I believe it allows accidentally dropping one of the parsers while keeping the other one. That would render the remaining parser unusable. The API should try to prevent accidental misuse if possible.

@zesterer zesterer force-pushed the main branch 5 times, most recently from 38bb88c to 02a1373 Compare May 26, 2025 14:11
Zij-IT added a commit to Zij-IT/chumsky that referenced this pull request Oct 27, 2025
This commit (hopefully) renders `define` and `declare` for users of the
library obselete, by adding a parser which wraps the declaration and
definition similar to `recursive`. This `recursive_n` allows for the
definition of mutually recursive parsers, resulting in a single parser.
This solves the memory leak in a fashion similar to zesterer#664 by keeping all
strong references within `RecursiveN` and dropping these all whenever the
parser returned by `recursive_n` is dropped.

Additionally, `Recursive::declare` and `Recursive::define` are removed
from the API.
@jkylling jkylling force-pushed the add-recursive-fold-main branch from db038db to f7c378e Compare November 1, 2025 11:49
@zesterer zesterer force-pushed the main branch 2 times, most recently from 0f2b61a to e350fc6 Compare November 5, 2025 19:21
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.

Memory leak in recursive when using define function

2 participants