Fix memory leak in function calls