<?xml version="1.0" ?>
<rss version="2.0">
<channel>
 <title>Yvan Seth's Hole in the Internet</title>
 <link>http://yvan.seth.id.au</link>
 <description>Further Internetual randomness courtesy of Yvan Seth, turnip.</description>
 <language>en</language>
 <copyright>Copyright 2004-2007 Yvan Seth</copyright>
 <generator>blosxom 2.0 + vim</generator>
 <docs>http://blogs.law.harvard.edu/tech/rss</docs>
 <ttl>360</ttl>
<item>
  <title>Backporting an Ubuntu/Debian Package - spatialite for Jaunty</title>
  <link>http://yvan.seth.id.au/Entries/Technology/Code/Backporting_an_Ubuntu_Debian_Package_-_spatialite_for_Jaunty.html</link>
  <guid isPermaLink="true">http://yvan.seth.id.au/Entries/Technology/Code/Backporting_an_Ubuntu_Debian_Package_-_spatialite_for_Jaunty.html</guid>
  <category>/Entries/Technology/Code</category>
  <pubDate>Wed, 21 Oct 2009 22:03:00 GMT</pubDate>
  <description>

&lt;p&gt;
There are a lot more &quot;Debian&quot;, aka Ubuntu, users around the place these days.  In
general it seems many newer users have been spoilt by Ubuntu's regular release
schedule.  Back in the old days we sometimes waited &lt;em&gt;years&lt;/em&gt; between Debian
releases, if there was something you needed that wasn't in your antique but
&quot;stable&quot; Debian you'd have to use backports, if you were lucky enough to find
what you wanted there, backport a package for yourself (thanks to the Debian
package build tools being mostly friendly), or just compile from source.
&lt;/p&gt;

&lt;p&gt;
Perhaps self-backporting has become an anachrnism?  Still, I find myself doing
it from time to time.  Right now I want &lt;code style=&quot;background:#eee;font-weight:bold;&quot;&gt;spatialite&lt;/code&gt; because I'm playing with
GeoDjango and don't want to mess with PostgreSQL or MySQL &amp;ndash; I'm just
fiddling.  In general on the 'net you find people are just grabbing precompiled
binary tarballs or, &lt;em&gt;shudder&lt;/em&gt;, the game are self-compiling and doing a &lt;code style=&quot;background:#eee;font-weight:bold;&quot;&gt;make
install&lt;/code&gt; &amp;hellip; won't somebody think about the package management system!
&lt;/p&gt;

&lt;p&gt;
Still another approach is to just upgrade to &quot;testing&quot; (or is it &quot;unstable&quot; - I
think &quot;stable&quot; Ubuntu is closer to what we might have called &quot;testing&quot; Debian
back in the day.) But the koala isn't fully baked yet, so why risk that?
Backporting the karmic package is safer, and probably quicker.  (Yes, yes, I know
there's only 10 more days of baking to go.)
&lt;/p&gt;

&lt;p&gt;
Here's a recipe to help avoid unbaked koalas:
&lt;/p&gt;

&lt;pre class=&quot;code&quot;&gt;
sudo -s
&lt;span style=&quot;color:green;&quot;&gt;# Put the Karmic 'universe' deb-src line into your sources&lt;/span&gt;
echo &quot;deb-src http://gb.archive.ubuntu.com/ubuntu/ karmic universe&quot; &gt; \
    /etc/apt/sources.list.d/karmic-universe.list
&lt;span style=&quot;color:green;&quot;&gt;# Install a few pre-requsites 
# you may find you need more than this
# but you'll definitely need at least the following&lt;/span&gt;
apt-get install autotools-dev debhelper doxygen dpatch dpkg-dev fakeroot \
    libgeos-dev libsqlite3-dev quilt ruby ruby-dev sharutils swig
exit

&lt;span style=&quot;color:green;&quot;&gt;# Get, compile, and install a newer libgeos&lt;/span&gt;
apt-get source libgeos-dev
cd geos-3.1.0
dpkg-buildpackage -rfakeroot
cd ..
sudo dpkg -i libgeos-3.1.0_3.1.0-1_i386.deb \
    libgeos-dev_3.1.0-1_i386.deb libgeos-c1_3.1.0-1_i386.deb

&lt;span style=&quot;color:green;&quot;&gt;# Get, compile, and install libproj&lt;/span&gt;
apt-get source libproj-dev
cd proj-4.6.1
dpkg-buildpackage -rfakeroot
cd ..
sudo dpkg -i libproj0_4.6.1-5_i386.deb \
    libproj-dev_4.6.1-5_i386.deb  proj-data_4.6.1-5_i386.deb

&lt;span style=&quot;color:green;&quot;&gt;# Get, compile, and install spatialite - see a pattern emerging?&lt;/span&gt;
apt-get source spatialite
cd spatialite-2.3.0
dpkg-buildpackage -rfakeroot
cd ..
sudo dpkg -i libspatialite2_2.3.0-1_i386.deb  spatialite-bin_2.3.0-1_i386.deb
&lt;/pre&gt;

&lt;p&gt;
The advantage of this approach is that your additional bleeding edge software
is installed &quot;properly&quot;.  The package management system is aware of it, and
when the koala is baked and you upgrade you'll be certain to get any important
updates.
&lt;/p&gt;

&lt;p&gt;
Of course, the sequence above makes it look like plain sailing.  The reality of
the process is that you start by adding the &lt;code style=&quot;background:#eee;font-weight:bold;&quot;&gt;deb-src&lt;/code&gt; line, then you get the
&lt;code style=&quot;background:#eee;font-weight:bold;&quot;&gt;spatialite&lt;/code&gt; source, then you try to build it.  The build fails with a long
list of the packages it needs as build dependencies.  You install each of the
build dependencies as required, grabbing some from the &quot;unstable&quot; source
repository when needed (as for &lt;code style=&quot;background:#eee;font-weight:bold;&quot;&gt;libgeos&lt;/code&gt; and &lt;code style=&quot;background:#eee;font-weight:bold;&quot;&gt;libproj&lt;/code&gt; above.)  In this case
it all came from &lt;code style=&quot;background:#eee;font-weight:bold;&quot;&gt;universe&lt;/code&gt;, so my restrictive &lt;code style=&quot;background:#eee;font-weight:bold;&quot;&gt;deb-src&lt;/code&gt; line was fine, but
usually you'll find you need things from &quot;main&quot; or even &quot;multiverse&quot; as well.
Be warned, if you start needing to upgrade things like your &lt;code style=&quot;background:#eee;font-weight:bold;&quot;&gt;libc&lt;/code&gt; it is time
to stop and either venture the &lt;code style=&quot;background:#eee;font-weight:bold;&quot;&gt;dist-upgrade&lt;/code&gt; or resort to &lt;code style=&quot;background:#eee;font-weight:bold;&quot;&gt;make install&lt;/code&gt;
(or try hacking the Debian package source to lower a dependency version;)
&lt;/p&gt;

&lt;p&gt;
This post is a little tongue-in-cheek in some ways.  I wouldn't expect people,
developers especially, to stick with &quot;stable&quot; religiously - nothing ventured
nothing gained! But I decided to pollute the 'net with these thoughts after
coming across some young developers who had never seen the &lt;code style=&quot;background:#eee;font-weight:bold;&quot;&gt;apt-get source&lt;/code&gt;
command, let alone built a Debian package (die-hard Ubuntu fans who're young
enough that Ubuntu was the first &quot;Debian&quot; they installed.) No, don't risk the
koala I shouted!  It is not the right way!  (Then I hobbled back up to my
hermit hole while the young fellows pitied the poor old crazy man.)
&lt;/p&gt;

&lt;p&gt;
I do think backporting is usually a better approach than risking a premature
system upgrade though.  That said, we live in a pretty bleeding-edge world
these days.  I myself use most of my direct dependencies from SVN trunks, stuff
releases - they're old and dusty, and a lot of these newfangled
frameworks/technologies/whathaveyou even recommend this.
&lt;/p&gt;

&lt;p&gt;
Also, I'd be dishonest if I didn't mention that sometimes when trying to
backport something from proper Debian/Ubuntu package sources you can end up
chasing your tail for ages.  Quickly leading to frustration, and giving up,
followed by either a &lt;code style=&quot;background:#eee;font-weight:bold;&quot;&gt;make install&lt;/code&gt; or a &lt;code style=&quot;background:#eee;font-weight:bold;&quot;&gt;dist-upgrade&lt;/code&gt; &amp;hellip; ah,
technology.
&lt;/p&gt;

&lt;p&gt;
For the interested, after doing the above the rest of the process to get
my Django sqlite database primed for geospatial data was simply:
&lt;/p&gt;

&lt;pre class=&quot;code&quot;&gt;
wget http://www.gaia-gis.it/spatialite/init_spatialite-2.3.sql.gz
gunzip init_spatialite-2.3.sql.gz
spatialite mydatabase.db  &lt; init_spatialite-2.3.sql
./manage.py syncdb
&lt;/pre&gt;

&lt;p&gt;
Oh, you want the fancy Open Street Maps editor widget too do you?
&lt;/p&gt;

&lt;pre class=&quot;code&quot;&gt;
apt-get source libogdi3.2
sudo apt-get install python-gdal gdal-bin libgdal1-1.5.0 libogdi3.2
cd ogdi-dfsg-3.2.0~beta2
dpkg-buildpackage -rfakeroot
cd ..
sudo dpkg -i libogdi3.2_3.2.0~beta2-4_i386.deb ogdi-bin_3.2.0~beta2-4_i386.deb
sudo apt-get install python-gdal
./manage.py shell
&lt;span style=&quot;color:#000066;&quot;&gt;Python 2.6.2 (release26-maint, Apr 19 2009, 01:56:41) 
[GCC 4.3.3] on linux2
Type &quot;help&quot;, &quot;copyright&quot;, &quot;credits&quot; or &quot;license&quot; for more information.
(InteractiveConsole)
&gt;&gt;&gt; from django.contrib.gis.gdal import SpatialReference
&gt;&gt;&gt; from django.contrib.gis.utils import add_postgis_srs
&gt;&gt;&gt; add_postgis_srs(SpatialReference(900913))
&gt;&gt;&gt; from django.contrib.gis import gdal
&gt;&gt;&gt; gdal.HAS_GDAL
&lt;strong&gt;True&lt;/strong&gt;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;
w00t!
&lt;/p&gt;
  </description>
</item>
<item>
  <title>std::endl</title>
  <link>http://yvan.seth.id.au/Entries/Technology/Code/std__endl.html</link>
  <guid isPermaLink="true">http://yvan.seth.id.au/Entries/Technology/Code/std__endl.html</guid>
  <category>/Entries/Technology/Code</category>
  <pubDate>Thu, 03 Jul 2008 23:17:00 GMT</pubDate>
  <description>


&lt;p&gt;
All these years I've been making regular use of good old &lt;code style=&quot;background:#eee;font-weight:bold;&quot;&gt;std::endl&lt;/code&gt; without
realising that it is a templated &lt;em&gt;function&lt;/em&gt;!  We live in a crazy world.
Somewhere in the back of my head I guess I'd just assumed it was simply
platform-dependant constant&amp;hellip; not quite.
&lt;/p&gt;

&lt;p&gt;
I came across this merry discovery when implementing a logger, since
re-inventing the wheel is always so much fun.  It isn't as bad as it seems, the
&quot;logger&quot; is basically just a matter of encapsulating the relevant utility code,
weighing in at less than 500 lines of which more than half are API comments.
&lt;/p&gt;

&lt;p&gt;
Anyway, I'd like my logger to act like a &lt;code style=&quot;background:#eee;font-weight:bold;&quot;&gt;std::ostream&lt;/code&gt;, although I don't want
it to &lt;em&gt;be&lt;/em&gt; a &lt;code style=&quot;background:#eee;font-weight:bold;&quot;&gt;std::ostream&lt;/code&gt; really (preferring to avoid inheriting from such
beasts without a really good reason.)  The functionality is simple, it wraps a
set of &lt;code style=&quot;background:#eee;font-weight:bold;&quot;&gt;std::ostream&lt;/code&gt;s to send the output to.  The important part is that it
implements log levels and and log message classes (which you can register and
name if you wish.)  At any one time there is a log threshold and a set of
active classes.  To keep things really simple it has a single &lt;code style=&quot;background:#eee;font-weight:bold;&quot;&gt;output&lt;/code&gt; method
that takes only a &lt;em&gt;std::string&lt;/em&gt; ref.
&lt;/p&gt;

&lt;p&gt;
Now I want to use it with good old &lt;code style=&quot;background:#eee;font-weight:bold;&quot;&gt;&amp;lt;&amp;lt;&lt;/code&gt; since that is comfortable and
not unexpected so I implement:
&lt;/p&gt;

&lt;pre class=&quot;code&quot;&gt;
template &amp;lt;typename T&amp;gt;
Log&amp;amp; operator&amp;lt;&amp;lt;(Log &amp;amp; log, T const &amp;amp; t) {
    std::ostringstream oss;
    oss &amp;lt;&amp;lt; t;
    log.output(oss.str());
    return log;
}
&lt;/pre&gt;

&lt;p&gt;
This seems to work swimmingly until I try the likes of &lt;code style=&quot;background:#eee;font-weight:bold;&quot;&gt;log &lt;&lt; std::endl&lt;/code&gt;,
then it fails to compile.  Eh, what on earth &lt;em&gt;is&lt;/em&gt; &lt;code style=&quot;background:#eee;font-weight:bold;&quot;&gt;std::endl&lt;/code&gt; that the
template won't catch it?  A peak into the &lt;code style=&quot;background:#eee;font-weight:bold;&quot;&gt;ostream&lt;/code&gt; C++ header tells us it is
a function pointer:
&lt;/p&gt;

&lt;pre class=&quot;code&quot;&gt;
  template&amp;lt;typename _CharT, typename _Traits&amp;gt;
    inline basic_ostream&amp;lt;_CharT, _Traits&amp;gt;&amp;amp;.
    endl(basic_ostream&amp;lt;_CharT, _Traits&amp;gt;&amp;amp; __os)
    { return flush(__os.put(__os.widen('\n'))); }
&lt;/pre&gt;

&lt;p&gt;
Egads!  So I implement this (bear with me):
&lt;/p&gt;

&lt;pre class=&quot;code&quot;&gt;
Log&amp;amp; Ephedrine::operator&amp;lt;&amp;lt;(Log &amp;amp; log, std::ostream&amp;amp; (*fn)(std::ostream&amp;amp;)) {
    std::ostringstream oss;
    fn(oss);
    log.output(oss.str());
    return log;
}
&lt;/pre&gt;

&lt;p&gt;
And now I can do this:
&lt;/p&gt;

&lt;pre class=&quot;code&quot;&gt;
g_log &amp;lt;&amp;lt; Log::NORMAL &amp;lt;&amp;lt; &quot;Normal log output.  A number is &quot; &amp;lt;&amp;lt; 2.123 &amp;lt;&amp;lt; std::endl;
g_log &amp;lt;&amp;lt; Log::VERBOSE &amp;lt;&amp;lt; &quot;Verbose log output.&quot; &amp;lt;&amp;lt; std::endl;
&lt;/pre&gt;

&lt;p&gt;
Joy.
&lt;/p&gt;

&lt;p&gt;
But there's more than one worm in this can.  Note that in the definition above
it calls &lt;code style=&quot;background:#eee;font-weight:bold;&quot;&gt;flush&lt;/code&gt; on the &lt;code style=&quot;background:#eee;font-weight:bold;&quot;&gt;ostream&lt;/code&gt;.  In the doxygen comments it notes
&quot;&lt;em&gt;This manipulator is often mistakenly used when a simple newline is desired, leading to poor buffering performance.&lt;/em&gt;&quot;
On checking the standard there it is too, this is &lt;code style=&quot;background:#eee;font-weight:bold;&quot;&gt;std::endl&lt;/code&gt; by &lt;em&gt;definition&lt;/em&gt;.
I've never used &lt;code style=&quot;background:#eee;font-weight:bold;&quot;&gt;ostreams&lt;/code&gt;, thus never &lt;code style=&quot;background:#eee;font-weight:bold;&quot;&gt;std::endl&lt;/code&gt;, in performance critical
code but I'll have to keep this in mind if I ever do!  In all of the C++ books
I've read, a fairly large number, I don't recall ever seeing this noted anywhere.
&lt;/p&gt;

&lt;p&gt;
This brings into mind a question of design.  I've avoided being an
&lt;code style=&quot;background:#eee;font-weight:bold;&quot;&gt;std::ostream&lt;/code&gt; yet gone and used &lt;code style=&quot;background:#eee;font-weight:bold;&quot;&gt;std::endl&lt;/code&gt;, and &lt;code style=&quot;background:#eee;font-weight:bold;&quot;&gt;std::endl&lt;/code&gt; is supposed to
flush the stream (by definition, it's in the standard, see section 27.6.2.7.)
My current implementation doesn't flush the streams, so what should I do? 
&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;A)&lt;/strong&gt; Not care.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;B)&lt;/strong&gt; Reimplement as &lt;code style=&quot;background:#eee;font-weight:bold;&quot;&gt;Log::endl&lt;/code&gt; (which will ultimately call &lt;code style=&quot;background:#eee;font-weight:bold;&quot;&gt;std::endl&lt;/code&gt; and thus end up flushing the streams!)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;C)&lt;/strong&gt; Create a special case specifically for &lt;code style=&quot;background:#eee;font-weight:bold;&quot;&gt;std::endl&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;
I don't like the first option, not my way of doing things by preference (alas,
it has been known to happen.)  The second option is easy to implement but will
feel unnatural to the user (a moot point, I'm the sole user!)  The third option
smells &quot;wrong,&quot; but I already decided to do it didn't I, and I went one step
worse and broke the guarantee for &lt;code style=&quot;background:#eee;font-weight:bold;&quot;&gt;std::endl&lt;/code&gt; (albeit probably mostly
unknown.)
&lt;/p&gt;

&lt;p&gt;
Is &lt;strong&gt;C&lt;/strong&gt; really enough though?  &lt;em&gt;No&lt;/em&gt;, it turns out.  Aside from breaking the
&lt;em&gt;flush&lt;/em&gt; guarantee it also doesn't go quite far enough.  Think about &lt;code style=&quot;background:#eee;font-weight:bold;&quot;&gt;std::hex&lt;/code&gt;
and similar modifiers.  By converting them to a string (an empty string) before
writing to the underlying &lt;code style=&quot;background:#eee;font-weight:bold;&quot;&gt;std::ostream&lt;/code&gt;s their meaning is lost.  My original
implementation above is, in essence, an abomination.
&lt;/p&gt;

&lt;p&gt;
In the end I settled on &lt;strong&gt;E&lt;/strong&gt;: templates.  What I should have done in the first
place, but avoided since I was hung up on pimpl at the time (templates and
pimpl don't mix well.)
&lt;/p&gt;

&lt;pre class=&quot;code&quot;&gt;
template &amp;lt;typename T&amp;gt;
void Log::output(T const &amp;amp; t) {
    ... /prep/
        BOOST_FOREACH(std::ostream &amp;amp; os, m_osList) {
            os &amp;lt;&amp;lt; t;
        }
    ...
}
&lt;/pre&gt;
  </description>
</item>
<item>
  <title>Bitten by the python</title>
  <link>http://yvan.seth.id.au/Entries/Technology/Code/Bitten_by_the_python.html</link>
  <guid isPermaLink="true">http://yvan.seth.id.au/Entries/Technology/Code/Bitten_by_the_python.html</guid>
  <category>/Entries/Technology/Code</category>
  <pubDate>Mon, 16 Jun 2008 17:10:00 GMT</pubDate>
  <description>


&lt;p&gt;
I play with the snake from time to time, Python having now almost entirely
supplanted Perl as my hak-n-slash language.  But I'm certainly still learning
as I go.  Today I've been trying to work out what's gone wrong with something
I'm doing.  I thought I'd done something strange with inheritance, not
understanding some case where inheritance causes data to become static (in the
sense of static members in C++.)  What was really the problem is that I didn't
realise that default parameters to members are kept and reused, and if you go
and &lt;em&gt;use&lt;/em&gt; a parameter you can end up with state being held on to where you
don't (well, I didn't) expect it.
&lt;/p&gt;

