From f9b54a6ca92f1d1532237db0439fc772789f3718 Mon Sep 17 00:00:00 2001 From: Uwe Hermann Date: Fri, 28 Jan 2011 00:56:46 +0100 Subject: [PATCH] Properly free all memory upon sigrokdecode_shutdown(). --- decode.c | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 51 insertions(+), 2 deletions(-) diff --git a/decode.c b/decode.c index 97a13c4..21a6e01 100644 --- a/decode.c +++ b/decode.c @@ -159,7 +159,7 @@ static int h_str(PyObject *py_res, PyObject *py_func, PyObject *py_mod, goto err_h_decref_str; } - if (!(*outstr = strdup(str))) { + if (!(*outstr = g_strdup(str))) { ret = SIGROKDECODE_ERR_MALLOC; goto err_h_decref_str; } @@ -245,7 +245,11 @@ static int sigrokdecode_load_decoder(const char *name, d->py_func = py_func; - /* TODO: Handle inputformats, outputformats. */ + /* TODO: Handle func, inputformats, outputformats. */ + /* Note: They must at least be set to NULL, will segfault otherwise. */ + d->func = NULL; + d->inputformats = NULL; + d->outputformats = NULL; *dec = d; @@ -342,6 +346,46 @@ err_run_decref_func: return ret; } +/** + * TODO + */ +static int sigrokdecode_unload_decoder(struct sigrokdecode_decoder *dec) +{ + g_free(dec->id); + g_free(dec->name); + g_free(dec->desc); + g_free(dec->func); + + /* TODO: Free everything in inputformats and outputformats. */ + + if (dec->inputformats != NULL) + g_slist_free(dec->inputformats); + if (dec->outputformats != NULL) + g_slist_free(dec->outputformats); + + Py_XDECREF(dec->py_func); + Py_XDECREF(dec->py_mod); + + return SIGROKDECODE_OK; +} + +/** + * TODO + */ +static int sigrokdecode_unload_all_decoders(void) +{ + GSList *l; + struct sigrokdecode_decoder *dec; + + for (l = sigrokdecode_list_decoders(); l; l = l->next) { + dec = l->data; + /* TODO: Error handling. */ + sigrokdecode_unload_decoder(dec); + } + + return SIGROKDECODE_OK; +} + /** * Shutdown libsigrokdecode. * @@ -349,6 +393,11 @@ err_run_decref_func: */ int sigrokdecode_shutdown(void) { + /* Unload/free all decoders, and then the list of decoders itself. */ + /* TODO: Error handling. */ + sigrokdecode_unload_all_decoders(); + g_slist_free(list_pds); + /* Py_Finalize() returns void, any finalization errors are ignored. */ Py_Finalize(); -- 2.30.2