Note that there are some explanatory texts on larger screens.

plurals
  1. POStackFrame.GetFileLineNumber() behaviour varies based on assembly Platform and Optimisation flags
    primarykey
    data
    text
    <p>i'm trying to understand a problem and although I've read a lot, I can't seem to find any resources explaining this odd combination.</p> <p>After a bit of experimentation I've found that combinations of setting compiler optimisations on/off and building for the AnyCPU/x86 platform changes the behaviour of StackFrame.GetFileLineNumber()</p> <p>I don't understand why i'm getting the following results (on my x64 system)</p> <pre><code> Optimisations | Platform | Line Number Reported | Result -------------------|---------------|-----------------------|---------- off | anycpu | 10 | Correct off | x86 | 10 | Correct on | anycpu | APPCRASH | WTF? on | x86 | 12 | WTF? </code></pre> <p>The code below reproduces the problem.</p> <pre><code>using System; using System.Diagnostics; namespace ConsoleApplication1 { class Program { static void Main() { GetLineNumberOfThisCall(); Console.WriteLine("\r\nPress any key to exit..."); Console.ReadKey(); } private static void GetLineNumberOfThisCall() { var stackTrace = new StackTrace(true); var callingFrame = stackTrace.GetFrame(1); Console.WriteLine("The call to the stack-walking method was found on line: {0}", callingFrame.GetFileLineNumber()); } } } </code></pre> <p>if you save the above code as <code>code.cs</code> create a batch file with the following script, it will compile assemblies to easily repo to the problem:</p> <pre><code>csc /t:exe /debug+ /out:anycpu-optimisation-on.exe /platform:anycpu /optimize+ code.cs csc /t:exe /debug+ /out:anycpu-optimisation-off.exe /platform:anycpu /optimize- code.cs csc /t:exe /debug+ /out:x86-optimisation-on.exe /platform:x86 /optimize+ code.cs csc /t:exe /debug+ /out:x86-optimisation-off.exe /platform:x86 /optimize- code.cs </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload