<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"><channel><atom:link rel="hub" href="http://tumblr.superfeedr.com/" xmlns:atom="http://www.w3.org/2005/Atom"/><description>I’m an undergraduate in computer science at Carnegie Mellon, with a fiancée named Ellen and a cat named Millie.</description><title>Evan DiBiase</title><generator>Tumblr (3.0; @edibiase)</generator><link>http://evand.org/</link><item><title>Rands In Repose: Pick-Up</title><description>&lt;a href="http://www.randsinrepose.com/archives/2010/06/08/pick-up.html"&gt;Rands In Repose: Pick-Up&lt;/a&gt;</description><link>http://evand.org/post/775603828</link><guid>http://evand.org/post/775603828</guid><pubDate>Tue, 06 Jul 2010 01:18:53 -0400</pubDate></item><item><title>Testicle... It's What's for Dinner | Eat and Drink | Portland Mercury</title><description>&lt;a href="http://www.portlandmercury.com/portland/testicle-its-whats-for-dinner/Content?oid=2518292"&gt;Testicle... It's What's for Dinner | Eat and Drink | Portland Mercury&lt;/a&gt;</description><link>http://evand.org/post/653738416</link><guid>http://evand.org/post/653738416</guid><pubDate>Tue, 01 Jun 2010 12:50:30 -0400</pubDate></item><item><title>Drunk on the Key</title><description>&lt;object type="application/x-shockwave-flash" width="400" height="300" data="http://vimeo.com/moogaloop.swf?clip_id=12132539&amp;server=vimeo.com&amp;fullscreen=1&amp;show_title=1&amp;show_byline=0&amp;show_portrait=0&amp;color=00ADEF"&gt;&lt;param name="quality" value="best" /&gt;&lt;param name="allowscriptaccess" value="always" /&gt;&lt;param name="allowfullscreen" value="true" /&gt;&lt;param name="scale" value="showAll" /&gt;&lt;param name="movie" value="http://vimeo.com/moogaloop.swf?clip_id=12132539&amp;server=vimeo.com&amp;fullscreen=1&amp;show_title=1&amp;show_byline=0&amp;show_portrait=0&amp;color=00ADEF" /&gt;&lt;embed src="http://www.vimeo.com/moogaloop.swf?clip_id=12132539&amp;server=www.vimeo.com&amp;show_title=1&amp;show_byline=0&amp;show_portrait=0&amp;color=00ADEF&amp;fullscreen=1" type="application/x-shockwave-flash" allowfullscreen="true" allowscriptaccess="always" width="400" height="300"&gt;&lt;/embed&gt;&lt;/object&gt;&lt;br/&gt;&lt;br/&gt;&lt;p&gt;&lt;a href="http://vimeo.com/12132539"&gt;Drunk on the Key&lt;/a&gt;&lt;/p&gt;</description><link>http://evand.org/post/644317604</link><guid>http://evand.org/post/644317604</guid><pubDate>Sat, 29 May 2010 14:06:56 -0400</pubDate></item><item><title>NEANDERTALS LIVE! | john hawks weblog</title><description>&lt;a href="http://bit.ly/9sPt1Z"&gt;NEANDERTALS LIVE! | john hawks weblog&lt;/a&gt;</description><link>http://evand.org/post/588005116</link><guid>http://evand.org/post/588005116</guid><pubDate>Mon, 10 May 2010 19:40:11 -0400</pubDate></item><item><title>That Which Does Not Kill Me Makes Me Stranger - New York Times</title><description>&lt;a href="http://www.nytimes.com/2006/02/05/sports/playmagazine/05robicpm.html?_r=1&amp;pagewanted=all"&gt;That Which Does Not Kill Me Makes Me Stranger - New York Times&lt;/a&gt;</description><link>http://evand.org/post/543740389</link><guid>http://evand.org/post/543740389</guid><pubDate>Fri, 23 Apr 2010 15:52:18 -0400</pubDate></item><item><title>The Guts of a New Machine</title><description>&lt;a href="http://www.nytimes.com/2003/11/30/magazine/30IPOD.html?ex=1386133200&amp;en=750c9021e58923d5&amp;ei=5007&amp;partner=USERLAND&amp;pagewanted=print"&gt;The Guts of a New Machine&lt;/a&gt;</description><link>http://evand.org/post/528485059</link><guid>http://evand.org/post/528485059</guid><pubDate>Sat, 17 Apr 2010 12:54:20 -0400</pubDate></item><item><title>mrgan:

Infographics are the new animated gifs.</title><description>&lt;img src="http://28.media.tumblr.com/tumblr_l0m9gtspB01qz50x3o1_500.png"/&gt;&lt;br/&gt;&lt;br/&gt;&lt;p&gt;&lt;a href="http://mrgan.tumblr.com/post/508462349/infographics-are-the-new-animated-gifs" class="tumblr_blog"&gt;mrgan&lt;/a&gt;:&lt;/p&gt;

&lt;blockquote&gt;&lt;p&gt;Infographics are the new animated gifs.&lt;/p&gt;&lt;/blockquote&gt;</description><link>http://evand.org/post/508532906</link><guid>http://evand.org/post/508532906</guid><pubDate>Fri, 09 Apr 2010 12:33:56 -0400</pubDate></item><item><title>Millie Loves Milk</title><description>&lt;object width="400" height="336"&gt;&lt;param name="movie" value="http://www.youtube.com/v/IboKXIdkmdA&amp;rel=0&amp;egm=0&amp;showinfo=0&amp;fs=1"&gt;&lt;/param&gt;&lt;param name="wmode" value="transparent"&gt;&lt;/param&gt;&lt;param name="allowFullScreen" value="true"&gt;&lt;/param&gt;&lt;embed src="http://www.youtube.com/v/IboKXIdkmdA&amp;rel=0&amp;egm=0&amp;showinfo=0&amp;fs=1" type="application/x-shockwave-flash" width="400" height="336" allowFullScreen="true" wmode="transparent"&gt;&lt;/embed&gt;&lt;/object&gt;&lt;br/&gt;&lt;br/&gt;&lt;p&gt;&lt;a href="http://www.youtube.com/watch?v=IboKXIdkmdA&amp;feature=youtube_gdata"&gt;Millie Loves Milk&lt;/a&gt;&lt;/p&gt;</description><link>http://evand.org/post/479428241</link><guid>http://evand.org/post/479428241</guid><pubDate>Sun, 28 Mar 2010 12:00:45 -0400</pubDate></item><item><title>Princeton researchers find that high-fructose corn syrup prompts considerably more weight gain</title><description>&lt;a href="http://www.princeton.edu/main/news/archive/S26/91/22K07/"&gt;Princeton researchers find that high-fructose corn syrup prompts considerably more weight gain&lt;/a&gt;: &lt;p&gt;&lt;blockquote&gt;A Princeton University research team has demonstrated that all sweeteners are not equal when it comes to weight gain: Rats with access to high-fructose corn syrup gained significantly more weight than those with access to table sugar, even when their overall caloric intake was the same.&lt;/blockquote&gt;

And they didn’t just gain more weight—they had higher levels of triglycerides, as well—both symptoms of metabolic syndrome in humans.&lt;/p&gt;</description><link>http://evand.org/post/467085403</link><guid>http://evand.org/post/467085403</guid><pubDate>Mon, 22 Mar 2010 23:15:54 -0400</pubDate></item><item><title>The case for the 3G-capable iPad | Tablets | iPhone Central | Macworld</title><description>&lt;a href="http://www.macworld.com/article/147189/2010/03/3g_ipad.html?lsrc=twt_jsnell"&gt;The case for the 3G-capable iPad | Tablets | iPhone Central | Macworld&lt;/a&gt;</description><link>http://evand.org/post/457363626</link><guid>http://evand.org/post/457363626</guid><pubDate>Thu, 18 Mar 2010 17:46:31 -0400</pubDate></item><item><title>There are so many problems with this.</title><description>&lt;img src="http://30.media.tumblr.com/tumblr_kym5xsfFsQ1qz4bnfo1_500.png"/&gt;&lt;br/&gt;&lt;br/&gt;&lt;p&gt;There are so many problems with this.&lt;/p&gt;</description><link>http://evand.org/post/420149593</link><guid>http://evand.org/post/420149593</guid><pubDate>Mon, 01 Mar 2010 12:29:00 -0500</pubDate></item><item><title>Adventures in C++: Disappearing Strings</title><description>&lt;p&gt;The first project for my Distributed Systems class must be done in C++. The majority of my coding efforts, however, have been in C, Objective-C, or Java. And while this project could have easily been hacked together using, essentially, only C, I decided to take the three weeks we had for the project to learn to write the best C++ solution I could.&lt;/p&gt;

&lt;p&gt;After a few days of fumbling around with C++’s syntax and reading through countless online tutorials, though, I realized that there was lots of conflicting advice about the best ways to write C++ scattered about the web. Mired in lots of syntax, terminology, and design choices I didn’t understand, I ordered &lt;cite&gt;&lt;a href="http://www.amazon.com/gp/product/0321334876?ie=UTF8&amp;tag=evansdeadtreecol&amp;linkCode=as2&amp;camp=1789&amp;creative=390957&amp;creativeASIN=0321334876"&gt;Effective C++&lt;/a&gt;&lt;/cite&gt;, by Scott Meyers. After a few hours of reading, lots of things suddenly clicked, and I started to lay down some code.&lt;/p&gt;

&lt;h3&gt;The Bug&lt;/h3&gt;

&lt;p&gt;In a day or so, I found myself with a simple, seemingly well-structured prototype. Unfortunately, it was a prototype that would blow up with a segmentation fault whenever I tried to run it. So I fired up &lt;code&gt;gdb&lt;/code&gt;. And, strangely, the program was crashing when an instance of a class, &lt;code&gt;Password&lt;/code&gt;, was just trying to access &lt;code&gt;value_&lt;/code&gt;, one of its member variables:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Program received signal EXC_BAD_ACCESS, Could not access memory.
Reason: KERN_INVALID_ADDRESS at address: 0xfffffffffffffff8
0x00007fff84b510aa in std::string::_Rep::_M_grab ()

(gdb) info stack
#0  0x00007fff84b510aa in std::string::_Rep::_M_grab ()
#1  0x00007fff84b51171 in std::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt;::basic_string ()
#2  0x00000001000029a0 in esd::Password::get_value (this=0x1001000a0) at password.h:19
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This seemed really weird, because &lt;code&gt;get_value()&lt;/code&gt; was declared like this:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;std::string get_value() const { return value_; }
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;with &lt;code&gt;value_&lt;/code&gt; itself declared as:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;const std::string value_;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;and, as all &lt;code&gt;const&lt;/code&gt; member variables should be, &lt;code&gt;value_&lt;/code&gt; was set in &lt;code&gt;Password&lt;/code&gt;’s only constructor’s initialization list:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Password::Password(const std::string&amp; value) : value_(value) {}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;It was easy to see that the string passed in to the constructor was, indeed, quite legitimate:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Breakpoint 1, esd::Password::Password (this=0x1001000a0, value=@0x7fff5fbff490) at password.cc:40
(gdb) p value
$1 = (const string &amp;) @0x7fff5fbff490: {
    static npos = 18446744073709551615, 
    _M_dataplus = {
        &lt;std::allocator&lt;char&gt;&gt; = {
            &lt;__gnu_cxx::new_allocator&lt;char&gt;&gt; = {&lt;No data fields&gt;}, &lt;No data fields&gt;},
        members of std::basic_string&lt;char,std::char_traits&lt;char&gt;,std::allocator&lt;char&gt; &gt;::_Alloc_hider: 
        _M_p = 0x100100098 "AA"
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;and that, immediately after the execution of the constructor, the object in &lt;code&gt;value_&lt;/code&gt; was a perfect copy of that passed-in string:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(gdb) p value_
$2 = {
    static npos = 18446744073709551615, 
    _M_dataplus = {
        &lt;std::allocator&lt;char&gt;&gt; = {
            &lt;__gnu_cxx::new_allocator&lt;char&gt;&gt; = {&lt;No data fields&gt;}, &lt;No data fields&gt;}, 
        members of std::basic_string&lt;char,std::char_traits&lt;char&gt;,std::allocator&lt;char&gt; &gt;::_Alloc_hider: 
        _M_p = 0x100100098 "AA"
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;But &lt;code&gt;gdb&lt;/code&gt; also revealed that, at the time &lt;code&gt;get_value()&lt;/code&gt; was invoked, &lt;code&gt;value_&lt;/code&gt; wasn’t looking so great:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(gdb) frame 2
#2  0x00000001000029a0 in esd::Password::get_value (this=0x1001000a0) at password.h:19
(gdb) p value_
$3 = {
    static npos = 18446744073709551615, 
    _M_dataplus = {
        &lt;std::allocator&lt;char&gt;&gt; = {
            &lt;__gnu_cxx::new_allocator&lt;char&gt;&gt; = {&lt;No data fields&gt;}, &lt;No data fields&gt;},
        members of std::basic_string&lt;char,std::char_traits&lt;char&gt;,std::allocator&lt;char&gt; &gt;::_Alloc_hider: 
        _M_p = 0x0
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Specifically, &lt;code&gt;_M_dataplus&lt;/code&gt;’s &lt;code&gt;_M_p&lt;/code&gt; had gone from &lt;code&gt;0x100100098&lt;/code&gt;, which contained &lt;code&gt;"AA"&lt;/code&gt;, to &lt;code&gt;0x0&lt;/code&gt;, which, well, didn’t contain anything.&lt;/p&gt;

&lt;p&gt;In other words, based on my knowledge of C++, it appeared as though the sequence of events leading up to the crash was as follows:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;The &lt;code&gt;Password::Password(const std::string&amp;)&lt;/code&gt; constructor is invoked with a reference to a &lt;code&gt;std::string&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The constructor’s initialization list code passed that reference to &lt;code&gt;std::string(const std::string&amp; str)&lt;/code&gt;, &lt;code&gt;std::string&lt;/code&gt;’s copy constructor, and placed the resulting copy in the &lt;code&gt;const&lt;/code&gt; &lt;code&gt;value_&lt;/code&gt; member variable.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;At some later point, &lt;code&gt;value_&lt;/code&gt;, which, as noted above, was &lt;em&gt;constant&lt;/em&gt;, is somehow transformed such that the actual string data it contains goes from the correct, original value (&lt;code&gt;"AA"&lt;/code&gt;) to an incorrect value (&lt;code&gt;0x0&lt;/code&gt;).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;After &lt;code&gt;value_&lt;/code&gt; has been mangled, someone invokes &lt;code&gt;Password::get_value()&lt;/code&gt;, which is an implicitly inlined accessor method defined to &lt;code&gt;return value_&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;Password::get_value()&lt;/code&gt; is defined to return a &lt;code&gt;std::string&lt;/code&gt;, so the string copy constructor is invoked to make the copy of &lt;code&gt;value_&lt;/code&gt; that will be returned.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The string copy constructor causes a segmentation fault, because &lt;code&gt;value_&lt;/code&gt; is no longer a valid string.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;The task, then, was to figure out what happened in Step 3: how did a &lt;code&gt;const&lt;/code&gt; string, copied from another string on initialization, end up becoming invalid?&lt;/p&gt;

&lt;h3&gt;The Frustration&lt;/h3&gt;

&lt;p&gt;I worked hard on tracking down this issue for two days, and considered a wide range of theories. Did I not understand how initializer lists worked? Was I failing to grasp the meaning of copy constructors? Should I be storing pointers to strings instead of the strings themselves?&lt;/p&gt;

&lt;p&gt;I thought that, after reading &lt;cite&gt;Effective C++&lt;/cite&gt;, I had formed a correct basic understanding of how the language worked, but each of these theories seemed to invalidate that understanding. I tried to keep in mind, however, that every good, hard bit of learning I had ever accomplished had felt just as frustrating as this at times. Whenever I tried something new, I would start out knowing that I knew nothing, move to knowing enough to put together a toy example, learn enough that I &lt;em&gt;thought&lt;/em&gt; I had a solid foundation, and then, finally, work through a series of really difficult problems in which I found out that my foundation was missing pieces or put together slightly wrong. I was in this last portion, and I knew it. My only way out was to work as hard as I could at figuring out what I wasn’t seeing.&lt;/p&gt;

&lt;p&gt;Once I had reread all of the relevant Google results and applicable chapters in &lt;cite&gt;Effective C++&lt;/cite&gt;, though, I still didn’t feel like I was any closer to solving the problem. But then, just as I was about to reach out to &lt;a href="http://www.stackoverflow.com"&gt;Stack Overflow&lt;/a&gt; for help, I thought of &lt;a href="http://valgrind.org/"&gt;Valgrind&lt;/a&gt;, which I had heard could help track down some of the trickiest of memory issues. So I gave it a try.&lt;/p&gt;

&lt;h3&gt;The Problem&lt;/h3&gt;

&lt;p&gt;That was, as they say, a &lt;em&gt;good call&lt;/em&gt;. As soon as Valgrind started up, it gave me this:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;==25468== Invalid read of size 8
==25468==    at 0x50F88BE: std::string::string(std::string const&amp;) (in /usr/lib64/libstdc++.so.6.0.8)
==25468==    by 0x403133: esd::Password::get_value() const (password.h:19)
==25468==    by 0x4029FF: esd::Password::IncrementPlace(unsigned long) const (password.cc:52)
==25468==    by 0x403373: esd::WorkUnit::get_passwords() const (work_unit.cc:37)
==25468==    by 0x4034DB: esd::WorkUnit::get_last_password() const (work_unit.cc:27)
==25468==    by 0x404BF2: esd::PasswordSpace::get_work_unit_queue() const (password_space.cc:85)
==25468==    by 0x405023: esd::PasswordSpace::CheckOutWorkUnit() (password_space.cc:41)
==25468==    by 0x401C57: main (server.cc:52)
==25468==  Address 0x405C080 is 0 bytes inside a block of size 8 free'd
==25468==    at 0x4C20130: operator delete(void*) (vg_replace_malloc.c:244)
==25468==    by 0x4021A2: std::tr1::_Sp_deleter&lt;esd::Password&gt;::operator()(esd::Password*) const (boost_shared_ptr.h:93)
==25468==    by 0x40229E: std::tr1::_Sp_counted_base_impl&lt;esd::Password*, std::tr1::_Sp_deleter&lt;esd::Password&gt; &gt;::dispose() (boost_shared_ptr.h:215)
==25468==    by 0x4022E0: std::tr1::_Sp_counted_base::release() (boost_shared_ptr.h:153)
==25468==    by 0x402339: std::tr1::shared_count::~shared_count() (boost_shared_ptr.h:277)
==25468==    by 0x4023A8: std::tr1::shared_ptr&lt;esd::Password&gt;::~shared_ptr() (boost_shared_ptr.h:486)
==25468==    by 0x402FAA: esd::Password::DistanceTo(std::tr1::shared_ptr&lt;esd::Password&gt;) const (password.cc:113)
==25468==    by 0x404AB6: esd::PasswordSpace::get_work_unit_queue() const (password_space.cc:79)
==25468==    by 0x405023: esd::PasswordSpace::CheckOutWorkUnit() (password_space.cc:41)
==25468==    by 0x401C57: main (server.cc:52)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;That was exactly what I needed to see. &lt;code&gt;std::string::string(std::string const&amp;)&lt;/code&gt; was crashing because it was trying to access memory that had been freed. Specifically, that freed memory was &lt;em&gt;the password itself&lt;/em&gt;. Even better, Valgrind could tell me &lt;em&gt;who&lt;/em&gt; and &lt;em&gt;where&lt;/em&gt; that memory was freed. In this case, it was &lt;code&gt;esd::Password::DistanceTo(std::tr1::shared_ptr&lt;esd::Password&gt;)&lt;/code&gt;, on line 133 of &lt;code&gt;password.cc&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;It turns out that line 133 of &lt;code&gt;password.cc&lt;/code&gt; is simply where &lt;code&gt;DistanceTo(…)&lt;/code&gt; returns the distance it has computed. That’s important, but it doesn’t explain why &lt;code&gt;DistanceTo(…)&lt;/code&gt; deletes the object on which it is invoked. Looking at the whole method in conjunction with the Valgrind output, however, flushes the bug out:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;size_t Password::DistanceTo(const std::tr1::shared_ptr&lt;Password&gt; other) const {
    if (Equals(other)) {
        return 0;
    }

    int comparison = CompareTo(other);
    std::tr1::shared_ptr&lt;Password&gt; this_shared_ptr(const_cast&lt;Password*&gt;(this));
    std::tr1::shared_ptr&lt;Password&gt; start;
    std::tr1::shared_ptr&lt;Password&gt; end;
    if (comparison &lt; 0) {
        start = this_shared_ptr;
        end = other;
    } else {
        start = other;
        end = this_shared_ptr;
    }

    size_t distance = 0;
    while (!start-&gt;Equals(end)) {
        distance++;
        start = start-&gt;NextPassword();
    }
    return distance;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;All I’m trying to do here figure out the absolute distance, in some ordering, from the password on which &lt;code&gt;DistanceTo(…)&lt;/code&gt; was invoked to some other password. To accomplish this, I’m figuring out which password comes before the other and then counting how many increments of the earlier password it takes to get to the later password. (There are likely faster ways to do this, but, for an initial prototype in a new language, I just wanted something that I could write clearly and quickly.)&lt;/p&gt;

&lt;p&gt;Here’s the problem:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;std::tr1::shared_ptr&lt;Password&gt; this_shared_ptr(const_cast&lt;Password*&gt;(this));
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This line tried to obtain a &lt;code&gt;smart_ptr&lt;Password&gt;&lt;/code&gt; to &lt;code&gt;this&lt;/code&gt;, so that I could assign either &lt;code&gt;start&lt;/code&gt; or &lt;code&gt;end&lt;/code&gt; to it, depending on whether &lt;code&gt;this&lt;/code&gt; came before &lt;code&gt;other&lt;/code&gt; in the password ordering. But &lt;code&gt;this&lt;/code&gt; is &lt;code&gt;const&lt;/code&gt; inside of &lt;code&gt;DistanceTo(…)&lt;/code&gt;, since the method itself is &lt;code&gt;const&lt;/code&gt;, and so, in my naïvety, I thought, “Well, I’ll just remove the &lt;code&gt;const&lt;/code&gt; from &lt;code&gt;this&lt;/code&gt; with a &lt;code&gt;const_cast&lt;Password*&gt;&lt;/code&gt;, and that will solve my problem!”&lt;/p&gt;

&lt;p&gt;And it &lt;em&gt;did&lt;/em&gt; solve the problem of &lt;code&gt;this&lt;/code&gt; being &lt;code&gt;const&lt;/code&gt;, but it created a much larger problem: I now have at least two &lt;code&gt;shared_ptr&lt;/code&gt; objects that point to the password instance: the one that was used to invoke &lt;code&gt;DistanceTo(…)&lt;/code&gt;, and &lt;code&gt;this_shared_ptr&lt;/code&gt;. Unfortunately, creating two &lt;code&gt;shared_ptr&lt;/code&gt;s that point to the same object but that don’t know about each other is a recipe for disaster, since they don’t know that they’re sharing ownership. So, when &lt;code&gt;DistanceTo(…)&lt;/code&gt; returns, it deletes &lt;code&gt;this_shared_ptr&lt;/code&gt;, which, in turn, deletes the object it points to—the password instance—because it doesn’t think that any other &lt;code&gt;shared_ptr&lt;/code&gt; is pointing to it.&lt;/p&gt;

&lt;p&gt;Ultimately, then, when someone else with a &lt;code&gt;shared_ptr&lt;/code&gt; to that password instance uses it to try and invoke &lt;code&gt;get_value()&lt;/code&gt;, we get a segmentation fault, because the memory that once contained the member &lt;code&gt;value_&lt;/code&gt; has been freed.&lt;/p&gt;

&lt;h3&gt;The Solution&lt;/h3&gt;

&lt;p&gt;So, how can I get a correct &lt;code&gt;shared_ptr&lt;/code&gt; to &lt;code&gt;this&lt;/code&gt;? One solution is described in &lt;cite&gt;&lt;a href="http://www.boost.org/doc/libs/1_41_0/libs/smart_ptr/sp_techniques.html"&gt;Boost Smart Pointer Programming Techniques&lt;/a&gt;&lt;/cite&gt;, which has a section called, appropriately, &lt;a href="http://www.boost.org/doc/libs/1_41_0/libs/smart_ptr/sp_techniques.html#from_this"&gt;Obtaining a &lt;code&gt;shared_ptr&lt;/code&gt; to &lt;code&gt;this&lt;/code&gt;&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;After thinking about the problem for a bit longer, though, I chose a different, more straightforward approach: instead of trying to obtain a &lt;code&gt;shared_ptr&lt;/code&gt; to &lt;code&gt;this&lt;/code&gt;, I simply created a new &lt;code&gt;Password&lt;/code&gt; with the same value as &lt;code&gt;this&lt;/code&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;std::tr1::shared_ptr&lt;Password&gt; this_copy = std::tr1::shared_ptr&lt;Password&gt;(new Password(get_value()));
std::tr1::shared_ptr&lt;Password&gt; start;
std::tr1::shared_ptr&lt;Password&gt; end;
if (comparison &lt; 0) {
    start = this_copy;
    end = other;
} else {
    start = other;
    end = this_copy;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;It might not be quite as memory efficient as the technique discussed in the Boost documentation, but, in my case, just using a copy of &lt;code&gt;this&lt;/code&gt; is clean and simple. In addition, the use of a copy makes it easier to see that &lt;code&gt;DistanceTo(…)&lt;/code&gt; really is &lt;code&gt;const&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;The End&lt;/h3&gt;

&lt;p&gt;This is a long post about what, ultimately, was a simple problem that resulted from my inexperience. I already feel a bit silly when I think back on the bug, since, in retrospect, the issue seems so &lt;em&gt;obvious&lt;/em&gt;—why didn’t I think through the consequences of creating a new &lt;code&gt;shared_ptr&lt;/code&gt; to &lt;code&gt;this&lt;/code&gt;?—and my path to the solution so convoluted—why didn’t I just run Valgrind right away?&lt;/p&gt;

&lt;p&gt;But three hours ago, I wasn’t sure what to try next, while, now, I can’t see how I could have ever been so lost. Some time soon, I’ll likely be struggling for hours before writing a post about that &lt;em&gt;next&lt;/em&gt; simple problem. But the fight to a solution will have made me a better programmer, just as it has this time. That’s worth the effort.&lt;/p&gt;</description><link>http://evand.org/post/376789183</link><guid>http://evand.org/post/376789183</guid><pubDate>Sun, 07 Feb 2010 17:12:09 -0500</pubDate><category>C++</category><category>programming</category><category>debugging</category><category>gdb</category><category>homework</category></item><item><title>I’m selling my first-generation Amazon Kindle on Internet...</title><description>&lt;img src="http://29.media.tumblr.com/tumblr_kx2wg7IJ1l1qz4bnfo1_500.jpg"/&gt;&lt;br/&gt;&lt;br/&gt;&lt;p&gt;&lt;a href="http://bit.ly/a72mvS"&gt;I’m selling my first-generation Amazon Kindle on Internet Garage Sale&lt;/a&gt;.&lt;/p&gt;</description><link>http://evand.org/post/361916786</link><guid>http://evand.org/post/361916786</guid><pubDate>Sat, 30 Jan 2010 16:16:55 -0500</pubDate></item><item><title>Back to the Land</title><description>&lt;a href="http://kalman.blogs.nytimes.com/2009/11/26/back-to-the-land/"&gt;Back to the Land&lt;/a&gt;: &lt;blockquote&gt;
  &lt;p&gt;“If you eat too much of this food, you become sick and also fatafat. And no amount of fatafat pills will help you.”&lt;/p&gt;
&lt;/blockquote&gt;</description><link>http://evand.org/post/259687475</link><guid>http://evand.org/post/259687475</guid><pubDate>Fri, 27 Nov 2009 13:07:30 -0500</pubDate></item><item><title>As seen in the 5th floor men’s bathroom in the...</title><description>&lt;img src="http://24.media.tumblr.com/tumblr_kslq8jSXZU1qz4bnfo1_500.jpg"/&gt;&lt;br/&gt;&lt;br/&gt;&lt;p&gt;As seen in the 5th floor men’s bathroom in the Gates-Hillman Center.&lt;/p&gt;</description><link>http://evand.org/post/233156648</link><guid>http://evand.org/post/233156648</guid><pubDate>Wed, 04 Nov 2009 15:28:10 -0500</pubDate></item><item><title>A Conversation with Millie</title><description>&lt;object width="400" height="336"&gt;&lt;param name="movie" value="http://www.youtube.com/v/7N_A5ePfIVk&amp;rel=0&amp;egm=0&amp;showinfo=0&amp;fs=1"&gt;&lt;/param&gt;&lt;param name="wmode" value="transparent"&gt;&lt;/param&gt;&lt;param name="allowFullScreen" value="true"&gt;&lt;/param&gt;&lt;embed src="http://www.youtube.com/v/7N_A5ePfIVk&amp;rel=0&amp;egm=0&amp;showinfo=0&amp;fs=1" type="application/x-shockwave-flash" width="400" height="336" allowFullScreen="true" wmode="transparent"&gt;&lt;/embed&gt;&lt;/object&gt;&lt;br/&gt;&lt;br/&gt;&lt;p&gt;&lt;a href="http://www.youtube.com/watch?v=7N_A5ePfIVk&amp;feature=youtube_gdata"&gt;A Conversation with Millie&lt;/a&gt;&lt;/p&gt;</description><link>http://evand.org/post/190761979</link><guid>http://evand.org/post/190761979</guid><pubDate>Fri, 18 Sep 2009 01:08:58 -0400</pubDate></item><item><title>Everything Is Terrible!: ADVANCED DOS STRATEGIES! (via...</title><description>&lt;object type="application/x-shockwave-flash" width="400" height="270" data="http://vimeo.com/moogaloop.swf?clip_id=6501196&amp;server=vimeo.com&amp;fullscreen=1&amp;show_title=1&amp;show_byline=0&amp;show_portrait=0&amp;color=00ADEF"&gt;&lt;param name="quality" value="best" /&gt;&lt;param name="allowscriptaccess" value="always" /&gt;&lt;param name="allowfullscreen" value="true" /&gt;&lt;param name="scale" value="showAll" /&gt;&lt;param name="movie" value="http://vimeo.com/moogaloop.swf?clip_id=6501196&amp;server=vimeo.com&amp;fullscreen=1&amp;show_title=1&amp;show_byline=0&amp;show_portrait=0&amp;color=00ADEF" /&gt;&lt;embed src="http://www.vimeo.com/moogaloop.swf?clip_id=6501196&amp;server=www.vimeo.com&amp;show_title=1&amp;show_byline=0&amp;show_portrait=0&amp;color=00ADEF&amp;fullscreen=1" type="application/x-shockwave-flash" allowfullscreen="true" allowscriptaccess="always" width="400" height="270"&gt;&lt;/embed&gt;&lt;/object&gt;&lt;br/&gt;&lt;br/&gt;&lt;p&gt;&lt;a href="http://www.everythingisterrible.com/2009/09/advanced-dos-strategies.html"&gt;Everything Is Terrible!: ADVANCED DOS STRATEGIES!&lt;/a&gt; (via &lt;a href="http://www.metafilter.com/84973/Advanced-DOS-Strategies"&gt;MetaFilter&lt;/a&gt;)&lt;/p&gt;</description><link>http://evand.org/post/186277903</link><guid>http://evand.org/post/186277903</guid><pubDate>Sat, 12 Sep 2009 14:36:00 -0400</pubDate></item><item><title>Zach Loves Sausage</title><description>&lt;object width="400" height="336"&gt;&lt;param name="movie" value="http://www.youtube.com/v/gTFk7IRmhAQ&amp;rel=0&amp;egm=0&amp;showinfo=0&amp;fs=1"&gt;&lt;/param&gt;&lt;param name="wmode" value="transparent"&gt;&lt;/param&gt;&lt;param name="allowFullScreen" value="true"&gt;&lt;/param&gt;&lt;embed src="http://www.youtube.com/v/gTFk7IRmhAQ&amp;rel=0&amp;egm=0&amp;showinfo=0&amp;fs=1" type="application/x-shockwave-flash" width="400" height="336" allowFullScreen="true" wmode="transparent"&gt;&lt;/embed&gt;&lt;/object&gt;&lt;br/&gt;&lt;br/&gt;&lt;p&gt;&lt;a href="http://www.youtube.com/watch?v=gTFk7IRmhAQ"&gt;Zach Loves Sausage&lt;/a&gt;&lt;/p&gt;</description><link>http://evand.org/post/175254251</link><guid>http://evand.org/post/175254251</guid><pubDate>Sun, 30 Aug 2009 01:33:03 -0400</pubDate></item><item><title>Jackson at Bark in the Park 2009</title><description>&lt;object width="400" height="336"&gt;&lt;param name="movie" value="http://www.youtube.com/v/udw9QK9BBdE&amp;rel=0&amp;egm=0&amp;showinfo=0&amp;fs=1"&gt;&lt;/param&gt;&lt;param name="wmode" value="transparent"&gt;&lt;/param&gt;&lt;param name="allowFullScreen" value="true"&gt;&lt;/param&gt;&lt;embed src="http://www.youtube.com/v/udw9QK9BBdE&amp;rel=0&amp;egm=0&amp;showinfo=0&amp;fs=1" type="application/x-shockwave-flash" width="400" height="336" allowFullScreen="true" wmode="transparent"&gt;&lt;/embed&gt;&lt;/object&gt;&lt;br/&gt;&lt;br/&gt;&lt;p&gt;&lt;a href="http://www.youtube.com/watch?v=udw9QK9BBdE"&gt;Jackson at Bark in the Park 2009&lt;/a&gt;&lt;/p&gt;</description><link>http://evand.org/post/169808476</link><guid>http://evand.org/post/169808476</guid><pubDate>Sun, 23 Aug 2009 14:19:50 -0400</pubDate></item><item><title>The Banality of Evil</title><description>&lt;object width="400" height="336"&gt;&lt;param name="movie" value="http://www.youtube.com/v/R8MLCoLcHKw&amp;rel=0&amp;egm=0&amp;showinfo=0&amp;fs=1"&gt;&lt;/param&gt;&lt;param name="wmode" value="transparent"&gt;&lt;/param&gt;&lt;param name="allowFullScreen" value="true"&gt;&lt;/param&gt;&lt;embed src="http://www.youtube.com/v/R8MLCoLcHKw&amp;rel=0&amp;egm=0&amp;showinfo=0&amp;fs=1" type="application/x-shockwave-flash" width="400" height="336" allowFullScreen="true" wmode="transparent"&gt;&lt;/embed&gt;&lt;/object&gt;&lt;br/&gt;&lt;br/&gt;&lt;p&gt;&lt;a href="http://www.youtube.com/watch?v=R8MLCoLcHKw"&gt;The Banality of Evil&lt;/a&gt;&lt;/p&gt;</description><link>http://evand.org/post/151393519</link><guid>http://evand.org/post/151393519</guid><pubDate>Wed, 29 Jul 2009 01:59:14 -0400</pubDate></item></channel></rss>
