AU: Fix memory leaks in Subprocess.
BUG=chromium-os:15421
TEST=unit tests, tested an AU
Change-Id: Id93223f434a491f4d4b1ca5a02d4f7fbdd411e02
Reviewed-on: http://gerrit.chromium.org/gerrit/1042
Tested-by: Darin Petkov <petkov@chromium.org>
Reviewed-by: Chris Masone <cmasone@chromium.org>
diff --git a/subprocess.cc b/subprocess.cc
index 7b84e84..34f5994 100644
--- a/subprocess.cc
+++ b/subprocess.cc
@@ -1,4 +1,4 @@
-// Copyright (c) 2009 The Chromium OS Authors. All rights reserved.
+// Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
@@ -121,7 +121,6 @@
ExecCallback callback,
void* p) {
GPid child_pid;
- GError* err;
scoped_array<char*> argv(new char*[cmd.size() + 1]);
for (unsigned int i = 0; i < cmd.size(); i++) {
argv[i] = strdup(cmd[i].c_str());
@@ -154,7 +153,7 @@
NULL,
&stdout_fd,
NULL,
- &err);
+ NULL);
FreeArgv(argv.get());
if (!success) {
LOG(ERROR) << "g_spawn_async failed";
@@ -204,7 +203,6 @@
ScopedFreeArgPointer argp_free(argp);
char* child_stdout;
-
bool success = g_spawn_sync(
NULL, // working directory
argv.get(),
@@ -218,10 +216,16 @@
return_code,
&err);
FreeArgv(argv.get());
- if (err)
+ if (err) {
LOG(INFO) << "err is: " << err->code << ", " << err->message;
- if (child_stdout && strlen(child_stdout))
- LOG(INFO) << "Subprocess output:\n" << child_stdout;
+ g_error_free(err);
+ }
+ if (child_stdout) {
+ if (strlen(child_stdout)) {
+ LOG(INFO) << "Subprocess output:\n" << child_stdout;
+ }
+ g_free(child_stdout);
+ }
return success;
}