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

[ObjCRuntime] Change Runtime.GetNSObject<T> to create a new instance if the existing one is of the wrong type. Fixes #13704. #16491

Merged
merged 1 commit into from
Oct 28, 2022
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
16 changes: 7 additions & 9 deletions src/ObjCRuntime/Runtime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1437,9 +1437,13 @@ static IntPtr CreateNSObject (IntPtr type_gchandle, IntPtr handle, NSObject.Flag
return null;

var obj = TryGetNSObject (ptr, evenInFinalizerQueue: false);
T? o;

if (obj is null) {
// First check if we got an object of the expected type
if (obj is T o)
return o;
mattleibow marked this conversation as resolved.
Show resolved Hide resolved

// We either didn't find an object, or it was of the wrong type, so we need to create a new instance.

// Try to get the managed type that correspond to this exact native type
IntPtr p = Class.GetClassForObject (ptr);
// If unknown then we'll get the Class that Lookup to NSObject even if this is not NSObject.
Expand All @@ -1461,14 +1465,8 @@ static IntPtr CreateNSObject (IntPtr type_gchandle, IntPtr handle, NSObject.Flag
} else {
target_type = typeof(NSObject);
}
o = ConstructNSObject<T> (ptr, target_type, MissingCtorResolution.ThrowConstructor1NotFound);
} else {
o = obj as T;
if (o is null)
throw new InvalidCastException ($"Unable to cast object of type '{obj.GetType ().FullName}' to type '{typeof(T).FullName}'.");
}

return o;
return ConstructNSObject<T> (ptr, target_type, MissingCtorResolution.ThrowConstructor1NotFound);
}

static public T? GetNSObject<T> (IntPtr ptr, bool owns) where T : NSObject
Expand Down
5 changes: 3 additions & 2 deletions tests/monotouch-test/ObjCRuntime/RegistrarTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2172,8 +2172,9 @@ public void TestCtors ()
// sure this is by design or by accident, but that's how we're behaving right now at least
ptr = Messaging.IntPtr_objc_msgSend (Class.GetHandle (typeof (D2)), Selector.GetHandle ("alloc"));
ptr = Messaging.IntPtr_objc_msgSend_long (ptr, Selector.GetHandle ("initWithBar:"), 2);
// Unable to cast object of type 'AppDelegate+D1' to type 'AppDelegate+D2'
Assert.Throws<InvalidCastException> (() => Runtime.GetNSObject<D2> (ptr), "b ex");
// Failed to marshal the Objective-C object 0x60000230a410 (type: MonoTouchFixtures_ObjCRuntime_RegistrarTest_D2). Could not find an existing managed instance for this object, nor was it possible to create a new managed instance (because the type 'MonoTouchFixtures.ObjCRuntime.RegistrarTest+D2' does not have a constructor that takes one NativeHandle argument
var ex = Assert.Throws<RuntimeException> (() => Runtime.GetNSObject<D2> (ptr), "b ex");
Assert.That (ex.Message, Does.Contain ("Could not find an existing managed instance for this object, nor was it possible to create a new managed instance (because the type 'MonoTouchFixtures.ObjCRuntime.RegistrarTest+D2' does not have a constructor that takes one"), "Exception message");
var obj = Runtime.GetNSObject<D1> (ptr);
Assert.AreEqual ("bar", obj.ctor1, "b ctor1");
} finally {
Expand Down
17 changes: 17 additions & 0 deletions tests/monotouch-test/ObjCRuntime/RuntimeTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,23 @@ public void GetNSObject_Posing_Class ()
}
}

[Test]
public void GetNSObject_T_SameHandleDifferentInstances ()
{
using var a = new NSDictionary<NSString, NSObject> ();
using var b = new NSDictionary<NSString, NSString> ();
using var c = new NSDictionary ();

if (a.Handle != b.Handle || a.Handle != c.Handle)
Assert.Ignore ("The dictionaries are actually different");

var handle = a.Handle;

Assert.DoesNotThrow (() => Runtime.GetNSObject<NSDictionary> (handle), "A");
Assert.DoesNotThrow (() => Runtime.GetNSObject<NSDictionary<NSString, NSObject>> (handle), "B");
Assert.DoesNotThrow (() => Runtime.GetNSObject<NSDictionary<NSString, NSString>> (handle), "C");
}

[Test]
public void UsableUntilDead ()
{
Expand Down