Skip to content

All empty NSDictionary instances have the same underlying native handle #13704

@rolfbjarne

Description

@rolfbjarne

Example code:

var a = new NSDictionary<NSString, NSObject> ();
var b = new NSDictionary<NSString, NSString> ();
var c = new NSDictionary ();
Console.WriteLine ($"a: 0x{a.Handle:X}");
Console.WriteLine ($"b: 0x{b.Handle:X}");
Console.WriteLine ($"c: 0x{c.Handle:X}");

prints the same handle for both instances:

a: 0x0x7fff80821080
b: 0x0x7fff80821080
c: 0x0x7fff80821080

This is unfortunately a problem.

Say you have a type with two properties:

NSDictionary<NSString, NSObject> PropertyA { get; set; }
NSDictionary<NSString, NSString> PropertyB { get; set; }

and native code has set them both to an empty NSDictionary, then fetching one will make the other break, because this happens:

Unable to cast object of type 'Foundation.NSDictionary`2[[Foundation.NSString, Xamarin.MacCatalyst, Version=0.0.0.0, Culture=neutral, PublicKeyToken=84e04ff9cfb79065],[Foundation.NSObject, Xamarin.MacCatalyst, Version=0.0.0.0, Culture=neutral, PublicKeyToken=84e04ff9cfb79065]]' to type 'Foundation.NSDictionary`2[[Foundation.NSString, Xamarin.MacCatalyst, Version=0.0.0.0, Culture=neutral, PublicKeyToken=84e04ff9cfb79065],[Foundation.NSString, Xamarin.MacCatalyst, Version=0.0.0.0, Culture=neutral, PublicKeyToken=84e04ff9cfb79065]]'

The generic versions of NSArray, NSSet and NSOrderedSet probably have the same problem.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugIf an issue is a bug or a pull request a bug fix

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions