Torkel Bjørnson-Langen

Torkel Bjørnson-Langen torbjo

Niner since 2013


  • My Favorite C++ 10-Liner

    @Jon Harrop:

    @Michi Henning: What you describe as a "reaping pass" is actually a garbage collector. So you are suggesting that Herb fix the memory leak in his favorite C++ code by adding a garbage collector to it. Let me be the first person to agree with you! Better yet, stop using C++ and use something that bundles a GC...

    Why this pathological focus on garbage collection (of memory)? It misses the broader point.

    Memory management is just a special case of the more general problem – resource management! Your classic garbage collector does *not* solve that problem.

    Python (a garbage collected language) have the same issues:

    >>> fp = open (filename, 'rb')
    >>> msg.attach (MIMEImage( # might throw!
    >>> fp.close()

    (WTF! Why doesn't the code-block preserve my linebreaks!?!)

    If an exception is thrown you have no guarantees on when the file handle will be closed/released. And you might start leaking resources.

    Modern C++ have come a long way at solving the resource management problem. Listen to Bjarne Stroustrup excelent talk. He explains it way better than I am able to. Sean Parent's talk "Inheritance Is The Base Class of Evil" is also highly recommended!

    C++ is a complex and hard to learn language; so it's not for everyone. And there are lots of god reasons not to use C++ at a particular project. But general statements like "stop using C++ and use something that bundles a GC", is just plain wrong! (IMHO)

    And you can of course just plug a garbage collector into your C++ program. Like the open source vector drawing program Inkscape does.