<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
		>
<channel>
	<title>Comments on: The meaning of RAII — or why you never need to worry about resource management again</title>
	<atom:link href="http://jalf.dk/blog/2010/01/the-meaning-of-raii-or-why-you-never-need-to-worry-about-resource-management-again/feed/" rel="self" type="application/rss+xml" />
	<link>http://jalf.dk/blog/2010/01/the-meaning-of-raii-or-why-you-never-need-to-worry-about-resource-management-again/</link>
	<description>Musings and thoughts on programming and other geeky stuff</description>
	<lastBuildDate>Fri, 16 Jul 2010 07:38:32 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
	<item>
		<title>By: Daniel Earwicker</title>
		<link>http://jalf.dk/blog/2010/01/the-meaning-of-raii-or-why-you-never-need-to-worry-about-resource-management-again/comment-page-1/#comment-797</link>
		<dc:creator>Daniel Earwicker</dc:creator>
		<pubDate>Sun, 21 Mar 2010 10:35:04 +0000</pubDate>
		<guid isPermaLink="false">http://jalf.dk/blog/?p=340#comment-797</guid>
		<description>&lt;p&gt;&quot;But the onus is still on the user of the class to ensure it is closed cor rectly. There is no obvi ous way to encode into the Db class that “once we’re done with an object of this type, the con nec tion must be closed immediately”.&lt;/p&gt;

&lt;p&gt;Don&#039;t you think the same is true in C++? Given a class C, is it intended to be used like this:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;C c;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Or like this:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;C *p = new C;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;If you attempt to use RAII by providing a class like C to be declared on the stack, then anyone is free to abuse it by allocating a C on the heap (and hopefully deleting it again at some point), and thus they lose the exception-safe cleanup capability. A basic principle of C++ is that there is no way to stop this.&lt;/p&gt;

&lt;p&gt;Yes, it&#039;s true that in C# it is very easy to forget to put a using statement in. So I tend to use the Scheme-style approach of a &quot;with-whatever&quot; method:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;WithOpenFile(filePath, file =&gt; 
{
    // use file in here.
});
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The WithOpenFile method is equivalent to a RAII wrapper class around a file handle, except that it cannot be abused. So it&#039;s actually more robust than the C++ wrapper-class technique. (With C++1x lambdas this is of course now going to be possible in C++ very soon).&lt;/p&gt;

&lt;p&gt;On the downside, it&#039;s also less flexible because you can&#039;t embed it inside another object and so associate their lifetimes (C# is severely lacking in language support there anyway).&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>“But the onus is still on the user of the class to ensure it is closed cor rectly. There is no obvi ous way to encode into the Db class that “once we’re done with an object of this type, the con nec tion must be closed immediately”.</p>

<p>Don’t you think the same is true in C++? Given a class C, is it intended to be used like this:</p>

<pre><code>C c;
</code></pre>

<p>Or like this:</p>

<pre><code>C *p = new C;
</code></pre>

<p>If you attempt to use RAII by providing a class like C to be declared on the stack, then anyone is free to abuse it by allocating a C on the heap (and hopefully deleting it again at some point), and thus they lose the exception-safe cleanup capability. A basic principle of C++ is that there is no way to stop this.</p>

<p>Yes, it’s true that in C# it is very easy to forget to put a using statement in. So I tend to use the Scheme-style approach of a “with-whatever” method:</p>

<pre><code>WithOpenFile(filePath, file =&gt; 
{
    // use file in here.
});
</code></pre>

<p>The WithOpenFile method is equivalent to a RAII wrapper class around a file handle, except that it cannot be abused. So it’s actually more robust than the C++ wrapper-class technique. (With C++1x lambdas this is of course now going to be possible in C++ very soon).</p>

<p>On the downside, it’s also less flexible because you can’t embed it inside another object and so associate their lifetimes (C# is severely lacking in language support there anyway).</p>]]></content:encoded>
	</item>
	<item>
		<title>By: Daniel Earwicker</title>
		<link>http://jalf.dk/blog/2010/01/the-meaning-of-raii-or-why-you-never-need-to-worry-about-resource-management-again/comment-page-1/#comment-796</link>
		<dc:creator>Daniel Earwicker</dc:creator>
		<pubDate>Sun, 21 Mar 2010 10:20:16 +0000</pubDate>
		<guid isPermaLink="false">http://jalf.dk/blog/?p=340#comment-796</guid>
		<description>&lt;p&gt;@jalf: &quot;But because it happens so rarely compared to ref counting, it’s still vastly cheaper overall.&quot;&lt;/p&gt;

&lt;p&gt;And when you factor in the &quot;multi core crisis&quot; GC is even more vastly cheap than ref-counting. GC can pipeline its work into background threads, while ref-counting adds a lot of interlocking.&lt;/p&gt;

&lt;p&gt;It&#039;s ironic, and it goes against all the &quot;folklore&quot;, but it has to be accepted that C++&#039;s low-level memory model approach is actually the more inefficient way to do things in very many (probably most) applications.&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>@jalf: “But because it happens so rarely compared to ref counting, it’s still vastly cheaper overall.”</p>

<p>And when you factor in the “multi core crisis” GC is even more vastly cheap than ref-counting. GC can pipeline its work into background threads, while ref-counting adds a lot of interlocking.</p>

<p>It’s ironic, and it goes against all the “folklore”, but it has to be accepted that C++‘s low-level memory model approach is actually the more inefficient way to do things in very many (probably most) applications.</p>]]></content:encoded>
	</item>
	<item>
		<title>By: jalf</title>
		<link>http://jalf.dk/blog/2010/01/the-meaning-of-raii-or-why-you-never-need-to-worry-about-resource-management-again/comment-page-1/#comment-382</link>
		<dc:creator>jalf</dc:creator>
		<pubDate>Tue, 19 Jan 2010 02:57:58 +0000</pubDate>
		<guid isPermaLink="false">http://jalf.dk/blog/?p=340#comment-382</guid>
		<description>&lt;p&gt;They do roughly the same thing, yes, but in very different ways.&lt;/p&gt;

