fix memory leak and convert istream to string