Exception Safety and Garbage Collection and Some Other Stuff

5 min read Original article ↗
  • 1.

    Exception Safety andGarbage Collection and Some Other StuffA Moderately Directed Rant

  • 2.

    Why should Icare about this?It’s very likely that you’ve been writing totally incorrect code without realizing itOnce you do realize it, it’s usually not too hard to fix the problem, depending on the languageThis is information that isn’t all that widely known, for whatever reasonYou can use it to show off at interviews!You can use it to start arguments about which programming language is the best!

  • 3.

    Boring DefinitionsWe needto define a couple basic things at the beginning so everybody’s on the same pageIt’ll be quick, I promiseGarbage collection is a method of managing dynamic (heap-allocated) memoryIt’s non-deterministic, and there is usually no guarantee that memory is cleaned up at allMost modern languages use garbage collection

  • 4.

    More Boring DefinitionsAresource is anything you need to return to the system once you’re done using itFile handles, dynamic memory, locks, etc.Exception safety means that you can throw an exception in the middle of your function without bad things happeningThere’s a complicated formal definition with degrees of exception safety but this is good enough for our purposes

  • 5.

    Reasonable Code Example(C)Let’s look at some C code so we can figure out what this talk is even aboutvoidexample() {lock(&g_mutex);int* my_int_pointer = (int*)malloc(sizeof(int));do_something_with(my_int_pointer);free(my_int_pointer);unlock(&g_mutex);}This is fairly reasonable, safe C code. It executes deterministically and everyone is happy

  • 6.

    Terrible Code Example(C++)Let’s see that exact same code, but now we’ll pretend that it was compiled as C++voidexample() {lock(&g_mutex);int* my_int_pointer = (int*)malloc(sizeof(int));do_something_with(my_int_pointer);free(my_int_pointer);unlock(&g_mutex);}Catastrophe! This is going to compile and run without warnings, but be completely and totally unsafe!Why? Exceptions!

  • 7.

    That Sucks!Yes, itdoes suck! It’s such a problem that people were motivated to go try to solve itBjarneStroustrup (C++ language creator) came up with a solution which he named Resource Acquisition Is Initialization (RAII)Incidentally, in addition to providing exception safety, RAII made C++ way easier to useLet’s look at a correct C++ version of our code example, using RAII

  • 8.

    Modern Code Example(C++)voidexample() {Lockmy_lock(&g_mutex);auto_ptr<int> my_int_pointer(new int());do_something_with(my_int_pointer);}Thanks to RAII this example is exception safe, and we don’t have to worry about cleanup.auto_ptr<T> is part of the C++ standard library, but we’ve just made up LockLet’s look at the code for our made-up Lock class so we can see how RAII actually works

  • 9.

    How RAII ActuallyWorksclassLock {private:mutex* m_pMutex;public:Lock(mutex* pMutex) : m_pMutex(pMutex) { lock(m_pMutex); } ~Lock() {unlock(m_pMutex); }};In C++ a stack-allocated object’s destructor is always called once it goes out of scope, whether due to a function returning, due to normal code execution, or due to stack unwinding caused by a thrown exception

  • 10.

    Sure, but Idon’t use C++That’s understandable. We are (for better or worse) a Java school, so let’s see if we can’t make RAII work in JavaImmediately we run into some problemsJava doesn’t have destructorsJava doesn’t have stack allocation for objectsSo RAII won’t work with Java, then. What else have we got?For dynamic memory we have garbage collection, but that’s a special case of the problem that doesn’t really need (or provide) determinismThe best we can do is the Dispose pattern

  • 11.

    The Dispose Pattern(Java)voidexample() {IntegermyInteger = newInteger(0);Locklock = newLock(g_mutex);try {doSomethingWith(myInteger); } finally {lock.dispose(); }}While rewriting this every time gives you exception safety, it’s really easy to forget itIf you forget to do this, your program will still compile and run with no warnings, despite being wrong. Awesome!This is more verbose than even the C example, yet is the minimum amount of code required for Java

  • 12.

    More Dispose Loveliness(Java)voidexample() {FilemyFile = newFile(filename);try {DBConnectiondbConn = newDBConnection(credentials);try {LockmyLock = newLock(g_mutex);try {doSomething(myFile, dbConn); } finally {myLock.dispose(); } } finally {dbConn.dispose(); } } finally {myFile.dispose(); }}This is again the minimum code required to be correct

  • 13.

    Again, but withRAII (C++)voidexample() {FilemyFile(filename);DBConnectiondbConn(credentials);LockmyLock(&g_mutex);doSomething(myFile, dbConn);}

  • 14.

    One More Time(D)voidexample() {scopemyFile = newFile(filename);scopedbConn= newDBConnection(credentials);scopemyLock = newLock(g_mutex);doSomething(myFile, dbConn);}

  • 15.

    “You can takemy deterministic resource management when my cold dead hand goes out of scope.” -- Anon

  • 16.

    Why does Javasuck so bad?By choice. You can have deterministic resource management alongside garbage collection, but the Java guys specifically chose not toThe D programming language supports RAII and has a garbage collector, so it’s definitely possibleJava, C#, Python, Ruby all screw this up to varying degreesThe latter three have some syntactic sugar for resource management, but the onus is still on you to remember to use itJava 7 catches up with C# and adds the same syntactic sugar, but still doesn’t solve the problemPerl, PHP, C++ and D all get it right to varying degreesIf PHP gets something right before your language does, you should reassess your life goals

  • 17.

    So what shouldI do?My (unpopular) answer? Use C++ and Perl/PHP for everything until the D ecosystem matures a bit, then switch over to D entirelyC++ has its own set of problems, but it’s my opinion that they’re exaggerated and the benefits far outweigh themIf you’re stuck using a broken language like Java, I really don’t know what to tell youI guess you could cry a little bit, but I don’t think it would solve the problemLearn the Dispose pattern, always remember to use it

  • 18.

    Okay, done rantingIwish I had more helpful advice for Java, C#, Python, Ruby users, but this is the unfortunate state we find ourselves inIf you want more information about anything mentioned:The D Programming Language by Andrei Alexandrescu is an excellent D introductionIf you want to learn how to code modern C++, you should read Effective C++ by Scott MeyersThe Boost website has good information about exception safety and reference-counted smart pointers, which I didn’t really talk about (Scott Meyers does in Effective C++)Google knows allQuestions?