&lt;p&gt;
I've hit this at least once before I think, it is vaguely familiar.  Anyway,
it's a bit of a tricky gotcha as far as I'm concerned.  Intuitive?  I wouldn't
say so.  Here's the example:
&lt;/p&gt;

&lt;pre class=&quot;code&quot;&gt;
#!/usr/bin/python

class Parent:
    def __init__(self, stuff = []):
        self.stuff = stuff
    def doIt(self):
        print &quot; &quot;.join(self.stuff)
    def addStuff(self, stuff):
        self.stuff.append(stuff)

class Child(Parent):
    def __init__(self, stuff = []):
        Parent.__init__(self)
    def doIt(self):
        self.addStuff(&quot;foo&quot;)
        Parent.doIt(self)

c1 = Child()
c1.doIt()
c2 = Child()
c2.doIt()
c3 = Child()
c3.doIt()
&lt;/pre&gt;

&lt;p&gt;
Execute this and we see:
&lt;/p&gt;

&lt;pre class=&quot;console&quot;&gt;
foo
foo foo
foo foo foo
&lt;/pre&gt;

&lt;p&gt;
What?  Why is it accumulating &quot;foo&quot;s?  The answer is in the
&quot;&lt;code style=&quot;background:#eee;font-weight:bold;&quot;&gt;stuff = &amp;#x5b;&amp;#x5d;&lt;/code&gt;&quot; argument to &lt;code style=&quot;background:#eee;font-weight:bold;&quot;&gt;__init__&lt;/code&gt;.  What is going on, as far as I
can garner from the documentation, is that that default argument (a list) is
being instantiated once and then kept for future use.  What's more, I'm
assigning &lt;code style=&quot;background:#eee;font-weight:bold;&quot;&gt;self.stuff&lt;/code&gt; to this, which is kept as a &lt;em&gt;reference&lt;/em&gt; and doesn't
create a copy.  So I have ended up with a static value for &lt;code style=&quot;background:#eee;font-weight:bold;&quot;&gt;self.stuff&lt;/code&gt;, well,
within the functionality of this code &amp;ndash; it isn't entirely congruous to a
&lt;em&gt;static&lt;/em&gt; member.
&lt;/p&gt;

&lt;p&gt;
How to fix it?  I don't know the definitive approach, but here's a couple of
ways.  To achieve complete deep copying of the argument whether it be default
or passed in use &lt;code style=&quot;background:#eee;font-weight:bold;&quot;&gt;copy.deepcopy&lt;/code&gt;:
&lt;/p&gt;

&lt;pre class=&quot;code&quot;&gt;
#!/usr/bin/python
import copy

class Parent:
    def __init__(self, stuff = []):
        self.stuff = copy.deepcopy(stuff)
...
&lt;/pre&gt;

&lt;p&gt;
Alternatively, you could use &lt;code style=&quot;background:#eee;font-weight:bold;&quot;&gt;copy.copy&lt;/code&gt; for a shallow copy and I guess that
might be analogous to this:
&lt;/p&gt;

&lt;pre class=&quot;code&quot;&gt;
#!/usr/bin/python

class Parent:
    def __init__(self, stuff = []):
        self.stuff = []
        self.stuff.extend(stuff)
...
&lt;/pre&gt;

&lt;p&gt;
I'm sure there are great uses for this behaviour in Python.  What are they?
Something more fundamental than &quot;neat tricks&quot; involving incremental/changing
default state?  Am I the only one to think this somewhat of a gotcha?
&lt;/p&gt;

&lt;p&gt;
Now that I've worked out what was wrong I can retrospectively build the right
Google magic to get
&lt;a href=&quot;http://docs.python.org/ref/function.html&quot; title=&quot;7.6 Function definitions&quot;&gt;straight to the answer&lt;/a&gt;.
&lt;/p&gt;

&lt;p&gt;
No time to read up on more casual chatter about it though.  The documented
formula is something like:
&lt;/p&gt;

&lt;pre class=&quot;code&quot;&gt;
#!/usr/bin/python

class Parent:
    def __init__(self, stuff = None):
        if stuff is None:
            stuff = []
        self.stuff = stuff
...
&lt;/pre&gt;

  </description>
</item>
<item>
  <title>Don't store references to boost::shared_ptr</title>
  <link>http://yvan.seth.id.au/Entries/Technology/Code/Don_t_store_references_to_boost__shared_ptr.html</link>
  <guid isPermaLink="true">http://yvan.seth.id.au/Entries/Technology/Code/Don_t_store_references_to_boost__shared_ptr.html</guid>
  <category>/Entries/Technology/Code</category>
  <pubDate>Wed, 09 Apr 2008 22:57:00 GMT</pubDate>
  <description>


&lt;p&gt;
I &lt;em&gt;should&lt;/em&gt; have known better, in fact I'm pretty sure I &lt;em&gt;did&lt;/em&gt; know better, but it
is so easy to fall into the trap of using reference members as often as
possible.  Actually, in other code I've not done this, so I'll call it a
&quot;typo&quot;, yeah. :)
&lt;/p&gt;

