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

Implicit conversion from NSUrl? to Uri? crashes on null value #14786

Closed
J-Swift opened this issue Apr 20, 2022 · 2 comments · Fixed by #14860
Closed

Implicit conversion from NSUrl? to Uri? crashes on null value #14786

J-Swift opened this issue Apr 20, 2022 · 2 comments · Fixed by #14860
Assignees
Labels
bug If an issue is a bug or a pull request a bug fix
Milestone

Comments

@J-Swift
Copy link
Contributor

J-Swift commented Apr 20, 2022

Steps to Reproduce

  1. Create a Swift class that has a nullable URL property:
@objc(Foo)
public class Foo : NSObject {
    @objc
    public var imageUrl: URL?
    
    @objc
    public init(url: URL?)
    {
        self.imageUrl = url
        super.init()
    }
}
  1. Generate bindings
[BaseType (typeof(NSObject))]
[DisableDefaultCtor]
interface Foo
{
    // @property (copy, nonatomic) NSURL * _Nullable imageUrl;
    [NullAllowed, Export ("imageUrl", ArgumentSemantic.Copy)]
    NSUrl ImageUrl { get; set; }

    // -(instancetype _Nonnull)initWithUrl:(NSURL * _Nullable)url __attribute__((objc_designated_initializer));
    [Export ("initWithUrl:")]
    [DesignatedInitializer]
    IntPtr Constructor ([NullAllowed] NSUrl url);
}
  1. Try to convert to URI? implicitly
var foo = new Foo(null);
Uri? imageUri = foo.ImageUrl;

Expected Behavior

imageUri should be null since it is nullable

Actual Behavior

Crash:

System.NullReferenceException: Object reference not set to an instance of an object.
   at Foundation.NSUrl.op_Implicit(NSUrl url)

I have to fake out the compiler to get it to work, otherwise the implicit conversion kicks in

Uri? uri;
if (foo.ImageUrl == null)
{
    uri = null;
}
else
{
    uri = foo.ImageUrl;
}

Environment

I'm running this on a Mac through dotnet CLI and VSCode since its a MAUI app

Build Logs

Example Project (If Possible)

@chamons chamons added this to the Future milestone Apr 21, 2022
@chamons chamons added the bug If an issue is a bug or a pull request a bug fix label Apr 21, 2022
@chamons
Copy link
Contributor

chamons commented Apr 21, 2022

Looks like our implicit operator should return null here.

@chamons
Copy link
Contributor

chamons commented Apr 29, 2022

I can reproduce this with:

			global::System.Uri uri = null;
			NSUrl sUrl = uri;

chamons added a commit to chamons/xamarin-macios that referenced this issue Apr 29, 2022
@chamons chamons self-assigned this Apr 29, 2022
@ghost ghost locked as resolved and limited conversation to collaborators Jun 6, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug If an issue is a bug or a pull request a bug fix
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants