Skip to content

C# objects need to be collected and finalised on Host finalisation #1

Description

@GooberMan

The basic flow of memory usage in C# goes like this:

  • Allocate a Host object
  • Call a function which creates an ImportedFunction object stored in a static variale; or allocate a bound object which creates an ImportedClass object
  • Do your thing
  • Close your program
  • Garbage collector finalises whatever it wants first, usually not in reverse order of allocation
  • Host is destroyed somewhere, which kills all C++ side allocations and things like memory alloc/dealloc function pointers
  • ImportedFunction/Class gets finalised, and accesses bad memory

This is clearly rubbish.

The solution is to force all ImportedFunction/Class objects to finalise when the host finalises, before binderoo_host_destroy is called. The current plan for the solution is to add each ImportedFunction/Class to a WeakReference list inside the Host. This will require making the Host object a singleton - which it is in C++ code anyway so no problem there. When the Host finalises, resolving those weak references and manually invoking Dispose on them will clean up all required C++ handles before the host itself gets finalised.

Activity

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

    .NETPertaining to .NET supportbinderoo_hostSpecifically for the binderoo_host modulebugSomething isn't working

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions