473,394 Members | 1,709 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,394 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 7935
"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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.