Bug 21749 - Garbage collection interferes with Objective-C messaging sending or maybe native handles?
Summary: Garbage collection interferes with Objective-C messaging sending or maybe nat...
Alias: None
Product: Xamarin.Mac
Classification: Desktop
Component: Other ()
Version: Master
Hardware: PC Mac OS
: Normal normal
Target Milestone: master
Assignee: Bugzilla
Depends on:
Reported: 2014-07-31 23:27 UTC by Brendan Zagaeski (Xamarin Team, assistant)
Modified: 2017-03-15 21:26 UTC (History)
7 users (show)

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

Test case (Xamarin.Mac) (27.76 KB, application/zip)
2014-07-31 23:27 UTC, Brendan Zagaeski (Xamarin Team, assistant)
Objective-C Sample (41.61 KB, application/zip)
2014-07-31 23:37 UTC, Brendan Zagaeski (Xamarin Team, assistant)

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:

Description Brendan Zagaeski (Xamarin Team, assistant) 2014-07-31 23:27:13 UTC
Created attachment 7559 [details]
Test case (Xamarin.Mac)

Garbage collection interferes with Objective-C messaging sending or maybe native handles?

In this test case, many recursive message sends are made by an `NSUrlProtocol` subclass and an `NSUrlConnectionDelegate` subclass. `GC.Collect()` is called once during each recursive call. After several of these recursive calls, the `Url` property of the `NSUrlRequest` method parameter sometimes returns an `NSUrl` with `Handle == IntPtr.Zero`. Additionally, two _consecutive_ calls to the `Url` property sometimes return _different_ results.

## Steps to reproduce

1. Build and run the attached Xamarin.Mac test case in either the Debug or Release configuration.

(Note: currently the Debug configuration has bundling + linking enabled. That just speeds things up a bit. You can disable them both if you like.)

2. Open a new window in the app.

## Result

After a little while the console output shows a message similar to this:

> Stopped after 75 requests: url1.Scheme = , url2.Scheme = file
> Stopped after 110 requests: url1.Scheme = , url2.Scheme = file
> Stopped after 117 requests: url1.Scheme = , url2.Scheme = file

## Expected result

The console output never shows any "Stopped" messages. Instead the app should continue to make calls to `CanInitWithRequest()` _indefinitely_ as described on [1].

[1] http://www.raywenderlich.com/59982/nsurlprotocol-tutorial

You can produce this expected result by removing the calls to `GC.Collect()` and `GC.WaitForPendingFinalizers()` from `CustomUrlProtocol.CanInitWithRequest()`. Removing just the call to `GC.WaitForPendingFinalizers()` does not change the result.

## Version information

Mono 3.6.0 ((no/f540f8a)

Also tested on Mono 3.6.1 (2111224)

Xcode 5.1 (5084), build 5B130a
OS X 10.9.4
Comment 1 Brendan Zagaeski (Xamarin Team, assistant) 2014-07-31 23:37:16 UTC
Created attachment 7560 [details]
Objective-C Sample

Just on the small chance that a corresponding Objective-C sample might come in handy, here's a quick, fairly close translation of the C# sample.
Comment 2 Brendan Zagaeski (Xamarin Team, assistant) 2014-07-31 23:46:24 UTC
The behavior in this bug might also be the same issue described in [1]:

"I'm getting some intermittent loading issues [with a custom NSUrlProtocol] where sometimes a WebView will load but other times it will not."

[1] https://forums.xamarin.com/discussion/11101/using-nsurlprotocol-to-add-a-custom-header
Comment 3 Rodrigo Kumpera 2014-08-04 16:39:19 UTC
This is a Xamarin.Mac bug specific to its binding engine.
Comment 4 Brendan Zagaeski (Xamarin Team, assistant) 2014-08-07 14:44:49 UTC
One additional observation that might be helpful:

I can also produce the "expected result" if I leave the explicit garbage collection calls unchanged, and instead wrap the two `NSUrl` variables `url1` and `url2` in `using` statements so that they are disposed at the end of each call to `CanInitWithRequest()`.

On an unrelated note, the call to `Request.MutableCopy()` is not required to produce the problem. If you like, you can remove that line, and then replace:

> connection = new NSUrlConnection (newRequest, connectionDelegate);

... with:
> connection = new NSUrlConnection (Request, connectionDelegate);
Comment 5 Rajneesh Kumar 2014-11-03 08:28:28 UTC
I have checked this issue and able to reproduce this reported behavior. To reproduce this issue I have followed the steps provided in Bug description.

Steps I followed:

1. Open attached sample application in XS.
2. Build and run the application
3. Open a new window in the app
4. After a little while the console output shows a message:

Stopped after 319 requests: url1.Scheme = , url2.Scheme = file
Stopped after 355 requests: url1.Scheme = , url2.Scheme = file
Stopped after 359 requests: url1.Scheme = , url2.Scheme = file
Stopped after 361 requests: url1.Scheme = , url2.Scheme = file
Stopped after 362 requests: url1.Scheme = , url2.Scheme = file

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

Application Output: https://gist.github.com/anonymous/72d414eac49d00071340

Environment Info:

=== Xamarin Studio ===

Version 5.5.3 (build 6)
Installation UUID: 011d70a5-dede-428b-ab04-ef451c2e539d
	Mono 3.10.0 ((detached/e204655)
	GTK+ 2.24.23 (Raleigh theme)

	Package version: 310000023

=== Xamarin.Android ===

Version: 4.18.1 (Business Edition)
Android SDK: /Users/MM/Desktop/android-sdk-macosx
	Supported Android versions:
		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)
		4.4.87 (API level 20)
		4.5    (API level 21)
Java SDK: /usr
java version "1.7.0_65"
Java(TM) SE Runtime Environment (build 1.7.0_65-b17)
Java HotSpot(TM) 64-Bit Server VM (build 24.65-b04, mixed mode)

=== Apple Developer Tools ===

Xcode 6.0.1 (6528)
Build 6A317

=== Xamarin.iOS ===

Version: (Business Edition)
Hash: 80e9ff7
Build date: 2014-10-22 15:09:12-0400

=== Xamarin.Mac ===

Version: (Business Edition)

=== Build Information ===

Release ID: 505030006
Git revision: fbe3e9453daf6a3bb9a9709ed22bec35f7c9056b
Build date: 2014-10-23 13:08:38-04
Xamarin addins: e44add2b39de4dd57c0742bb2e620dfad84c64c6

=== 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
Comment 6 Chris Hamons 2017-03-15 21:26:29 UTC
This bug has been open for a long time, so I ported the example to Xamarin.Mac Unified and I can't reproduce it.

Sorry for the delay, but it appears to have been fixed sometime after 2014