&lt;p&gt;Consider that a reference counter has to be updated &lt;em&gt;every&lt;/em&gt; time a reference is created or deleted. If a smart pointer points to object &lt;code&gt;a&lt;/code&gt;, and you set it to point to &lt;code&gt;b&lt;/code&gt; instead, you have to update the reference counters for &lt;em&gt;both&lt;/em&gt; objects. And every update has to be done atomically to ensure thread safety as well. That makes it still more costly. When you add it up like that, it&#039;s actually quite a few CPU cycles that are thrown away updating reference counters.&lt;/p&gt;

&lt;p&gt;By comparison, a garbage collector doesn&#039;t have to do &lt;em&gt;anything&lt;/em&gt; when references are created, modified or destroyed. It only has to step in when the heap has been filled so much that a memory allocation fails. Once that happens, it has to traverse the graph of live objects, marking each as in use. All dead (nonreachable) objects are never even touched by the GC; so they&#039;re effectively free. Traversing this graph does take a bit of time, as does the heap compaction that typically follows. But because it happens so rarely compared to ref counting, it&#039;s still vastly cheaper overall.&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>They do roughly the same thing, yes, but in very different ways.</p>

<p>Consider that a reference counter has to be updated <em>every</em> time a reference is created or deleted. If a smart pointer points to object <code>a</code>, and you set it to point to <code>b</code> instead, you have to update the reference counters for <em>both</em> objects. And every update has to be done atomically to ensure thread safety as well. That makes it still more costly. When you add it up like that, it’s actually quite a few CPU cycles that are thrown away updating reference counters.</p>

<p>By comparison, a garbage collector doesn’t have to do <em>anything</em> when references are created, modified or destroyed. It only has to step in when the heap has been filled so much that a memory allocation fails. Once that happens, it has to traverse the graph of live objects, marking each as in use. All dead (nonreachable) objects are never even touched by the GC; so they’re effectively free. Traversing this graph does take a bit of time, as does the heap compaction that typically follows. But because it happens so rarely compared to ref counting, it’s still vastly cheaper overall.</p>]]></content:encoded>
	</item>
	<item>
		<title>By: sheepsimulator</title>
		<link>http://jalf.dk/blog/2010/01/the-meaning-of-raii-or-why-you-never-need-to-worry-about-resource-management-again/comment-page-1/#comment-381</link>
		<dc:creator>sheepsimulator</dc:creator>
		<pubDate>Mon, 18 Jan 2010 18:59:44 +0000</pubDate>
		<guid isPermaLink="false">http://jalf.dk/blog/?p=340#comment-381</guid>
		<description>&lt;p&gt;GREAT ARTICLE!  I felt like it was directed &lt;em&gt;exactly&lt;/em&gt; where I am at in my journey to learn more about OOP.  I think I know how to talkabout RAII better: resources are mapped to objects.&lt;/p&gt;

&lt;p&gt;Didn&#039;t know that garbage collectors were less resource-intensive than shared_ptrs; I thought they were about the same, since they did about the same thing.&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>GREAT ARTICLE!  I felt like it was directed <em>exactly</em> where I am at in my journey to learn more about OOP.  I think I know how to talkabout RAII better: resources are mapped to objects.</p>

<p>Didn’t know that garbage collectors were less resource-intensive than shared_ptrs; I thought they were about the same, since they did about the same thing.</p>]]></content:encoded>
	</item>
	<item>
		<title>By: RAII is not Clear? &#171; C++ Soup!</title>
		<link>http://jalf.dk/blog/2010/01/the-meaning-of-raii-or-why-you-never-need-to-worry-about-resource-management-again/comment-page-1/#comment-350</link>
		<dc:creator>RAII is not Clear? &#171; C++ Soup!</dc:creator>
		<pubDate>Sun, 03 Jan 2010 16:44:48 +0000</pubDate>
		<guid isPermaLink="false">http://jalf.dk/blog/?p=340#comment-350</guid>
		<description>&lt;p&gt;[...] still be confused by this idiom. One thing I read while keeping tabs on the web for C++ articles is this one from [...]&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>[…] still be confused by this idiom. One thing I read while keeping tabs on the web for C++ articles is this one from […]</p>]]></content:encoded>
	</item>
</channel>
</rss>
