From ee97eba98ef78d72e60d78d2ef89c31973a78ad8 Mon Sep 17 00:00:00 2001 From: Nikola Irinchev Date: Wed, 19 Oct 2022 03:00:42 +0200 Subject: [PATCH 1/2] Fix an NRE when the session gets garbage collected while a notification listener is active --- Realm/Realm/Handles/SessionHandle.cs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Realm/Realm/Handles/SessionHandle.cs b/Realm/Realm/Handles/SessionHandle.cs index 208f839275..02136d8867 100644 --- a/Realm/Realm/Handles/SessionHandle.cs +++ b/Realm/Realm/Handles/SessionHandle.cs @@ -455,6 +455,11 @@ private static void HandleSessionPropertyChangedCallback(IntPtr managedSessionHa _ => throw new NotSupportedException($"Unexpected notifiable property value: {property}") }; var session = (Session)GCHandle.FromIntPtr(managedSessionHandle).Target; + if (session == null) + { + // We're taking a weak handle to the session, so it's possible that it's been collected + return; + } System.Threading.ThreadPool.QueueUserWorkItem(_ => { From 0441bfbc7deb485341988bb2e57206c0f52a6e38 Mon Sep 17 00:00:00 2001 From: Nikola Irinchev Date: Wed, 19 Oct 2022 15:57:35 +0200 Subject: [PATCH 2/2] Add changelog entry --- CHANGELOG.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b0adea8639..0b0dd68ef6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,13 +4,13 @@ * None ### Fixed -* None +* Fixed a NullReferenceException being thrown when subscribing to `PropertyChanged` notifications on a `Session` instance that is then garbage collected prior to unsubscribing. (PR [#3061](https://github.com/realm/realm-dotnet/pull/3061)) ### Compatibility * Realm Studio: 11.0.0 or later. ### Internal -* Using Core x.y.z. +* Using Core 12.9.0. ## 10.17.0 (2022-10-06)