fix memory leak and modify return value of insert to pair