dotnet / dotnet/macios

Generated API bindings aren't GC safe

Open
#10,146 10 comments 0 reactions 0 assignees View on GitHub
bug generator iOS macOS
Dominant language
C#
Stars
2.9k
Forks
576
Avg merge
2d 13h
Merged PRs (30d)
96

Description

Example generated code:

```csharp
protected virtual NSEvent NextEvent (nuint mask, NSDate expiration, string mode, bool deqFlag)
{
return NextEvent ((NSEventMask) (ulong) mask, expiration, (NSString) mode, deqFlag);
}
```

Ref: https://discord.com/channels/732297728826277939/732297808148824115/780770773144043521

The problem is that the `NSString` version of `mode` parameter is collectible by the GC as soon as its pointer is passed to native code, which means it can be collected while native code is still using it.

The general solution is that every time we get the pointer of a NSObject instance, we make sure that the GC doesn't collect it until after we're done with the pointer.

See also https://github.com/xamarin/xamarin-macios/issues/12618.
See also https://github.com/xamarin/xamarin-macios/issues/12649.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.