[1.5] Import first patch (fixes.patch) from issue 249 to fix leaks on error condition.