Skip to content

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

Closed

Description

@spouliot

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

Activity

  1. added
    bug
    macOS
    iOS
    on Sep 2, 2021
  2. added this to the Future milestone on Sep 2, 2021
  3. rolfbjarne commented on Sep 6, 2021

    @rolfbjarne
    Member

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

    var retobj = NSArray.FromNSObjects(retval);
    retobj.DangerousRetain ();
    retobj.DangerousAutorelease ();
    return retobj.Handle;
  4. spouliot commented on Sep 7, 2021

    @spouliot
    ContributorAuthor

    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

  5. rolfbjarne commented on Apr 8, 2025

    @rolfbjarne
    Member

    This has been fixed now (4c22a30).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugiOSmacOS

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions