Bug 7449 - Variable is still assigned to even if expression throws exception
Summary: Variable is still assigned to even if expression throws exception
Status: RESOLVED FIXED
Alias: None
Product: iOS
Classification: Xamarin
Component: General (show other bugs)
Version: 6.0.x
Hardware: PC Mac OS
: --- major
Target Milestone: Untriaged
Assignee: Zoltan Varga
URL:
Depends on:
Blocks:
 
Reported: 2012-09-25 12:30 UTC by jesse.attas
Modified: 2012-10-04 07:06 UTC (History)
5 users (show)

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


Attachments

Description jesse.attas 2012-09-25 12:30:59 UTC
The code snippet below behaves incorrectly. The value of "result" at the end of the program should be -1 because the expression that assigns it throws an exception. Instead it's garbage data like 2.69291027116829E-308. This bug only occurs in release mode with "Use LLVM optimizing compiler" turned on.

    public class ClassWithBug : UIViewController
        public override void ViewDidLoad()
        {
            base.ViewDidLoad();
            
            OtherClass c = new OtherClass();
            bool hitException = false;
            double result = -1;
            try
            {
                Console.WriteLine("Before calling: {0}", result);
                result = c.PropertyThatThrowsException;            // THIS LINE SHOULD NOT AFFECT "result" BECAUSE IT THROWS
                Console.WriteLine("Shouldn't hit: {0}", result);
            }
            catch(Exception e)
            {
                hitException = true;
                Console.WriteLine("Hit Exception {1}: {0}", result, e);
            }
            Console.WriteLine("hitException:{1} result:{0}", result, hitException);            
        }
    }
    
    public class OtherClass
    {
        public OtherClass()
        {
        }
        
        private double _backingStore;
        public double PropertyThatThrowsException
        {
            get 
            {
                if (FunctionThatReturnsFalse())
                {
                    return _backingStore;
                }
                else
                {
                    Console.WriteLine("In other class, value is {0}", _backingStore);
                    throw new ArgumentException("Property not supported");
                }
            }
            set
            {
                _backingStore = value;
            }
        }
        
        private bool FunctionThatReturnsFalse()
        {
            Random r = new Random();
            return r.NextDouble() < 0;
        }
    }
}
Comment 1 jesse.attas 2012-09-25 12:31:36 UTC
Version info:

MonoDevelop 3.0.4.7
Runtime:
	Mono 2.10.9 (tarball)
	GTK 2.24.10
	GTK# (2.12.0.0)
	Package version: 210090011
Apple Developer Tools:
	 Xcode 4.5 (1839)
	 Build 4G182
Monotouch: 6.0.0
Comment 2 Sebastien Pouliot 2012-09-25 14:38:07 UTC
>  This bug only occurs in release mode with "Use LLVM optimizing compiler" turned on.

Zoltan, that looks like your area :)
Comment 3 Rolf Bjarne Kvinge [MSFT] 2012-09-26 16:34:26 UTC
This might be related to bug #6650.
Comment 4 Zoltan Varga 2012-10-04 07:06:57 UTC
Fixed in mobile-master/monotouch-6.0-series.

Note You need to log in before you can comment on or make changes to this bug.