fix use of memory after deallocated bug