Fix error path of main thread with freeing allocated buffer