fix bug with memory leak. thanks to @hshhhhh