Window disappearing during async HttpClient request

TylerDennistonTylerDenniston Tyler DennistonUSMember ✭✭

Hello,

I'm running into what I think is a bug with HttpClient and the asynchronous ReadAsByteArrayAsync function. It's pretty simple: I have a button with an async action that downloads a remote file using the ReadAsByteArrayAsync function. The download seems to complete successfully every time, but the window containing the button disappears.

I've uploaded a sample project reproducing the bug here: https://github.com/tdenniston/XamAsyncDownloadBug

To reproduce the bug, run the application, click the button to show the download window, and then click the button to download the file asynchronously. Sometimes the window will disappear on its own, and sometimes it will disappear if, during the download, you click back to the main window.

Here are my system details:

Operating System
Mac OS X 10.11.6
Darwin 15.6.0 Darwin Kernel Version 15.6.0

Visual Studio Community 2017 for Mac
Version 7.1 (build 1297)
Runtime:
Mono 5.2.0.215 (d15-3/da80840) (64-bit)

Apple Developer Tools
Xcode 8.2.1 (11766.1)
Build 8C1002

Xamarin.Mac
Version: 3.6.0.19 (Visual Studio Community)

Tagged:

Answers

  • TylerDennistonTylerDenniston Tyler Denniston USMember ✭✭

    Update: the bug seems to go away if instead of using ShowWindow() to display the download window, I instead use View.Window.AddChildWindow(...) in the parent. Perhaps this bug is caused by my misunderstanding of how window relationships must work?

  • ChrisHamonsChrisHamons Chris Hamons USXamarin Team Xamurai

    This is likely your bug:

                    var storyboard = NSStoryboard.FromName("Main", null);
                    var windowController = (NSWindowController)storyboard.InstantiateControllerWithIdentifier("DownloadWindowController");
                    windowController.ShowWindow(this);
    

    You are creating a storyboard and not holding onto it. At some point when you use enough memory, GC.Collect will come around, dispose your controller, which will close your window.

    Try holding into the controller and see if it changes behavior.

  • TylerDennistonTylerDenniston Tyler Denniston USMember ✭✭

    Thanks for the reply. Your hypothesis does make sense, but making that change does not fix the bug. Additionally, it seems that the Xamarin Mac samples use a local storyboard variable as well, e.g. https://github.com/xamarin/mac-samples/blob/master/SourceWriter/SourceWriter/AppDelegate.cs#L184.

  • ChrisHamonsChrisHamons Chris Hamons USXamarin Team Xamurai

    Hmm. Locally this:

        NSWindowController controller;
        partial void buttonClick(NSObject sender) {
                var storyboard = NSStoryboard.FromName("Main", null);
                controller = (NSWindowController)storyboard.InstantiateControllerWithIdentifier("DownloadWindowController");
                controller.ShowWindow(this);
        }
    

    does not show the issue, but this:

            try
            {
                var storyboard = NSStoryboard.FromName("Main", null);
                var windowController = (NSWindowController)storyboard.InstantiateControllerWithIdentifier("DownloadWindowController");
                windowController.ShowWindow(this);
            }
            finally
            {
                GC.Collect (2); 
            }
    

    shows it 100% of the time for me.

Sign In or Register to comment.