&lt;p&gt;
No time to go into details with examples and all that, best to keep things
simple anyway.
&lt;/p&gt;

&lt;p&gt;
Avoid storing auto/smart/shared pointer &lt;em&gt;references&lt;/em&gt; in the name of avoiding
premature optimisation.  You always have to think: will this be destroyed
before &lt;em&gt;this&lt;/em&gt; is destroyed.  If it takes more than 10 seconds to work that out
then steer clear of the reference path!  &lt;em&gt;Hoping&lt;/em&gt; it'll all be OK is asking for
trouble.  Especially if you don't own the calling code!
&lt;/p&gt;

&lt;p&gt;
A shared pointer instance stores little state, so just copy the damn thing.
This way the target instance will be destroyed when nothing needs it anymore,
which is the whole point.  Better a few copies than having things vanish
earlier than you expected because you tried to game the system.  
&lt;/p&gt;

&lt;p&gt;
My new personal rule is: always &lt;em&gt;copy&lt;/em&gt; shared/auto/smart pointers.
&lt;/p&gt;

&lt;p&gt;
If the copy is a problem it'll show up in profiling later and that's when you
work out how to fix it.
&lt;/p&gt;

&lt;p&gt;
(Alas there isn't an easy way around avoiding circular references.)
&lt;/p&gt;
  </description>
</item>
</channel>
</rss>
