Cocoa binding tries to access to a GC-ed managed object and crashes
- Dominant language
- C#
- Stars
- 2.9k
- Forks
- 576
- Avg merge
- 2d 12h
- Merged PRs (30d)
- 123
Description
### Steps to Reproduce
1. Build and run example project (see below)
2. Click "Change object"
3. Wait about 2 seconds
4. Click "Change enabled"
5. You may need to replay steps 2-4 several times.
6. Application crashes.
### Expected Behavior
I do not know how exactly such a situation should be handled from the point of view of Mac development, but I think something like one of the following is expected
* No crash at all
* Meaningful managed exception
* Meaningful native crash information.
Also, the actual behaviour looks like as bindings bug as described here https://docs.microsoft.com/en-us/xamarin/mac/deploy-test/debugging-native-crash in the "Example 3: Callbacks and managed objects" part.
### Actual Behavior
Application crashed with almost meaningless stacktrace.
Application output with crash stacktrace attached.
[appOutput.txt](https://github.com/xamarin/xamarin-macios/files/4683745/appOutput.txt)
### Additional information
The example project use NSArray VDPropertyGridView.Properties as datasource for NSTableView (with help of binding via the ArrayController).
In the Properties array each item itself (of type VDPropertyWrapper) is binded to the Enabled property of parent object VDPropertyGridView (see VDPropertyGridView.GetProperties).
And then item's Enabled property is binded to the NSTableView cells.
A click on the "Change object" causes the Properties array is replaced with new one and the previously created VDPropertyWrappers become unreachable from app code (but still binded to the VDPropertyGridView.Enabled). We can confirm that VDPropertyWrappers will be GC-ed by adding finalizer (it actually being called after "Change object" was clicked).
The next click to the "Change enabled" changes the VDPropertyGridView.Enabled and Cocoa try to update binded value. And crashes.
Please note, that in example project I manually call GC each second (see Main.cs).
The possible workarouns:
* Do not call GC - the issue will stop reproducing (but will be still there)
* Call unbind before recreating Properties array (see TODO comment in VDPropertyGridView). This will actually fix the problem.
### Environment
```
=== Visual Studio Professional 2019 for Mac ===
Version 8.6 (build 4520)
Installation UUID: cc5d19b0-c21c-44b6-9fb1-f352f7ed1eb5
GTK+ 2.24.23 (Raleigh theme)
Xamarin.Mac 6.18.0.23 (d16-6 / 088c73638)
Package version: 610000104
=== Mono Framework MDK ===
Runtime:
Mono 6.10.0.104 (2019-12/5d03a6fe116) (64-bit)
Package version: 610000104
=== Roslyn (Language Service) ===
3.6.0-3.20210.9+4eafdcb1bcbd8d3573f2ba6065e56d9b9ce4f8a3
=== NuGet ===
Version: 5.6.0.6591
=== .NET Core SDK ===
SDK: /usr/local/share/dotnet/sdk/3.1.300/Sdks
SDK Versions:
3.1.300
3.1.202
3.1.200
3.1.102
3.1.101
3.1.100
3.0.101
MSBuild SDKs: /Library/Frameworks/Mono.framework/Versions/6.10.0/lib/mono/msbuild/Current/bin/Sdks
=== .NET Core Runtime ===
Runtime: /usr/local/share/dotnet/dotnet
Runtime Versions:
3.1.4
3.1.2
3.1.1
3.1.0
3.0.1
2.1.18
2.1.17
2.1.16
2.1.15
2.1.14
=== Xamarin.Profiler ===
Version: 1.6.12.29
Location: /Applications/Xamarin Profiler.app/Contents/MacOS/Xamarin Profiler
=== Updater ===
Version: 11
=== Apple Developer Tools ===
Xcode 11.4.1 (16137)
Build 11E503a
=== Xamarin.Mac ===
Version: 6.18.1.31 (Visual Studio Professional)
Hash: b3eedfed9
Branch: d16-6
Build date: 2020-04-09 10:22:36-0400
=== Xamarin.iOS ===
Version: 13.18.1.31 (Visual Studio Professional)
Hash: b3eedfed9
Branch: d16-6
Build date: 2020-04-09 10:22:36-0400
=== Xamarin Designer ===
Version: 16.6.0.329
Hash: d4f8bcd13
Branch: remotes/origin/d16-6
Build date: 2020-04-24 02:16:02 UTC
=== Xamarin.Android ===
Not Installed
=== Microsoft Mobile OpenJDK ===
Java SDK: Not Found
Android Designer EPL code available here:
https://github.com/xamarin/AndroidDesigner.EPL
=== Android SDK Manager ===
Version: 16.6.0.50
Hash: 5901879
Branch: remotes/origin/d16-6
Build date: 2020-04-30 04:01:22 UTC
=== Android Device Manager ===
Version: 16.6.0.95
Hash: 45d17b5
Branch: remotes/origin/d16-6
Build date: 2020-04-30 04:01:42 UTC
=== Build Information ===
Release ID: 806004520
Git revision: 9c92c7baceef7771f8dcf5c86e1e1a66fdc26182
Build date: 2020-05-18 10:05:00-04
Build branch: release-8.6
Xamarin extensions: 9c92c7baceef7771f8dcf5c86e1e1a66fdc26182
=== Operating System ===
Mac OS X 10.15.4
Darwin 19.4.0 Darwin Kernel Version 19.4.0
Wed Mar 4 22:28:40 PST 2020
root:xnu-6153.101.6~15/RELEASE_X86_64 x86_64
=== Enabled user installed extensions ===
FileNesting 0.1.2
Open With 0.2
```
### Build Logs
[buildLog.txt](https://github.com/xamarin/xamarin-macios/files/4683910/buildLog.txt)
### Example Project (If Possible)
[PropertyGridDebug.zip](https://github.com/xamarin/xamarin-macios/files/4683630/PropertyGridDebug.zip)
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.