-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Object: Do not toggle reference after handle was already freed
It can happen that calling "RemoveToggleRef" leads to a delayed invocation of it's toggle notify callback. If "RemoveToggleRef" is called the garbage collector is free to free the ToggleRef in which case the managed "ToggleNotify" callback is freed, too. This change uses an "UnmanagedCallersOnly" function which is out of scope of the GC and can always be called. In case it is called after the ToggleRef was freed it just does nothing. Fixes: #1125
- Loading branch information
Showing
3 changed files
with
70 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
src/Libs/GObject-2.0/Internal/ObjectMapper.ToggleRegistration.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
using System; | ||
using System.Runtime.InteropServices; | ||
|
||
namespace GObject.Internal; | ||
|
||
public partial class ObjectMapper | ||
{ | ||
private static unsafe class ToggleRegistration | ||
{ | ||
internal static void AddToggleRef(IntPtr handle) | ||
{ | ||
AddToggleRef(handle, &ToggleNotify, IntPtr.Zero); | ||
Internal.Object.Unref(handle); | ||
} | ||
|
||
internal static void RemoveToggleRef(IntPtr handle) | ||
{ | ||
var sourceFunc = new GLib.Internal.SourceFuncAsyncHandler(() => | ||
{ | ||
RemoveToggleRef(handle, &ToggleNotify, IntPtr.Zero); | ||
return false; | ||
}); | ||
GLib.Internal.MainContext.Invoke(GLib.Internal.MainContextUnownedHandle.NullHandle, sourceFunc.NativeCallback, IntPtr.Zero); | ||
} | ||
|
||
[DllImport(ImportResolver.Library, EntryPoint = "g_object_add_toggle_ref")] | ||
private static extern void AddToggleRef(IntPtr @object, delegate* unmanaged<IntPtr, IntPtr, int, void> toggleNotify, IntPtr data); | ||
|
||
[DllImport(ImportResolver.Library, EntryPoint = "g_object_remove_toggle_ref")] | ||
private static extern void RemoveToggleRef(IntPtr @object, delegate* unmanaged<IntPtr, IntPtr, int, void> toggleNotify, IntPtr data); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters