473,320 Members | 1,947 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Interesting Results, StackFrame

Here is some code the provides some really interesting results! First, read
over the two methods in class 'A' and compare. Just by looking at them,
both results appear to return the EXACT same information the same way. But
the appearances are deceiving! Copy and paste into a console application,
then set the configuration to Release mode :)

using System;
using System.Diagnostics;
using System.Reflection;

namespace ConsoleApplication1
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
Console.WriteLine(A.DoIt1());
Console.WriteLine(A.DoIt2());
}
}

public class A
{
public static string DoIt1()
{
StackTrace trace = new StackTrace();
StackFrame frame = trace.GetFrame(0);
MethodBase method = frame.GetMethod();
return method.Name;
}
public static string DoIt2()
{
StackTrace trace = new StackTrace();
StackFrame frame = trace.GetFrame(0);
MethodBase method = frame.GetMethod();
int i = 0; i++;
return method.Name;
}
}
}
Very interesting ... anywho, comments or explanations? :)

Mythran

May 23 '06 #1
8 7930
"Mythran" <ki********@hotmail.comREMOVETRAIL> wrote:
Here is some code the provides some really interesting results! [snip] Very interesting ... anywho, comments or explanations? :)


What you're seeing is inlining. Stack traces in release mode don't
necessarily reflect the true sequence of calls. If any of the stack
frames include code access security attributes, or if they are
moderately complex, then they won't be inlined.

-- Barry

--
http://barrkel.blogspot.com/
May 23 '06 #2
On Tue, 23 May 2006 15:01:22 -0700, Mythran wrote:
Here is some code the provides some really interesting results! First, read
over the two methods in class 'A' and compare. Just by looking at them,
both results appear to return the EXACT same information the same way. But
the appearances are deceiving! Copy and paste into a console application,
then set the configuration to Release mode :) [...] Very interesting ... anywho, comments or explanations? :)


That looks surpring indeed. I suppose that when compiling in Release mode,
the JIT compiler must do some optimization such as inlining the call to
DoIt1, hence the result.
May 23 '06 #3

"Barry Kelly" <ba***********@gmail.com> wrote in message
news:4l********************************@4ax.com...
"Mythran" <ki********@hotmail.comREMOVETRAIL> wrote:
Here is some code the provides some really interesting results!

[snip]
Very interesting ... anywho, comments or explanations? :)


What you're seeing is inlining. Stack traces in release mode don't
necessarily reflect the true sequence of calls. If any of the stack
frames include code access security attributes, or if they are
moderately complex, then they won't be inlined.

-- Barry

--
http://barrkel.blogspot.com/


And I don't have code access security attributes applied, and I don't see
how i++ is even considered complex at all...the only difference in the two
methods is one having an "int i = 0; i++;" and the other doesn't...yet the
results of the trace is different...

Mythran

May 23 '06 #4
Mythran,

What is so interesting

I get the exact result expected,
DoIt1
DoIt2

To really see result use
Console.WriteLine(A.DoIt1());
Console.WriteLine(A.DoIt2());
Console.ReadLine();

Sa

"Mythran" <ki********@hotmail.comREMOVETRAIL> wrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...
Here is some code the provides some really interesting results! First,
read over the two methods in class 'A' and compare. Just by looking at
them, both results appear to return the EXACT same information the same
way. But the appearances are deceiving! Copy and paste into a console
application, then set the configuration to Release mode :)

using System;
using System.Diagnostics;
using System.Reflection;

namespace ConsoleApplication1
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
Console.WriteLine(A.DoIt1());
Console.WriteLine(A.DoIt2());
}
}

public class A
{
public static string DoIt1()
{
StackTrace trace = new StackTrace();
StackFrame frame = trace.GetFrame(0);
MethodBase method = frame.GetMethod();
return method.Name;
}
public static string DoIt2()
{
StackTrace trace = new StackTrace();
StackFrame frame = trace.GetFrame(0);
MethodBase method = frame.GetMethod();
int i = 0; i++;
return method.Name;
}
}
}
Very interesting ... anywho, comments or explanations? :)

Mythran

May 23 '06 #5
"Mythran" <ki********@hotmail.comREMOVETRAIL> wrote:
And I don't have code access security attributes applied, and I don't see
how i++ is even considered complex at all...the only difference in the two
methods is one having an "int i = 0; i++;" and the other doesn't...yet the
results of the trace is different...


Basically, you can't rely on whether or not inlining occurs. That's
about all you can say.

-- Barry

--
http://barrkel.blogspot.com/
May 23 '06 #6
"msdn" <sq**********@hotmail.com> wrote:
What is so interesting

I get the exact result expected,
DoIt1
DoIt2


Did you compile in both release and debug mode? Or alternatively, on the
command line, try 'csc /optimize+' versus 'csc /optimize-'.

-- Barry

--
http://barrkel.blogspot.com/
May 23 '06 #7
Yes I did in both ways (release and debug) and in both ways I got the same
result.

Sa
"Barry Kelly" <ba***********@gmail.com> wrote in message
news:0j********************************@4ax.com...
"msdn" <sq**********@hotmail.com> wrote:
What is so interesting

I get the exact result expected,
DoIt1
DoIt2


Did you compile in both release and debug mode? Or alternatively, on the
command line, try 'csc /optimize+' versus 'csc /optimize-'.

-- Barry

--
http://barrkel.blogspot.com/

May 24 '06 #8
"msdn" <sq**********@hotmail.com> wrote:
Yes I did in both ways (release and debug) and in both ways I got the same
result.


And you ran both on the command line (i.e. outside the IDE, so the
debugger isn't attached), on .NET 2.0? Interesting.

-- Barry

--
http://barrkel.blogspot.com/
May 24 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Lord Bah | last post by:
I've seen some other posts referring to this problem, but didn't find any solutions. We're trying to log a call stack when we have a problem, using the StackTrace and StackFrame classes in C#....
2
by: Avinash | last post by:
Hi, I am facing problem with the use of the stackframe and stacktrace for the exception hadling with Windows service. Can any one please tell me how to use of the above objects (stackframe and...
2
by: scottrm | last post by:
I am using StackFrame class to retrieve File Name and File Line number for printing out into the debug file. Everything is working under Debug build. However StackFrame doesn't capture File Name...
0
by: Ben Fidge | last post by:
Hi When my ASP.NET application traps an exception, it's details and the call stack are written to a log, but for some reason, StackFrame.GetFileName() always returns null. This is when running...
3
by: Joe Rattz | last post by:
Moved some asp.net code using StackFrame (built in debug mode) to a Windows Server 2003 box and get empty filename and filelinenumber back. Anyone know why? Works just fine on Win2k.
2
by: HCF_15 | last post by:
Hi all, I have a small piece of code to use StackFrame to GetMethod, I found it is inconsistent in Debug version and Release version, is there anything I am doing wrong? Here is code, build in...
2
by: newscorrespondent | last post by:
I have compiled in DEBUG mode but don't get the debug information from StackFrame class. Is there a specific setting I need to use so that information is included for a debug build. Thanks Tom
0
by: Gerrit Beuze | last post by:
Hi all, I'd like to get at the real underlying type when using the EnvDTE.Debugger Currently I do this: EnvDTE.Thread thread = dte.Debugger.CurrentThread; foreach (StackFrame frame in...
3
by: Ryanivanka | last post by:
hi, is the stackframe in managed code the same as in unmanaged? or they are not related at all.... :) if I use some asm codes (get the esp register or something about stackframe) in a...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.