<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Ticklish Techs &#187; debugging</title>
	<atom:link href="http://www.ticklishtechs.net/tag/debugging/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.ticklishtechs.net</link>
	<description>a mostly .NET but also some other cool techs blog</description>
	<lastBuildDate>Wed, 05 Jan 2011 21:44:29 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.4</generator>
		<item>
		<title>Be careful when using GetCallingAssembly() and always use the release build for testing</title>
		<link>http://www.ticklishtechs.net/2010/03/04/be-careful-when-using-getcallingassembly-and-always-use-the-release-build-for-testing/</link>
		<comments>http://www.ticklishtechs.net/2010/03/04/be-careful-when-using-getcallingassembly-and-always-use-the-release-build-for-testing/#comments</comments>
		<pubDate>Thu, 04 Mar 2010 17:46:24 +0000</pubDate>
		<dc:creator>Benjamin Schröter</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[coding]]></category>
		<category><![CDATA[debugging]]></category>
		<category><![CDATA[dotnet]]></category>
		<category><![CDATA[dotnet3.5]]></category>

		<guid isPermaLink="false">http://www.ticklishtechs.net/2010/03/04/be-careful-when-using-getcallingassembly-and-always-use-the-release-build-for-testing/</guid>
		<description><![CDATA[This looks like such a innocent method but it lead to big trouble in one of my projects. But lets start with when someone would use this method that is declared as a static method in the Assembly class. In the MSDN you can read: Returns the Assembly of the method that invoked the currently [...]]]></description>
			<content:encoded><![CDATA[<p>This looks like such a innocent method but it lead to big trouble in one of my projects. But lets start with when someone would use this method that is declared as a static method in the <code>Assembly</code> class. In the MSDN you can <a href="http://msdn.microsoft.com/en-us/library/system.reflection.assembly.getcallingassembly.aspx">read</a>:</p>
<blockquote><p>Returns the Assembly of the method that invoked the currently executing method.</p></blockquote>
<p>In our project we had an assembly with a lot of helper methods. On of these gets resources from the calling assembly. In various places of our code we called this method to get icons or other resources. This method used exactly this <code>GetCallingAssembly()</code> method to figure out what assembly to look for resources.</p>
<p>That worked pretty good in debug mode but exceptions were thrown in release mode. We could not understand what is going on. It became even worse: when we build a release version and tried to debug that version (using Visual Studio Debugger) in worked again. It looked like a <a href="http://en.wikipedia.org/wiki/Heisenbug#Heisenbug">heisenbug</a>.</p>
<p>It took us some time to figure out what is also written in <a href="http://msdn.microsoft.com/en-us/library/system.reflection.assembly.getcallingassembly%28VS.85%29.aspx">MSDN</a>:</p>
<blockquote><p>If the method that calls the <code>GetCallingAssembly</code> method is expanded inline by the compiler (that is, if the compiler inserts the function body into the emitted Microsoft intermediate language (MSIL), rather than emitting a function call), then the assembly returned by the <code>GetCallingAssembly</code> method is the assembly containing the inline code. This might be different from the assembly that contains the original method. To ensure that a method that calls the <code>GetCallingAssembly</code> method is not inlined by the compiler, you can apply the <code>MethodImplAttribute</code> attribute with <code>MethodImplOptions.NoInlining</code>.</p></blockquote>
<p>The JIT compiler moves code around to optimize for performance. Small methods (up to about 56 Byte IL-Code if I remember it right) can be inlined where the method call was before. But the compiler does this only in release, not in debug mode. Also when attaching the debugger to our release build the JIT compiler stopped inlining to enable debugging and our bug was gone.</p>
<p>After understanding this, the fix is easy. Just don’t allow the compiler to inline that particular method that calls <code>Assembly.GetCallingAssembly()</code>. Then the method stays in the assembly where the source code is written and everything will be fine.</p>
<pre class="code">[<span style="color: #2b91af;">MethodImplAttribute</span>(<span style="color: #2b91af;">MethodImplOptions</span>.NoInlining)]
<span style="color: blue;">public void </span>SomeFunction(<span style="color: blue;">int </span>i)
{
    <span style="color: green;">// ...
    </span><span style="color: blue;">var </span>a = <span style="color: #2b91af;">Assembly</span>.GetCallingAssembly();
    <span style="color: green;">// ...
</span>}</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>This attribute does the trick and I recommend to use it on all methods that call <code>GetCallingAssembly()</code> and can be called form another assembly and need the <em>real </em>calling assembly.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ticklishtechs.net/2010/03/04/be-careful-when-using-getcallingassembly-and-always-use-the-release-build-for-testing/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

