add memory leak check & bug fix