fix memory leak
add a script for MacOS and Linux