]> sigrok.org Git - libsigrokdecode.git/blobdiff - type_decoder.c
type_decoder: Fixup memory leak in Decoder.put() (meta, python)
[libsigrokdecode.git] / type_decoder.c
index 880dbade0a7697d054308001b1e3a66b988d829c..aa92c3b22490d9e68813661bbd80c7e7575e89e2 100644 (file)
@@ -40,6 +40,15 @@ static const char *output_type_name(unsigned int idx)
        return names[MIN(idx, G_N_ELEMENTS(names) - 1)];
 }
 
+static void release_annotation(struct srd_proto_data_annotation *pda)
+{
+       if (!pda)
+               return;
+       if (pda->ann_text)
+               g_strfreev(pda->ann_text);
+       g_free(pda);
+}
+
 static int convert_annotation(struct srd_decoder_inst *di, PyObject *obj,
                struct srd_proto_data *pdata)
 {
@@ -112,6 +121,15 @@ err:
        return SRD_ERR_PYTHON;
 }
 
+static void release_binary(struct srd_proto_data_binary *pdb)
+{
+       if (!pdb)
+               return;
+       if (pdb->data)
+               g_free((void *)pdb->data);
+       g_free(pdb);
+}
+
 static int convert_binary(struct srd_decoder_inst *di, PyObject *obj,
                struct srd_proto_data *pdata)
 {
@@ -229,6 +247,13 @@ err:
        return SRD_ERR_PYTHON;
 }
 
+static void release_meta(GVariant *gvar)
+{
+       if (!gvar)
+               return;
+       g_variant_unref(gvar);
+}
+
 static PyObject *Decoder_put(PyObject *self, PyObject *args)
 {
        GSList *l;
@@ -241,6 +266,8 @@ static PyObject *Decoder_put(PyObject *self, PyObject *args)
        struct srd_pd_callback *cb;
        PyGILState_STATE gstate;
 
+       py_data = NULL;
+
        gstate = PyGILState_Ensure();
 
        if (!(di = srd_inst_find_by_obj(NULL, self))) {
@@ -287,6 +314,7 @@ static PyObject *Decoder_put(PyObject *self, PyObject *args)
                        Py_BEGIN_ALLOW_THREADS
                        cb->cb(&pdata, cb->cb_data);
                        Py_END_ALLOW_THREADS
+                       release_annotation(pdata.data);
                }
                break;
        case SRD_OUTPUT_PYTHON:
@@ -319,6 +347,7 @@ static PyObject *Decoder_put(PyObject *self, PyObject *args)
                        Py_BEGIN_ALLOW_THREADS
                        cb->cb(&pdata, cb->cb_data);
                        Py_END_ALLOW_THREADS
+                       release_binary(pdata.data);
                }
                break;
        case SRD_OUTPUT_META:
@@ -331,6 +360,7 @@ static PyObject *Decoder_put(PyObject *self, PyObject *args)
                        Py_BEGIN_ALLOW_THREADS
                        cb->cb(&pdata, cb->cb_data);
                        Py_END_ALLOW_THREADS
+                       release_meta(pdata.data);
                }
                break;
        default:
@@ -339,11 +369,17 @@ static PyObject *Decoder_put(PyObject *self, PyObject *args)
                break;
        }
 
+       if (py_data)
+               Py_DECREF(py_data);
+
        PyGILState_Release(gstate);
 
        Py_RETURN_NONE;
 
 err:
+       if (py_data)
+               Py_DECREF(py_data);
+
        PyGILState_Release(gstate);
 
        return NULL;