fix memory leak on thrown exception

Issue found via [cppcheck](http://cppcheck.sourceforge.net/).