Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Garbage Collection of Delegates #106

Merged
merged 2 commits into from
Nov 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions Libs/GObject/Classes/ClosureHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ internal class ClosureHelper : IDisposable

private static readonly Dictionary<Delegate, ClosureHelper> Handlers = new Dictionary<Delegate, ClosureHelper>();

// We need to store a reference to MarshalCallback to
// prevent the delegate from being collected by the GC
private readonly ClosureMarshal _marshalCallback;

private bool _disposedValue;
private readonly Action? _callback;
private readonly ActionRefValues? _callbackRefValues;
Expand Down Expand Up @@ -41,7 +45,8 @@ public ClosureHelper(Object obj, ActionRefValues callbackRefValues) : this(obj)
private ClosureHelper(Object obj)
{
Handle = Closure.Native.new_object((uint) Marshal.SizeOf(typeof(Closure)), obj.Handle);
Closure.Native.set_marshal(Handle, MarshalCallback);
_marshalCallback = MarshalCallback;
Closure.Native.set_marshal(Handle, _marshalCallback);
}

~ClosureHelper() => Dispose(false);
Expand All @@ -50,12 +55,12 @@ private ClosureHelper(Object obj)

#region Methods

private void MarshalCallback(IntPtr closure, ref Value return_value, uint n_param_values,
Value[] param_values, IntPtr invocation_hint, IntPtr marshal_data)
private void MarshalCallback(IntPtr closure, ref Value returnValue, uint nParamValues,
Value[] paramValues, IntPtr invocationHint, IntPtr marshalData)
{
_callback?.Invoke();

_callbackRefValues?.Invoke(ref param_values);
_callbackRefValues?.Invoke(ref paramValues);
}

public static bool TryGetByDelegate(Action action, out ClosureHelper closure)
Expand Down
14 changes: 10 additions & 4 deletions Libs/GObject/Classes/Object.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ public partial class Object : INotifyPropertyChanged, IDisposable
#region Properties

protected internal IntPtr Handle { get; private set; }

// We need to store a reference to WeakNotify to
// prevent the delegate from being collected by the GC
private WeakNotify? _onFinalized;

protected bool Disposed { get; private set; }

Expand Down Expand Up @@ -135,7 +139,11 @@ protected virtual void Initialize() { }

// Modify this in the future to play nicely with virtual function support?
private void OnFinalized(IntPtr data, IntPtr where_the_object_was) => Dispose();
private void RegisterOnFinalized() => Native.weak_ref(Handle, OnFinalized, IntPtr.Zero);
private void RegisterOnFinalized()
{
_onFinalized = OnFinalized;
Native.weak_ref(Handle, _onFinalized, IntPtr.Zero);
}

private void RegisterProperties()
{
Expand Down Expand Up @@ -182,9 +190,7 @@ private void RegisterEvent(string eventName, ClosureHelper closure, bool after)
if (ret == 0)
throw new Exception($"Could not connect to event {eventName}");

// Add to our closures list so the callback
// doesn't get garbage collected.
// closures.Add(closure);
// Add to our closures list so the callback doesn't get garbage collected.
Closures[closure] = ret;
}

Expand Down