My Application works in Debug mode only

Hi all,
My .NET application works in debug mode only.

If I build my application without the "Generate Debugging Information" equal true, the application start properly, looks like everything is just fine, I can use several commands of the same type, but it stops working, I mean it goes in a loop or something.

If I run the same application with debug option equal true and I do the same test everything work just fine.
This is a problem because a cannot debug the issue using VS.

About the application:
Language is C#
IDE is Visual Studio .NET 2003
Type is a Windows Forms graphical application to draw vectorial persistent objects.
Code type is only managed.
Graphics lib used: GDI+

Any idea

Should I debug it using Console.Writeline

In general, why in dedug mode works

Thank you in advance

Andrea



Answer this question

My Application works in Debug mode only

  • Vorn

    I'm not sure about your logic but you can debug applications that are built in release mode.  Even in release mode (unless you changed the project settings) the PDB file is generated.  The PDB file contains enough information that, given the source, can be used to walk through your code.  I would recommend that you compile in release mode and then debug through VS.  You'll then be able to use the debugger to debug your problem.

    Michael Taylor - 9/1/05

  • Wowbagger_TIP

    Hi Andrea,

    Only a minor change: from the link that Shawn posted, rather than inserting the string conversion you could cast the result of Math.Atan2 to a double before storing it, so:

    // NOTE: cast to double to narrow result returned.
    double radangle = (double)Math.Atan2(this.x, this.y);

    Or, for clarity, you could have a CompareWithinTolerance(double val1, double val2, double tol) method somewhere, that would compare the absolute difference between the inputs, but that would be quite convoluted, you'd have

    if ( ( radangle > 0.0 || CompareWithinTolerance(radangle, 0.0, somesmallnumber) )
    && ...




  • Soumyp

    The fix works for this test case too.
    Here the result by adding in the GetDegAngle() the string conversion

    ...

    string s;
    s = radangle.ToString();

    return
    degangle;
    }

    Release:
    (-5420, 0 ) (0, 4060 ) (6060, 340 ) (640, 340 ) (6060, 4400 ) 180 90 270
    (0, -4060 ) (-2920, 0 ) (6060, 4400 ) (6060, 340 ) (3140, 4400 ) 270 180 270
    (2920, 0 ) (-2500, 0 ) (3140, 4400 ) (6060, 4400 ) (640, 4400 ) 0 180 540
    (2920, 0 ) (0, -2640 ) (3140, 4400 ) (6060, 4400 ) (3140, 1760 ) 0 270 630
    (2500, 0 ) (0, -4060 ) (640, 4400 ) (3140, 4400 ) (640, 340 ) 0 270 630
    (0, 4060 ) (5420, 0 ) (640, 340 ) (640, 4400 ) (6060, 340 ) 90 0 270

  • syedasad

    Very interesting...especially seeing how it is only the first three edges that differ and the are out of sync by 180...

    Where is x & y being generated from
    Are they Screen or Client points



  • stevencbk

    I did some test and now I'm really lost !!!!!!!
    I found what goes wrong but I cannot figure it.

    Here the code of my Point class to calculate the angle between two points:

    // Angle in Degree
      public double GetDegAngle()
      {
       double degangle = 0;
       double radangle = Math.Atan2( this.y, this.x );

       if ( radangle >= 0.0 && radangle <= Math.PI ) degangle = radangle * 180.0/ Math.PI ;
       if ( radangle >= -Math.PI && radangle < 0.0 ) degangle = 360.0 + radangle * 180.0/ Math.PI;
     
       return degangle;
      }
      
      public double GetDegAngle( MKPnt2d fromP, MKPnt2d toP)
      {
       
       MKPnt2d p1 = new MKPnt2d( fromP.x - this.x, fromP.y - this.y );
       MKPnt2d p2 = new MKPnt2d( toP.x - this.x, toP.y - this.y );

       double fromangle = p1.GetDegAngle();
       double toangle = p2.GetDegAngle();

       double result = toangle + 360.0 - fromangle;
       
       Globals.Instance.DebugWindow.Writeline( p1.ToString() + " " + p2.ToString() + " " + this.ToString() + " " + fromP.ToString() + " " + toP.ToString() + " " + fromangle.ToString() + " " + toangle.ToString() + " " + result.ToString() );
       
       if (result >= 360.0) result = result - 360.0; 
       
       return ( result );
      }


    Here the print out with and without debug option, in black the value for the inputs that are exactly the same, in green the debug option result and in red the different result with Release option.  

    With Debug:

    Edge # 1
    (-5420 , 0) (0 , 4060) (6060 , 340) (640 , 340) (6060 , 4400) 180 90 270
     Edge # 2
    (0 , -4060) (-2920 , 0) (6060 , 4400) (6060 , 340) (3140 , 4400) 270 180 270
     Edge # 3
    (2920 , 0) (-2500 , 0) (3140 , 4400) (6060 , 4400) (640 , 4400) 0 180 540
    (2920 , 0) (0 , -2640) (3140 , 4400) (6060 , 4400) (3140 , 1760) 0 270 630
     Edge # 4
    (2500 , 0) (0 , -4060) (640 , 4400) (3140 , 4400) (640 , 340) 0 270 630
     Edge # 5
    (0 , 4060) (5420 , 0) (640 , 340) (640 , 4400) (6060 , 340) 90 0 270


    Without Debug:

    Edge # 1
    (-5420 , 0) (0 , 4060) (6060 , 340) (640 , 340) (6060 , 4400) 0 90 450
     Edge # 2
    (0 , -4060) (-2920 , 0) (6060 , 4400) (6060 , 340) (3140 , 4400) 270 0 90
     Edge # 3
    (2920 , 0) (-2500 , 0) (3140 , 4400) (6060 , 4400) (640 , 4400) 0 0 360
    (2920 , 0) (0 , -2640) (3140 , 4400) (6060 , 4400) (3140 , 1760) 0 270 630
     Edge # 4
    (2500 , 0) (0 , -4060) (640 , 4400) (3140 , 4400) (640 , 340) 0 270 630
     Edge # 5
    (0 , 4060) (5420 , 0) (640 , 340) (640 , 4400) (6060 , 340) 90 0 270


    Tongue Tied


  • TCVMZhen

    Thank you all here more clues and I found a fix.

    First, looks like that with the same compiler setting, to Release, if I run the application from inside VS everything works fine, if I run it by double click on the exe from an explorer window it does not work.

    I kept digging and now I can make the app produce the same result, however it is an odd fix.

    public double GetDegAngle()
    {
       double degangle = 0;
       
    double radangle = Math.Atan2( this.y, this.x );

       if ( radangle >= 0.0 && radangle <= Math.PI ) degangle = radangle * 180.0/ Math.PI ;
       if ( radangle >= -Math.PI && radangle < 0.0 ) degangle = 360.0 + radangle * 180.0/ Math.PI;

       // this is the fix
       string
    s;
       s = radangle.ToString();
       
       
    return degangle;
    }

    By converting the double radagle into a string this function works fine, how odd !!!!

    I'm not confortable in using this as a fix, I hope in a more understandable solution.

    Andrea

  • Bathisar

    Gary,
    I tried your code and I found the problem again.

    1) Just compile once in debug an once in release mode.
    2) Close your VS
    3) Open a CMD and change directory to the debug folder, run the test
    4) Open another CMD and change directory to the release folder, run the test
    5) Now compare the results

    Debug:
    (-5420, 0 ) (0, 4060 ) (6060, 340 ) (640, 340 ) (6060, 4400 ) 180 90 270
    (0, -4060 ) (-2920, 0 ) (6060, 4400 ) (6060, 340 ) (3140, 4400 ) 270 180 270
    (2920, 0 ) (-2500, 0 ) (3140, 4400 ) (6060, 4400 ) (640, 4400 ) 0 180 540
    (2920, 0 ) (0, -2640 ) (3140, 4400 ) (6060, 4400 ) (3140, 1760 ) 0 270 630
    (2500, 0 ) (0, -4060 ) (640, 4400 ) (3140, 4400 ) (640, 340 ) 0 270 630
    (0, 4060 ) (5420, 0 ) (640, 340 ) (640, 4400 ) (6060, 340 ) 90 0 270

    Release:
    (-5420, 0 ) (0, 4060 ) (6060, 340 ) (640, 340 ) (6060, 4400 ) 0 90 450
    (0, -4060 ) (-2920, 0 ) (6060, 4400 ) (6060, 340 ) (3140, 4400 ) 270 0 90
    (2920, 0 ) (-2500, 0 ) (3140, 4400 ) (6060, 4400 ) (640, 4400 ) 0 0 360
    (2920, 0 ) (0, -2640 ) (3140, 4400 ) (6060, 4400 ) (3140, 1760 ) 0 270 630
    (2500, 0 ) (0, -4060 ) (640, 4400 ) (3140, 4400 ) (640, 340 ) 0 270 630
    (0, 4060 ) (5420, 0 ) (640, 340 ) (640, 4400 ) (6060, 340 ) 90 0 270

    Tongue Tied

  • R3dD0g

    Thank you, I will try.
    Andrea


  • DOMcCrea

    Thank you Gary !!!!!!!!!!!!!!!!!!!!!!!!!
    Casting Math.Atan2 works great.

    Andrea
    SmileSmileSmileSmileSmile

  • john_jps

    Thank you Shawn.
    I will try to flip the optimization switch and see.
    Andrea

  • e-u-l-o-g-y

    It looks like -something- is being optimised out. Running in Debug with the "Optimize Code" set to true for Debug returns the same results as for Release.

    Inserting a Console.WriteLine(radangle.ToString()) straight after
       double radangle = Math.Atan2( this.y, this.x );
    in GetDegAngle() though returns the same results as your Debug example above.

    Here's a slightly simplified console app I knocked up to test this:

        /// <summary>
        /// Summary description for Class1.
        /// </summary>
        class Class1
        {
            /// <summary>
            /// The main entry point for the application.
            /// </summary>
            [STAThread]
            static void Main(string[] args)
            {
                Point p1 = new Point( 6060, 340 );
                Point p2 = new Point( 6060, 4400 );
                Point p3 = new Point( 3140, 4400 );
                Point p4 = new Point( 640, 4400 );
                Point p5 = new Point( 640, 340 );

                p1.GetDegAngle( p5, p2 );
                p2.GetDegAngle( p1, p3 );
                p3.GetDegAngle( p2, p4 );
                p3.GetDegAngle( p2, new Point( 3140, 1760 ) );
                p4.GetDegAngle( p3, p5 );
                p5.GetDegAngle( p4, p1 );
            }
        }

        public class Point
        {
            public double x, y;

            public Point( double x, double y )
            {
                this.x = x;
                this.y = y;
            }

            // Angle in Degree
            public double GetDegAngle()
            {
                double degangle = 0;
                double radangle = Math.Atan2( this.y, this.x );
                //Console.WriteLine( "radangle: " + radangle.ToString() );

                if ( radangle >= 0.0 && radangle <= Math.PI )
                {
                    //Console.WriteLine( "Condition 1 met" );
                    degangle = radangle * 180.0/ Math.PI ;
                }
                if ( radangle >= -Math.PI && radangle < 0.0 )
                {
                    //Console.WriteLine( "Condition 2 met" );
                    degangle = 360.0 + radangle * 180.0/ Math.PI;
                }
     
                return degangle;
            }

            public double GetDegAngle( Point fromP, Point toP)
            {
      
                Point p1 = new Point( fromP.x - this.x, fromP.y - this.y );
                Point p2 = new Point( toP.x - this.x, toP.y - this.y );

                double fromangle = p1.GetDegAngle();
                double toangle = p2.GetDegAngle();

                double result = toangle + 360.0 - fromangle;
      
                Console.WriteLine( p1.ToString() + " " + p2.ToString() + " " + this.ToString() + " " + fromP.ToString() + " " + toP.ToString() + " " + fromangle.ToString() + " " + toangle.ToString() + " " + result.ToString() );
      
                if (result >= 360.0) result = result - 360.0;
      
                return ( result );
            }

            public override string ToString()
            {
                return "(" + this.x.ToString() + ", " + this.y.ToString() + " )";
            }
        }


    cheers,
    Gary


  • Snyper_10

    Hi Andrea,

    You should read David Notario's blog entry CLR and floating point: Some answers to common questions .  One of the problems he covers is " I get different results when compiling with optimizations vs without optimizations!".

    -Shawn



  • TheRealTaz

    x and y are double client coding.
    Andrea

  • My Application works in Debug mode only