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

[generator] Incorrectly generated code inside Trampolines.g.cs #12618

Open
spouliot opened this issue Sep 2, 2021 · 2 comments
Open

[generator] Incorrectly generated code inside Trampolines.g.cs #12618

spouliot opened this issue Sep 2, 2021 · 2 comments
Labels
bug If an issue is a bug or a pull request a bug fix iOS Issues affecting Xamarin.iOS macOS Issues affecting Xamarin.Mac
Milestone

Comments

@spouliot
Copy link
Contributor

spouliot commented Sep 2, 2021

Looking into the generated Trampolines.g.cs files we can spot some code patterns that looks like:

return NSArray.FromNSObjects(retval).Handle;

This is incorrect as the GC can dispose of the managed NSArray before the Handle is used again.

If unlucky (the window of opportunity is small but exist) that can dealloc the native NSArray before the Handle is used again.

See also: #10146

@spouliot spouliot added bug If an issue is a bug or a pull request a bug fix macOS Issues affecting Xamarin.Mac iOS Issues affecting Xamarin.iOS labels Sep 2, 2021
@spouliot spouliot added this to the Future milestone Sep 2, 2021
@rolfbjarne
Copy link
Member

One potential solution would be to retain/autorelease the return value:

var retobj = NSArray.FromNSObjects(retval);
retobj.DangerousRetain ();
retobj.DangerousAutorelease ();
return retobj.Handle;

@spouliot
Copy link
Contributor Author

spouliot commented Sep 7, 2021

That's simpler than what I had in mind [0] but I'd be worried about a tight-loop calling that trampoline.

[0] ideally we would avoid a managed NSArray to be created since we're returning an handle anyway

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug If an issue is a bug or a pull request a bug fix iOS Issues affecting Xamarin.iOS macOS Issues affecting Xamarin.Mac
Projects
None yet
Development

No branches or pull requests

2 participants