Fix a small memory leak when creating a new child process
This commit is contained in:
parent
7f0674ac27
commit
e3728942dc
1 changed files with 10 additions and 2 deletions
|
|
@ -16,6 +16,7 @@
|
|||
static inline char**
|
||||
serialize_string_tuple(PyObject *src) {
|
||||
Py_ssize_t sz = PyTuple_GET_SIZE(src);
|
||||
|
||||
char **ans = calloc(sz + 1, sizeof(char*));
|
||||
if (!ans) fatal("Out of memory");
|
||||
for (Py_ssize_t i = 0; i < sz; i++) {
|
||||
|
|
@ -28,6 +29,13 @@ serialize_string_tuple(PyObject *src) {
|
|||
return ans;
|
||||
}
|
||||
|
||||
static inline void
|
||||
free_string_tuple(char** data) {
|
||||
size_t i = 0;
|
||||
while(data[i++]) free(data[i-1]);
|
||||
free(data);
|
||||
}
|
||||
|
||||
extern char **environ;
|
||||
|
||||
static inline void
|
||||
|
|
@ -125,8 +133,8 @@ spawn(PyObject *self UNUSED, PyObject *args) {
|
|||
break;
|
||||
}
|
||||
#undef exit_on_err
|
||||
free(argv);
|
||||
free(env);
|
||||
free_string_tuple(argv);
|
||||
free_string_tuple(env);
|
||||
if (PyErr_Occurred()) return NULL;
|
||||
return PyLong_FromLong(pid);
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue