Catch does not execute its code

I am developing a C # Application for Windows Phone 8.1 (Silverlight). Recently I ran into an issue related to the application falling asleep and storyboarding.

The construction is built as follows:

class X : DependencyObject
{
    public static readonly DependencyProperty vProperty =
        DependencyProperty.Register("v", typeof(double), typeof(X), new PropertyMetadata(0.0));

    public double v
    {
        get
        {
            return (double)GetValue(vProperty);
        }
        set
        {
            SetValue(vProperty, value);
        }
    }

    private Storyboard _storyboard;
    void Prepare()
    {
        _storyboard = new Storyboard();
        var animation= new DoubleAnimation
        {
            From = 0,
            To = 1,
            BeginTime = 0,
            Duration = 0,
        };
        _storyboard.Children.Add(animation);
        Storyboard.SetTarget(animation, this);
        Storyboard.SetTargetProperty(animation, vProperty);
    }

    void Go()
    {
       _storyboard.Begin();
    }
}

      

There is a NullReferenceException from within _storyboard.Begin () if the application is placed in the background image between Prepare and Go (about 10% playback speed). Of course it ends up crashing.

I was unable to identify the source of the problem, and since I needed a quickfix to do this, I decided to just catch this NullRefereneceException in this rare scenario. This is where the real question begins. I changed the Go implementation:

    public void Go()
    {
        Debug.WriteLine("BreakPoint 1");
        try
        {
            _storyboard.Begin();
        }
        catch (NullReferenceException)
        {
            Debug.WriteLine("BreakPoint 2");
        }
    }

      

Afterwards the crash is not reproduced at all, but the problem is that "BreakPoint 2" never gets hit (no printout in Output). BreakPoint 1 is usually removed and printed. Changing a NullReferenceException to a different type of exception (not parent type c) throws a crash.

So ... what's going on here? Is this crash a cache or not? What strange behavior? Can we assume it will work as expected?

Additional question: Maybe you know why the source code is crashing in the first place?

EDIT: The end of the stack trace internalException from TargetInvocationExceptions looks like this:

   at MS.Internal.XcpImports.CheckHResult(UInt32 hr)
   at MS.Internal.XcpImports.Storyboard_Begin(Storyboard storyboard)
   at System.Windows.Media.Animation.Storyboard.Begin()
   at X.Go()

      

+3


source to share


1 answer


I know you said you tried to use parent types for the NullReferenceException, but try it without in the debugger:

public void Go()
{
    Debug.WriteLine("BreakPoint 1");
    try
    {
        _storyboard.Begin();
    }
    catch (Exception)
    {
        Debug.WriteLine("BreakPoint 2");
        System.Diagnostics.Debugger.Break();
    }
}

      

My suspicion is that the catch is not working because you are running in the debugger. Also try System.Diagnostics.Debugger.Launch();

in catch if .Break();

doesn't work. Finally, try throw;

in catch if .Launch();

doesn't work.

If the debugger is trying to start anyway, then you have another hint.

UPDATE



I cannot give you all the reasons why this might happen, as it may not be possible to pinpoint exactly what is causing it in your situation.

I've seen similar behavior due to the use of multithreading. Multithreading can behave differently when running with an attached debugger without it. Synchronization issues and race conditions can prevent the debugger from throwing exceptions that might otherwise occur frequently when there is no deferred debugger.

I've also seen cases where it was System.Diagnostics.Debugger.IsAttached

used in third party code and even in my command code, which caused the application to behave differently based on statements if

using this check.

And finally, I've had times when I couldn't find a specific reason for the behavior. I have learned to use the method System.Diagnostics.Debugger.Break()

whenever I see behavior showing up differently depending on whether a debugger is attached or not. Sometimes it's really just a gut feeling.

+4


source







All Articles