<?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>Emad Alashi's Blog &#187; JIT compiler Method not found</title>
	<atom:link href="http://www.emadashi.com/index.php/tag/jit-compiler-method-not-found/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.emadashi.com</link>
	<description></description>
	<lastBuildDate>Tue, 31 Aug 2010 08:00:02 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>JIT compiler and &quot;Method Not Found&quot; error</title>
		<link>http://www.emadashi.com/index.php/2009/08/jit-compiler-and-method-not-found-error/</link>
		<comments>http://www.emadashi.com/index.php/2009/08/jit-compiler-and-method-not-found-error/#comments</comments>
		<pubDate>Tue, 11 Aug 2009 21:22:06 +0000</pubDate>
		<dc:creator>Emad Alashi</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Misc]]></category>
		<category><![CDATA[JIT compiler Method not found]]></category>

		<guid isPermaLink="false">http://www.emadashi.com/index.php/2009/08/jit-compiler-and-method-not-found-error/</guid>
		<description><![CDATA[ 
Actually it can be &#8220;Could Not Load Type&#8221; too, but the reason is the same: you are referencing the wrong DLL version.
well, this is totally understandable; of course you are going to get this exception when you use an outdated DLL that lacks the new extra parameter to THAT certain method. But the interesting [...]]]></description>
			<content:encoded><![CDATA[<p><img style="border-right-width: 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" border="0" alt="CropperCapture[20]" src="http://www.emadashi.com/wp-content/uploads/2009/08/windowslivewriterjitcompilerandmethodnotfounderror-150f9croppercapture20-3.jpg" width="396" height="152"> </p>
<p>Actually it can be &#8220;<em>Could Not Load Type</em>&#8221; too, but the reason is the same: you are referencing the wrong DLL version.</p>
<p>well, this is totally understandable; of course you are going to get this exception when you use an outdated DLL that lacks the new extra parameter to THAT certain method. But the interesting part is it will not happen when you first run your application, and neither when execution reaches the changed method; it will happen when code-execution reaches a place that REFERENCES that changed method.</p>
<p>lets look at the following example.</p>
<p>I have created two projects: Windows Forms project called &#8220;HostDLL&#8221;, and a Class Library project called &#8220;BadDLL&#8221;.<br />The HostDLL references the BadDLL; upon clicking a button in the form, the HostDLL will create an instance of class &#8220;BadClass&#8221; and call the method &#8220;DoStuff&#8221; which takes two integer parameters. <br />everything goes fine: </p>
<p><!--<br />
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset178\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue255;\red255\green255\blue255;\red0\green0\blue0;\red43\green145\blue175;\red163\green21\blue21;}??\fs20 \cf1 private\cf0  \cf1 void\cf0  button1_Click(\cf1 object\cf0  sender, \cf4 EventArgs\cf0  e)\par ??        \{\par ??            \cf4 BadClass\cf0  bc = \cf1 new\cf0  \cf4 BadClass\cf0 ();\par ??            \cf1 int\cf0  x = 3, y = 10;\par ??\par ??            \cf1 bool\cf0  never = \cf1 false\cf0 ;\par ??            \cf1 if\cf0  (never)\par ??            \{\par ??                bc.DoStuff(x, y);\par ??            \}\par ??            \cf4 MessageBox\cf0 .Show(\cf5 "Done successfully"\cf0 );\par ??        \}}<br />
-->
<div style="font-family: courier new; background: white; color: black; font-size: 10pt">
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp; 20</span>&nbsp;<span style="color: blue">private</span> <span style="color: blue">void</span> button1_Click(<span style="color: blue">object</span> sender, <span style="color: #2b91af">EventArgs</span> e)</p>
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp; 21</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</p>
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp; 22</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: #2b91af">BadClass</span> bc = <span style="color: blue">new</span> <span style="color: #2b91af">BadClass</span>();</p>
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp; 23</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">int</span> x = 3, y = 10;</p>
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp; 24</span>&nbsp;</p>
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp; 25</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">bool</span> never = <span style="color: blue">false</span>;</p>
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp; 26</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">if</span> (never)</p>
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp; 27</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</p>
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp; 28</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; bc.DoStuff(x, y);</p>
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp; 29</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p>
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp; 30</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: #2b91af">MessageBox</span>.Show(<span style="color: #a31515">&#8220;Done successfully&#8221;</span>);</p>
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp; 31</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p>
</div>
<p>Notice the IF clause in line 26, and notice that calling the method &#8220;bc.DoStuff&#8221; will never take place because the &#8220;never&#8221; variable is always false.</p>
<p>Now, intentionally, we will make a breaking change in the BadDLL; adding a new integer parameter z to the DoStuff method (we will make this change AFTER we have compiled the HostDLL so we don&#8217;t get compile-time correction).</p>
<p>Run the applicaion HostDLL to show the form, you will notice that there is no error, even when the BadDLL has been changed. Now hit the button&#8230; you will get a run-time error that says &#8220;Method Not Found&#8221;. The interesting part of the story is that even though DoStuff will never get called, yet we will still get this run-time error. <br />The reason is that the JIT compiler does what it&#8217;s called after: &#8220;<strong>Just-In-Time</strong> <strong>Compiler</strong>&#8220;; using help from the &#8220;<a href="http://www.amazon.com/CLR-via-Second-Pro-Developer/dp/0735621632">CLR via C#</a>&#8221; book authored by <a href="http://www.wintellect.com/cs/blogs/jeffreyr/default.aspx">Jeffery Richter</a>, specifically in &#8220;Executing Your Assembly&#8217;s Code&#8221; section, the explanation is that:</p>
<blockquote><p>To execute a method, its Intermediate Language must first be converted to native CPU instructions. This is the job of the CLR&#8217;s JIT (just-in-time) compiler&#8230;Just before the <em>method </em>executes, the CLR detects all of the types that are referenced by <em>the method&#8217;s</em><br />code (<em>in our case it&#8217;s the BadClass</em>). This causes the CLR to allocate an internal data structure that is used to manage access to the referenced types.</p>
</blockquote>
<p>The author continues in a graph in which he explains the process:</p>
<blockquote><p>1. In the assembly that implements the type, look up the method being called in the metadata<br />2. From the metadata, get the IL for this method<br />3. Allocate a block of memory<br />4. Compile the IL into native CPU instructions </p>
</blockquote>
<p>So it is at that point the code is compiled, and at that point the JIT discovers that there is a type (which is BadClass in our case) is referenced having an invalid method with one extra parameter.</p>
<p>So always be careful when you reference other DLL&#8217;s, if you don&#8217;t make sure you have the right version, you will be subject to a potential lovely RTE message <img src='http://www.emadashi.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.emadashi.com/index.php/2009/08/jit-compiler-and-method-not-found-error/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
