fix memleak in error handling