Bug 5568 - NSOutlineView DataSource/Delegate appears to be broken
Summary: NSOutlineView DataSource/Delegate appears to be broken
Status: RESOLVED FIXED
Alias: None
Product: MonoMac
Classification: Desktop
Component: Bindings ()
Version: unspecified
Hardware: PC Windows
: Normal normal
Target Milestone: ---
Assignee: Miguel de Icaza [MSFT]
URL:
Depends on:
Blocks:
 
Reported: 2012-06-07 12:40 UTC by Chris Hamons
Modified: 2012-06-17 01:25 UTC (History)
2 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:


Attachments
Objective-C (41.90 KB, application/zip)
2012-06-07 12:40 UTC, Chris Hamons
Details
c# Example (2.37 MB, application/zip)
2012-06-07 12:41 UTC, Chris Hamons
Details
New Example. (1.69 MB, application/zip)
2012-06-14 17:23 UTC, Chris Hamons
Details


Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED FIXED

Description Chris Hamons 2012-06-07 12:40:36 UTC
I'm attempting to use NSOutlineView with the Delegate/Datasource properties. Attached is a Objective-C and C# example program, with identical code. One works, one does not.
Comment 1 Chris Hamons 2012-06-07 12:40:53 UTC
Created attachment 2029 [details]
Objective-C
Comment 2 Chris Hamons 2012-06-07 12:41:16 UTC
Created attachment 2030 [details]
c# Example
Comment 3 Chris Hamons 2012-06-07 14:37:25 UTC
I forgot to mention in the original report, but the crash doesn't appear on launch. You have to resize the window for about 10 seconds to make it occur. It occurs 100% of the time on my machine.
Comment 4 Chris Hamons 2012-06-14 17:23:33 UTC
Created attachment 2062 [details]
New Example.
Comment 5 Chris Hamons 2012-06-14 17:24:59 UTC
I've tracked down either a new issue or another example of the same root cause (I believe latter). 

It appears that the delegate of NSOutlineView goes bad when both are true:

1) It is shoved in the visual tree:
2) GC.Collect() is then called.

The attached always crashes mono/monotouch/the app when CRASH is defined and never does when I keep an explicit reference around (CRASH not defined).
Comment 6 Miguel de Icaza [MSFT] 2012-06-17 01:25:21 UTC
Hello Chris,

I fixed one part of the problem, which was that NSViews were not keeping a reference alive to the items below it.   A workaround for you until you upgrade your MonoMac is to do something like:

static object retainer;

And add the variable "outline" to retainer.

The second problem is a bit more complicated, I can offer a solution for now, and I need to think more about it.

The two samples are actually different.  The Objective-C one returns NSNumbers while the C# one returns a custom class.   A simple fix is to return an NSNumber and then use an NSNumber later on, instead of the custom class.

----

Now if you want to use your wrapper, the problem is this: we create an object in managed code that contains the value, and return it to Objective-C.   Objective-C takes a reference to it, and since we no longer need it, we garbage collect it.

Later, Objective-C surfaces the object back to managed code, but we already got rid of the object, so you get an exception that the constructor with type IntPtr does not exist.

So the options are:

(a) Keep a hashtable of all returned objects to Objective-C, so that they can be invoked back

(b) Encode in the NSNumber the data that you need to produce the data on demand.