free allocated resources on error

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