Skip to content

Memory leaks in Rust implementation #709

Open

Description

@ogghostjelly

Circular references using Rc in Rust causes memory leaks! An environment can reference a function and a function can reference an environment, creating a circular reference. Possible fixes for the issue could be:

  • Cloning the outer environment when a function is created instead of keeping a reference to it, this could causes issues with referencing variables before assignment e.g if the environment gets mutated the functions own copy of the environment won't be changed.
  • Using a Weak reference instead of Rc, this could cause complications with function currying and referencing environments that have been dropped.
  • Implementing a garbage collector. The dumpster crate looks promising as a drop-in replacement for Rc.

I have already added the cloning outer reference method into my own mal rust implementation.

Or since the impls are mainly there for demonstration purposes it might not be worth the added complexity of fixing this issue.

Activity

  1. asarhaddon commented on Aug 26, 2026

    @asarhaddon
    Contributor

    Cloning the outer environment does not implement the desired behaviour, see #677.

    Weak references could maybe fix variants of (let* [f (fn* [] nil)] nil) but probably not variants of (let* [a (atom nil) l (list a)] (reset! a l)).

  2. asarhaddon commented on Aug 28, 2026

    @asarhaddon
    Contributor

    Merge request #742 fixes this issue for rust.
    The guide should definitively warn implementors against these two non-solutions #739.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions