<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="/stylesheets/rss.css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>notkeepingitreal.com: Paul Brannan: Avoiding Pitfalls in C Extensions</title>
    <link>http://notkeepingitreal.com/articles/2007/11/02/paul-brannan-avoiding-pitfalls-in-c-extensions</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description></description>
    <item>
      <title>Paul Brannan: Avoiding Pitfalls in C Extensions</title>
      <description>&lt;p&gt;What&amp;#8217;s a strategy for playing &lt;a href="http://en.wikipedia.org/wiki/Mafia_%28game%29"&gt;werewolf&lt;/a&gt;? Paying attention to the details of what is going on. This is a also a good strategy for writing C extensions to ruby &amp;#8211; maybe you&amp;#8217;re pair programming and maybe you&amp;#8217;re not, but if you have a bug, your code will likely crash hard; pay attention!&lt;/p&gt;


	&lt;p&gt;Don&amp;#8217;t be afraid to write a ruby extension, but there are definitely things that you should be aware of before you start:&lt;/p&gt;


	&lt;ul&gt;
	&lt;li&gt;you will often have two objects that are representing the same thing, but one is the C object and one is the ruby object &amp;#8211; do not give these variables the same name! Use a naming convention that differentiates them&lt;/li&gt;
		&lt;li&gt;ranges for your data types are important during conversions!&lt;/li&gt;
		&lt;li&gt;allocate resources and deallocate resources in the same place if possible!&lt;/li&gt;
		&lt;li&gt;do not destroy an object that does not belong to you!&lt;/li&gt;
		&lt;li&gt;check function return values!&lt;/li&gt;
		&lt;li&gt;make sure you use rb_ensure!&lt;/li&gt;
		&lt;li&gt;executing arbitrary code and using tainted data are potential security concerns!&lt;/li&gt;
		&lt;li&gt;unit testing is hard, as many C libraries were not built with unit testing in mind, but functional tests that execute your extension code are quite necessary!&lt;/li&gt;
		&lt;li&gt;convert C++ exceptions to ruby exceptions at C++/Ruby boundaries and vice versa!&lt;/li&gt;
		&lt;li&gt;use the Pstore database if you need a database &amp;#8211; it&amp;#8217;s simple!&lt;/li&gt;
		&lt;li&gt;implement to_s and inspect for any custom classes! they&amp;#8217;re useful!&lt;/li&gt;
		&lt;li&gt;use virtual destructors when doing inheritance!&lt;/li&gt;
		&lt;li&gt;there are tools to help &amp;#8211; Rice, etc!&lt;/li&gt;
		&lt;li&gt;damn straight!&lt;/li&gt;
	&lt;/ul&gt;</description>
      <pubDate>Fri, 02 Nov 2007 14:24:00 -0600</pubDate>
      <guid isPermaLink="false">urn:uuid:8b615ed4-be37-4f49-aa07-47fab38cfc76</guid>
      <author>Kevin</author>
      <link>http://notkeepingitreal.com/articles/2007/11/02/paul-brannan-avoiding-pitfalls-in-c-extensions</link>
      <category>Rubyconf</category>
      <trackback:ping>http://notkeepingitreal.com/articles/trackback/1857</trackback:ping>
    </item>
  </channel>
</rss>
