fix memory leak in error path
use common exit path.