2

In my Visual Studio 2015 I create a new Windows.Forms application (not a console application) and do nothing else than adding two lines of code using the % operator within a self assigning + or - operation (e.g. to make a number even). The result is an Invalidprogramexception when I launch this as a Release build with 'optimize code' (which is the default).

using System;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            int a = 0, b;
            a += a % 2; // b = a + a % 2; also crashes.
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }
}

Again: This only crashes if I launch the Release build. As soon as I uncheck 'Optimize code' in the Build settings, it works fine, so obviously the optimizer confuses the code. Changing the code slightly makes things either work or not work, depending on the code. I checked various Framework versions (2.0 to 5.4.1) it is the same thing all over the place. And it happens not only in the example above - it occurred to me in a big Forms application somewhere in the regular code.

Of course it would be easy to rearrange code in a way that it does not crash for now. But I would very much like to gain an understanding of the problem, because how can you feel safe with % operator if such things happen?

I searched the internet for this problem a lot now but have not found anything about it. I would be happy already if someone here could confirm this issue or otherwise tell me that I am hallucinating. All fine, because right now I feel totally lost.

Bassaly
  • 21
  • 3
  • 1
    Just tested the code you posted and it doesn't crashes at all... – Gusman Jan 17 '17 at 17:44
  • Are you compiling Any CPU, x86 or x64? – Gusman Jan 17 '17 at 17:44
  • There is nothing "unsafe" about the `%` operator - you are getting that error for a different reason. – D Stanley Jan 17 '17 at 17:53
  • 1
    maybe related: http://stackoverflow.com/a/24572087/1132334 – Cee McSharpface Jan 17 '17 at 18:00
  • 1
    consider [PEVerify](https://support.microsoft.com/es-co/help/312544/you-may-receive-an-invalidprogramexception-error-message-when-you-run-a-microsoft-.net-connected-program), and remove the `Form1` related stuff: can you repro with just the first two lines in the body of `Main()`? Does it occur only when `vshost`ed, or also when you start the release exe from shell? Does it also occur when `a` is nonzero? – Cee McSharpface Jan 17 '17 at 18:07
  • Yes, it happens without the form-related stuff as well. It also happens when I run the .exe from the shell. And the value of a does not seem to have any effect. I tried various nonzero values, also negatives. – Bassaly Jan 17 '17 at 21:52
  • Two more things: It happens with x86, x64 and Any CPU all the same. Still it does happen only when creating a new Windows Forms application. When creating a Console application it works fine. – Bassaly Jan 17 '17 at 21:58

0 Answers0