Commit 9758d7eb authored by Martin Finkel's avatar Martin Finkel

Fix #5: Keep delegate ref before passing to native code to prevent GC collection

parent c786e7d7
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Security;
......@@ -20,6 +21,7 @@ namespace LibVLCSharp.Shared
}
public IntPtr NativeReference;
readonly List<EventCallback> _callbacks = new List<EventCallback>();
protected EventManager(IntPtr ptr)
{
......@@ -31,12 +33,18 @@ namespace LibVLCSharp.Shared
protected void AttachEvent(EventType eventType, EventCallback eventCallback)
{
if(Internal.LibVLCEventAttach(NativeReference, eventType, eventCallback, IntPtr.Zero) != 0)
_callbacks.Add(eventCallback);
if (Internal.LibVLCEventAttach(NativeReference, eventType, eventCallback, IntPtr.Zero) != 0)
{
_callbacks.Remove(eventCallback);
throw new VLCException($"Could not attach event {eventType}");
}
}
protected void DetachEvent(EventType eventType, EventCallback eventCallback)
{
_callbacks.Remove(eventCallback);
Internal.LibVLCEventDetach(NativeReference, eventType, eventCallback, IntPtr.Zero);
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment