Bug 29418 - Garbage Collector issue when subclassing native objects
Summary: Garbage Collector issue when subclassing native objects
Status: RESOLVED ANSWERED
Alias: None
Product: iOS
Classification: Xamarin
Component: XI runtime ()
Version: XI 8.10
Hardware: Macintosh Mac OS
: --- normal
Target Milestone: Untriaged
Assignee: Rolf Bjarne Kvinge [MSFT]
URL:
Depends on:
Blocks:
 
Reported: 2015-04-27 06:40 UTC by René
Modified: 2015-04-27 08:15 UTC (History)
3 users (show)

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


Attachments
Demo project (34.62 KB, application/zip)
2015-04-27 06:40 UTC, René
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 ANSWERED

Description René 2015-04-27 06:40:27 UTC
Created attachment 10906 [details]
Demo project

Please refer to the attached video and the sample project.

* "btn" is an instance of a UIButton subclass that is created in ViewDidLoad(). Tapping the button removes it from its superview. The button will get collected.
* "imageView" is an instance of a UIImageView subclass that is created in ViewDidLoad(). There is a "removeBtn" of type UIButton which removed the image view from its superview. The image view will _not_ be collected.
* As soon as the "removeBtn" is added, the "btn" instance won't be collected either even though they are completely unrelated.

Questions:

* Should "btn" really be collected?
* If yes, why is "imageView" not collected?
* Why is "btn" no longer collected if another independent button is added?


=== Xamarin Studio ===

Version 5.9 (build 431)
Installation UUID: 7a127b8e-364a-4453-8b18-e87f614b7d80
Runtime:
	Mono 4.0.0 ((detached/d136b79)
	GTK+ 2.24.23 (Raleigh theme)

	Package version: 400000143

=== Apple Developer Tools ===

Xcode 6.3 (7569)
Build 6D570

=== Xamarin.iOS ===

Version: 8.10.0.266 (Business Edition)
Hash: c8648f0
Branch: master
Build date: 2015-04-21 12:22:24-0400

=== Xamarin.Android ===

Version: 5.1.0.115 (Business Edition)
Android SDK: /Users/rene/Library/Developer/Xamarin/android-sdk-macosx
	Supported Android versions:
		2.3 (API level 10)
		4.2 (API level 17)
		4.4 (API level 19)
		5.0 (API level 21)
Java SDK: /usr
java version "1.6.0_65"
Java(TM) SE Runtime Environment (build 1.6.0_65-b14-466.1-11M4716)
Java HotSpot(TM) 64-Bit Server VM (build 20.65-b04-466.1, mixed mode)

=== Xamarin Android Player ===

Version: Unknown version
Location: /Applications/Xamarin Android Player.app

=== Xamarin.Mac ===

Version: 2.0.0.262 (Business Edition)

=== Build Information ===

Release ID: 509000431
Git revision: 7560726734fc7267de2fa9abed2509968deefaa8
Build date: 2015-04-17 19:25:48-04
Xamarin addins: 2e772c734ab3148054eae7bf8949f340fdeb5e5e

=== Operating System ===

Mac OS X 10.10.3
Darwin iMacRR.local 14.3.0 Darwin Kernel Version 14.3.0
    Mon Mar 23 11:59:05 PDT 2015
    root:xnu-2782.20.48~5/RELEASE_X86_64 x86_64
Comment 1 René 2015-04-27 06:41:13 UTC
Recording of the issue: https://dl.dropboxusercontent.com/u/111612273/GCConfusion.mp4
Comment 2 Rolf Bjarne Kvinge [MSFT] 2015-04-27 06:59:41 UTC
First: the imgView isn't collected because it's kept alive by removeImgBtn's event handler. If I remove removeImgBtn when it's clicked, then the imgView is collected:

	removeImgBtn.TouchUpInside += (sender, e) => {
		removeImgBtn.RemoveFromSuperview ();
		imgView.RemoveFromSuperview();
		...
	};

the second part (btn keeping removeImgBtn alive (and removeImgBtn keeps btn alive as well)) is still somewhat puzzling though.
Comment 3 Rolf Bjarne Kvinge [MSFT] 2015-04-27 07:01:07 UTC
So in the last comment I answered your question #2.

Question #1: yes, btn should be collected when it's removed.
Comment 4 Rolf Bjarne Kvinge [MSFT] 2015-04-27 07:41:27 UTC
Question #3: it's managed code keeping the button alive:

> xamarin_release_trampoline (MemoryTest_CustomButton Handle=0x796d5590) retainCount=2; HasManagedRef=1 GCHandle=35
> Switched object 0x796d5590 to weak GCHandle = 18

This shows that Xamarin.iOS only keeps a weak reference to the CustomButton instance.
Comment 5 Rolf Bjarne Kvinge [MSFT] 2015-04-27 08:15:46 UTC
> René Ruppert: @rolf: Xamarin Profiler shows that there is an anonymous storey that is still alive. This storey is used for both buttons. Since button 2 is still there, button 1 is kept alive.

Closing since this is not a Xamarin.iOS bug.