<?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>Alice and Bob in Cryptoland &#187; ctypes</title>
	<atom:link href="http://alicebob.cryptoland.net/tag/ctypes/feed/" rel="self" type="application/rss+xml" />
	<link>http://alicebob.cryptoland.net</link>
	<description></description>
	<lastBuildDate>Mon, 14 Feb 2011 13:29:59 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Access violation errors with callbacks in ctypes</title>
		<link>http://alicebob.cryptoland.net/access-violation-errors-with-callbacks-in-ctypes/</link>
		<comments>http://alicebob.cryptoland.net/access-violation-errors-with-callbacks-in-ctypes/#comments</comments>
		<pubDate>Sat, 11 Oct 2008 17:19:15 +0000</pubDate>
		<dc:creator>Conrado</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[ctypes]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://alicebob.cryptoland.net/?p=49</guid>
		<description><![CDATA[I&#8217;ve just spent a few hours trying to solve this bug, so I&#8217;m publishing this so maybe it will help someone with this issue&#8230; Assume that you&#8217;re working with a DLL/.so library through ctypes in Python, and this library allows you to set a callback for some other function. In my case, I was working [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve just spent a few hours trying to solve this bug, so I&#8217;m publishing this so maybe it will help someone with this issue&#8230;</p>
<p>Assume that you&#8217;re working with a DLL/.so library through ctypes in Python, and this library allows you to set a callback for some other function. In my case, I was working with unrar.dll. The code was something among these lines:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">UNRARCALLBACK = ctypes.<span style="color: black;">WINFUNCTYPE</span><span style="color: black;">&#40;</span>ctypes.<span style="color: black;">c_int</span>, ctypes.<span style="color: black;">c_uint</span>, ctypes.<span style="color: black;">c_long</span>, ctypes.<span style="color: black;">c_long</span>, ctypes.<span style="color: black;">c_long</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">#in a class...</span>
RARSetCallback<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">handle</span>, UNRARCALLBACK<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">callback_fn</span><span style="color: black;">&#41;</span>, <span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span>
RARProcessFile<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">handle</span>, RAR_TEST, <span style="color: #008000;">None</span>, <span style="color: #008000;">None</span><span style="color: black;">&#41;</span></pre></div></div>

<p>The first lines constructs the function prototype, the second sets the callback in a function of the DLL file, and the third calls a function in the DLL which will call the callback.</p>
<p>Can you spot the error?</p>
<p>The code worked fine in Python 2.5, but then I changed to 2.6 and it stopped working. I got a &#8220;WindowsError: exception: access violation reading&#8230;&#8221; (or writing) exception in the third call.</p>
<p>The reason, which is obvious in hindsight, is cleared explained in the docs:</p>
<blockquote><p>
Make sure you keep references to CFUNCTYPE objects as long as they are used from C code. ctypes doesn’t, and if you don’t, they may be garbage collected, crashing your program when a callback is made.
</p></blockquote>
<p>(Though it&#8217;s not explicit, it applies to WINFUNCTYPE objects too)</p>
<p>The WINFUNCTYPE object created in the second line no longer exists in the third line, so when the callback was called, it no longer pointed to a valid address. The solution is simple &mdash; just keep a reference to the object:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">UNRARCALLBACK = ctypes.<span style="color: black;">WINFUNCTYPE</span><span style="color: black;">&#40;</span>ctypes.<span style="color: black;">c_int</span>, ctypes.<span style="color: black;">c_uint</span>, ctypes.<span style="color: black;">c_long</span>, ctypes.<span style="color: black;">c_long</span>, ctypes.<span style="color: black;">c_long</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">#inside a class...</span>
<span style="color: #008000;">self</span>.<span style="color: black;">callback_ref</span> = UNRARCALLBACK<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">callback_fn</span><span style="color: black;">&#41;</span>
RARSetCallback<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">rarFile</span>.<span style="color: black;">RAR</span>._handle, <span style="color: #008000;">self</span>.<span style="color: black;">callback_ref</span>, <span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span>
RARProcessFile<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">rarFile</span>.<span style="color: black;">RAR</span>._handle, RAR_TEST, <span style="color: #008000;">None</span>, <span style="color: #008000;">None</span><span style="color: black;">&#41;</span></pre></div></div>

<p>The only mystery left is why the old code worked on 2.5!</p>
]]></content:encoded>
			<wfw:commentRss>http://alicebob.cryptoland.net/access-violation-errors-with-callbacks-in-ctypes/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

