fix memory leak & sigpipe