Skip to content
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
22 changes: 14 additions & 8 deletions src/ObjCRuntime/Runtime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1250,9 +1250,8 @@ internal static void NativeObjectHasDied (IntPtr ptr, NSObject? managed_obj)

// Completes deferred object_map registration (issue #25861): when 'onlyIfNeeded' is
// true, registers the object only if the pointer isn't already present, and leaves
// any existing entry untouched. This avoids redundantly re-registering objects that
// were registered eagerly (e.g. direct bindings), and avoids clobbering a concurrent
// registration (e.g. another object reusing a freed native pointer).
// any live existing entry untouched. Dead entries are removed so they don't prevent
// registration of a new object for a reused native pointer.
internal static void RegisterNSObject (NSObject obj, IntPtr ptr, bool onlyIfNeeded = false)
{
GCHandle handle;
Expand All @@ -1264,11 +1263,18 @@ internal static void RegisterNSObject (NSObject obj, IntPtr ptr, bool onlyIfNeed

lock (lock_obj) {
if (onlyIfNeeded) {
if (object_map.ContainsKey (ptr)) {
// Already registered; don't touch the existing entry, just free the
// handle we speculatively allocated.
handle.Free ();
return;
if (object_map.TryGetValue (ptr, out var existing)) {
if (existing.Target is not null) {
// Already registered; don't touch the existing entry, just free
// the handle we speculatively allocated.
handle.Free ();
return;
}

// The weak target was collected without the native object dying.
// Remove the stale entry before registering the replacement.
object_map.Remove (ptr);
existing.Free ();
}
} else {
if (object_map.Remove (ptr, out var existing))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2570,7 +2570,6 @@ System.Private.CoreLib.dll:System.Collections.Generic.Dictionary`2..ctor(System.
System.Private.CoreLib.dll:System.Collections.Generic.Dictionary`2..ctor(System.Int32, System.Collections.Generic.IEqualityComparer`1<TKey>)
System.Private.CoreLib.dll:System.Collections.Generic.Dictionary`2..ctor(System.Int32)
System.Private.CoreLib.dll:System.Collections.Generic.Dictionary`2.Add(TKey, TValue)
System.Private.CoreLib.dll:System.Collections.Generic.Dictionary`2.ContainsKey(TKey)
System.Private.CoreLib.dll:System.Collections.Generic.Dictionary`2.CopyTo(System.Collections.Generic.KeyValuePair`2<TKey,TValue>[], System.Int32)
System.Private.CoreLib.dll:System.Collections.Generic.Dictionary`2.FindValue(TKey)
System.Private.CoreLib.dll:System.Collections.Generic.Dictionary`2.get_Count()
Expand Down
12 changes: 6 additions & 6 deletions tests/dotnet/UnitTests/expected/TVOS-MonoVM-size.txt
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
AppBundleSize: 9,918,610 bytes (9,686.1 KB = 9.5 MB)
AppBundleSize: 9,917,728 bytes (9,685.3 KB = 9.5 MB)
# The following list of files and their sizes is just informational / for review, and isn't used in the test:
aot-instances.aotdata.arm64:
900,304 bytes (879.2 KB = 0.9 MB)
899,704 bytes (878.6 KB = 0.9 MB)
Info.plist:
1,105 bytes (1.1 KB = 0.0 MB)
1,127 bytes (1.1 KB = 0.0 MB)
Microsoft.tvOS.aotdata.arm64:
23,784 bytes (23.2 KB = 0.0 MB)
23,760 bytes (23.2 KB = 0.0 MB)
Microsoft.tvOS.dll:
49,664 bytes (48.5 KB = 0.0 MB)
PkgInfo:
8 bytes (0.0 KB = 0.0 MB)
runtimeconfig.bin:
1,481 bytes (1.4 KB = 0.0 MB)
SizeTestApp:
7,632,040 bytes (7,453.2 KB = 7.3 MB)
7,632,024 bytes (7,453.1 KB = 7.3 MB)
SizeTestApp.aotdata.arm64:
1,464 bytes (1.4 KB = 0.0 MB)
SizeTestApp.dll:
7,168 bytes (7.0 KB = 0.0 MB)
System.Private.CoreLib.aotdata.arm64:
721,448 bytes (704.5 KB = 0.7 MB)
721,184 bytes (704.3 KB = 0.7 MB)
System.Private.CoreLib.dll:
568,832 bytes (555.5 KB = 0.5 MB)
System.Runtime.aotdata.arm64:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2570,7 +2570,6 @@ System.Private.CoreLib.dll:System.Collections.Generic.Dictionary`2..ctor(System.
System.Private.CoreLib.dll:System.Collections.Generic.Dictionary`2..ctor(System.Int32, System.Collections.Generic.IEqualityComparer`1<TKey>)
System.Private.CoreLib.dll:System.Collections.Generic.Dictionary`2..ctor(System.Int32)
System.Private.CoreLib.dll:System.Collections.Generic.Dictionary`2.Add(TKey, TValue)
System.Private.CoreLib.dll:System.Collections.Generic.Dictionary`2.ContainsKey(TKey)
System.Private.CoreLib.dll:System.Collections.Generic.Dictionary`2.CopyTo(System.Collections.Generic.KeyValuePair`2<TKey,TValue>[], System.Int32)
System.Private.CoreLib.dll:System.Collections.Generic.Dictionary`2.FindValue(TKey)
System.Private.CoreLib.dll:System.Collections.Generic.Dictionary`2.get_Count()
Expand Down