Bug 58196 - CVDisplayLink Stop hangs the app
Summary: CVDisplayLink Stop hangs the app
Status: RESOLVED INVALID
Alias: None
Product: Xamarin.Mac
Classification: Desktop
Component: Base Class Libraries ()
Version: 3.4.0 (15.2)
Hardware: PC Mac OS
: --- normal
Target Milestone: ---
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2017-07-17 18:12 UTC by Alvaro Rivoir
Modified: 2017-07-21 00:22 UTC (History)
4 users (show)

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


Attachments
Repro sample (347.65 KB, application/zip)
2017-07-17 18:12 UTC, Alvaro Rivoir
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 INVALID

Description Alvaro Rivoir 2017-07-17 18:12:13 UTC
Created attachment 23604 [details]
Repro sample

Calling method Stop of CVDisplayLink hangs the app, anything is executed, and the app remains unresponsive until killed.


        public override void ViewDidLoad()
        {
            base.ViewDidLoad();

            // Do any additional setup after loading the view.
            View.WantsLayer = true;
            View.Layer.BackgroundColor = NSColor.Red.CGColor;
            Test();
        }

        private async void Test()
        {
            await Task.Delay(1000);
            var before = DateTime.Now;
            var displayLink = new CVDisplayLink();
            displayLink.SetOutputCallback(new CVDisplayLink.DisplayLinkOutputCallback((CVDisplayLink dl, ref CVTimeStamp inNow, ref CVTimeStamp inOutputTime, CVOptionFlags flagsIn, ref CVOptionFlags flagsOut) =>
                {
                    NSRunLoop.Main.InvokeOnMainThread(() =>
                    {
                        if (dl.IsRunning)
                        {
                            if (DateTime.Now - before > TimeSpan.FromSeconds(2))
                            {
                                displayLink.Stop();//After this line anything is executed
                                displayLink.Dispose();
                                View.Layer.BackgroundColor = NSColor.Red.CGColor;
                            }
                            else
                            {
                                var bytes = new byte[3];
                                _rand.NextBytes(bytes);
                                var color = NSColor.FromRgb(bytes[0], bytes[1], bytes[2]);
                                View.Layer.BackgroundColor = color.CGColor;
                            }
                        }
                    });
                    return CVReturn.Success;
                }));
            displayLink.Start();
        }
Comment 1 Alex Soto [MSFT] 2017-07-21 00:22:31 UTC
Hello, this is more a question of how to use an API than an issue in Xamarin.Mac, if I move the displayLink to stop after a couple of seconds instead of Stoping and disposing the object while executing the delegate it won't freeze or crash.

if (DateTime.Now - before > TimeSpan.FromSeconds(2))
{
	Stop (displayLink);
	View.Layer.BackgroundColor = NSColor.Red.CGColor;
}

void Stop (CVDisplayLink displayLink)
{
	var task = Task.Factory.StartNew (() => {
		System.Threading.Thread.Sleep (2000);
		displayLink.Stop ();
		displayLink.Dispose ();
		Console.WriteLine ("Done");
	});
}

The broader developer community would be the best and quickest place for additional troubleshooting help on this issue.
Posting a question on Stack Overflow [0] or the Xamarin Forums [1] would be the best next step.

[0] http://stackoverflow.com/questions/tagged/xamarin
[1] https://forums.xamarin.com/

(Investigation by the Xamarin team in the bug tracker for an issue like this would usually require that the reporter include additional background info to hint that Xamarin is behaving differently compared to Xcode.)

In case it might also be a helpful reference, you can find some general recommendations on the best places to ask various kinds of questions about Xamarin on:

https://developer.xamarin.com/guides/cross-platform/troubleshooting/support-options/