Fix bugs that causes double free.