Bug 21238 - NewItemForRepresentedObject items on NSCollectionView aren't kept in memory, if GC'ed crashes can happen on scrolling \ API changes.
Summary: NewItemForRepresentedObject items on NSCollectionView aren't kept in memory, ...
Status: VERIFIED FIXED
Alias: None
Product: Xamarin.Mac
Classification: Desktop
Component: Library (Xamarin.Mac.dll) ()
Version: 1.6.9
Hardware: PC Mac OS
: Normal normal
Target Milestone: 2.0.x
Assignee: Chris Hamons
URL:
Depends on:
Blocks:
 
Reported: 2014-07-10 11:58 UTC by Chris Hamons
Modified: 2015-03-03 13:41 UTC (History)
4 users (show)

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


Attachments
Reproduction Case (33.63 KB, application/zip)
2014-07-10 11:58 UTC, Chris Hamons
Details
Better workaround (2.25 KB, application/octet-stream)
2014-11-13 11:59 UTC, Bart King
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:
VERIFIED FIXED

Description Chris Hamons 2014-07-10 11:58:02 UTC
Created attachment 7326 [details]
Reproduction Case

NSCollectionView.Content just doesn't work at all. If you set it two different times (even to the same exact value!) you will crash the app.

Attached is an example, but it is trivial to reproduce.
Comment 1 Chris Hamons 2014-07-10 13:44:44 UTC
Well, the bug is a little more difficult than first thought.

The issue, is that NewItemForRepresentedObject is returning PasteGridItemSelectableViewController items that have zero references and eligible for GC. When we set the content or do the other things listed in the desk case, apple decided to ask for that object, and we can't recreate the c# object that got GC'ed.

A simple workaround:

		List<object> stayingAlive = new List<object>();
		public override NSCollectionViewItem NewItemForRepresentedObject(NSObject obj)
		{
			var item = ((BoxedPaste)obj).PasteItem;
			NSCollectionViewItem o = _isEdit ? new PasteGridItemSelectableViewController(item) : new PasteGridItemViewController(item);;
			stayingAlive.Add (o);
			return o;
		}

This will be non-trivial to fix, I'll talk to the rest of the team next week.
Comment 2 Chris Hamons 2014-07-16 09:40:27 UTC
Confirming.
Comment 3 Sadik Ali 2014-07-16 10:06:26 UTC
I have checked this issue.

When I run the attached sample and press the "Add Items" button on UI and scroll the collection view I observed that when I scroll collection view randomly application is hanged and it shows the spinning beach ball of death and I have to force quite the application to re-run. I have tried the same on many times but every times application hanged but not crashed.

When I apply the workaround mentioned on Comment 1 and I run the application I am able to run application successfully and application doesn't shows the spinning ball or never get hanged no matter how many times I randomly scroll the collection view or resize app window.

Screencast: http://www.screencast.com/t/rByMgXDlUz

Application output: https://gist.github.com/saurabh360/df82d4a7939e7d45ec67

Environment Info:

=== Xamarin Studio ===

Version 5.1.3 (build 0)
Installation UUID: 6ea47b0d-1852-4aaf-808d-373ff0a5002b
Runtime:
	Mono 3.6.0 ((no/5ff701f)
	GTK+ 2.24.23 (Raleigh theme)

	Package version: 306000037

=== Xamarin.Android ===

Version: 4.12.5 (Enterprise Edition)
Android SDK: /Users/jatin66/Desktop/Backup/android-sdk-macosx
	Supported Android versions:
		1.6   (API level 4)
		2.1   (API level 7)
		2.2   (API level 8)
		2.3   (API level 10)
		3.1   (API level 12)
		3.2   (API level 13)
		4.0   (API level 14)
		4.0.3 (API level 15)
		4.1   (API level 16)
		4.2   (API level 17)
		4.3   (API level 18)
		4.4   (API level 19)
Java SDK: /usr
java version "1.7.0_25"
Java(TM) SE Runtime Environment (build 1.7.0_25-b15)
Java HotSpot(TM) 64-Bit Server VM (build 23.25-b01, mixed mode)

=== Apple Developer Tools ===

Xcode 5.1 (5084)
Build 5B130a

=== Xamarin.iOS ===

Version: 7.2.5.5 (Enterprise Edition)
Hash: 87026b6
Branch: 
Build date: 2014-07-02 19:39:08-0400

=== Xamarin.Mac ===

Version: 1.8.0.8

=== Build Information ===

Release ID: 501030000
Git revision: 4676331632b3e5820ed4a92576594309907d778c
Build date: 2014-07-10 20:42:24-04
Xamarin addins: 571e972d874f74867110c205fc25b56048e7e93a

=== Operating System ===

Mac OS X 10.9.3
Darwin Jatin66s-iMac.local 13.2.0 Darwin Kernel Version 13.2.0
    Thu Apr 17 23:03:13 PDT 2014
    root:xnu-2422.100.13~1/RELEASE_X86_64 x86_64
Comment 4 Bart King 2014-11-13 11:59:45 UTC
Created attachment 8719 [details]
Better workaround

Here's a piece of code I made that acts as a reusable solution to this problem.

The key detail is that it keeps track of the .NET objects for all the cells that are created by the collection view. When the content of the collection view changes, because the native objects encapsulated by the previously stored cell .NET objects may have been disposed/drained by the OS, there is code here to check if they are still valid or not.

It is an effort to make sure that there are no substantial memory leaks without compromising on functionality. Otherwise, the list of cells can become quite unruly, especially when there are thousands of cells in the collection view.

This code does work and resolves the problem for me.
Comment 5 Chris Hamons 2015-02-24 12:28:09 UTC
This is not fixed by ref count extensions in unified.
Comment 6 Chris Hamons 2015-02-24 14:00:17 UTC
Fix in master / 527ff9cfebc486ac8f6e28fa193827560d5cb004
Comment 7 Rajneesh Kumar 2015-03-03 13:41:07 UTC
I have checked this issue with the following builds:

=== Xamarin Studio ===
Version 5.8 (build 1043)
Installation UUID: 011d70a5-dede-428b-ab04-ef451c2e539d
Runtime:
Mono 4.0.0 ((detached/2634099)
GTK+ 2.24.23 (Raleigh theme)
Package version: 400000017
Apple Developer Tools Xcode 6.1 (6604)
Build 6A1052d
Xamarin.Mac Version: 1.13.1.417 (Business Edition)
=== Build Information ===
Release ID: 508001043
Git revision: eeba4b77cc5ab021f9c5e2bddf16272e1f6b75d9
Build date: 2015-03-02 06:49:10-05
Xamarin addins: a58814e6e6034bf1169e758b92ead093a3f40404
Operating System Mac OS X 10.9.5
Darwin MacMini.local 13.4.0 Darwin Kernel Version 13.4.0
Sun Aug 17 19:50:11 PDT 2014
root:xnu-2422.115.4~1/RELEASE_X86_64 x86_64

I have checked this issue with the help of attached sample provided in bug description. I observed that it does't crashes or hang on scrolling and resizing the app window. Here is the screencast for the same: http://www.screencast.com/t/LlVs4GfXKOJx

This issue has been fixed, if some one face this issue, so feel free to reopen it.  So as of now, I am closing this issue. 
Thanks.