Fix uninitialized var and memory leak on error