Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / September 2007

Tip: Looking for answers? Try searching our database.

stacktrace and release mode - interesting problem

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Iain Mcleod - 12 Sep 2007 20:17 GMT
put the following into a console app in .net 2.0, compile it in release mode
and then run it 2 ways:
through visual studio (works as expected)
now double click the console app via windows explorer.

This crashes with a null reference exception because the stacktrace only
contains one frame [it still thinks it's in sub Main()].

This seems to be a problem with release mode and compiler optimisations:
http://www.thescripts.com/forum/thread494282.html[^]

My question:
Is there any SAFE way to get the calling method's methodinfo object?

Thanks in advance
Iain

Module Module1

Sub Main()
TestFunction()
Console.ReadLine()
End Sub

Private Sub TestFunction()
PrintStackTrace()
End Sub

Private Sub PrintStackTrace()
Console.WriteLine(New StackTrace().GetFrame(1).GetMethod().Name)
End Sub

End Module
Peter Duniho - 12 Sep 2007 22:01 GMT
> put the following into a console app in .net 2.0, compile it in release mode
> and then run it 2 ways:
[quoted text clipped - 6 lines]
> This seems to be a problem with release mode and compiler optimisations:
> http://www.thescripts.com/forum/thread494282.html[^]

I don't understand your question.  The link you provided includes clear
replies explaining that the issue is due to inlining and is not a
problem (except of course if you are writing code that doesn't take
inlining into account).

In C#, there's a code attribute that can be used to suppress inlining
for specific methods.  I don't know if VB has something similar, but if
it does, then that would be the solution to your problem.

Otherwise, don't write code that assumes a method won't be inlined.

Pete
Iain Mcleod - 13 Sep 2007 09:28 GMT
Thanks Peter

This is exactly the attribute I was looking for:
<MethodImpl(MethodImplOptions.NoInlining)>

You're perfectly correct as well.  You should never write code that relies
on inlining not taking place.  Sadly my colleague who wrote a reflection
based data access layer had never heard of inlining until yesterday.
In fact, the project containing the offending data access layer had been in
production in release mode but in a web project - so obviously no inlining
was taking place.  It was only when I reused the data access library in a
console app that the problem manifested itself.  

I'm sure we're not the first to have fallen victim to this unpredictability.

Cheers
Iain

> > put the following into a console app in .net 2.0, compile it in release mode
> > and then run it 2 ways:
[quoted text clipped - 19 lines]
>
> Pete
Patrice - 13 Sep 2007 08:15 GMT
You could likely test the number of frames to take into account possible
optimizations.

IMO your best is first to explain what you are trying to do...

--
Patrice

> put the following into a console app in .net 2.0, compile it in release
> mode and then run it 2 ways:
[quoted text clipped - 29 lines]
>
> End Module

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.