]> sigrok.org Git - libsigrok.git/commitdiff
sr_dev_clear(): Always free sdi->priv (devc).
authorUwe Hermann <redacted>
Tue, 11 Jul 2017 20:59:52 +0000 (22:59 +0200)
committerUwe Hermann <redacted>
Thu, 13 Jul 2017 09:59:11 +0000 (11:59 +0200)
Until now, clear_helper() callbacks for std_dev_clear_with_callback()
were expected to g_free(devc), but not all of them did that.

Have std_dev_clear_with_callback() unconditionally g_free(sdi->priv)
(i.e., devc), regardless of whether a clear_helper() callback was
provided or not. It was doing g_free(sdi->priv) when no callback
was provided already anyway.

This makes the individual drivers' clear_helper() implementations
shorter and prevents errors such as missing g_free(devc) calls.

This works, because all drivers either call std_dev_clear_with_callback()
directly, or indirectly via std_dev_clear().

This also allows us to remove some no-longer needed dev_clear()
and clear_helper() implementations that only did g_free(devc)
in favor of std_dev_clear().

14 files changed:
src/hardware/demo/api.c
src/hardware/dreamsourcelab-dslogic/api.c
src/hardware/ftdi-la/api.c
src/hardware/fx2lafw/api.c
src/hardware/hameg-hmo/api.c
src/hardware/hantek-6xxx/api.c
src/hardware/ikalogic-scanalogic2/api.c
src/hardware/ikalogic-scanaplus/api.c
src/hardware/lecroy-xstream/api.c
src/hardware/rigol-ds/api.c
src/hardware/scpi-pps/api.c
src/hardware/sysclk-lwla/api.c
src/hardware/yokogawa-dlm/api.c
src/std.c

index 55b17376d90a4a05c7c4809e3da2dc011f03732e..6549b5d784c5db8fe069dbd5a3223cca57e99d25 100644 (file)
@@ -193,7 +193,6 @@ static void clear_helper(void *priv)
        while (g_hash_table_iter_next(&iter, NULL, &value))
                g_free(value);
        g_hash_table_unref(devc->ch_ag);
-       g_free(devc);
 }
 
 static int dev_clear(const struct sr_dev_driver *di)
index 2b84ac3e0b03bb783fb6f9795761998c8eb98036..7e099f49a82b8fb121fc9559877e2278ea495248 100644 (file)
@@ -283,19 +283,6 @@ static GSList *scan(struct sr_dev_driver *di, GSList *options)
        return std_scan_complete(di, devices);
 }
 
-static void clear_helper(void *priv)
-{
-       struct dev_context *devc;
-
-       devc = priv;
-       g_free(devc);
-}
-
-static int dev_clear(const struct sr_dev_driver *di)
-{
-       return std_dev_clear_with_callback(di, clear_helper);
-}
-
 static int dev_open(struct sr_dev_inst *sdi)
 {
        struct sr_dev_driver *di = sdi->driver;
@@ -637,7 +624,7 @@ static struct sr_dev_driver dreamsourcelab_dslogic_driver_info = {
        .cleanup = std_cleanup,
        .scan = scan,
        .dev_list = std_dev_list,
-       .dev_clear = dev_clear,
+       .dev_clear = std_dev_clear,
        .config_get = config_get,
        .config_set = config_set,
        .config_list = config_list,
index 8e4f5edae04167b92f4a83e2ceab493c55b99daa..6b545afb08be17cdb82d351198269077ad7d0758 100644 (file)
@@ -246,7 +246,6 @@ static void clear_helper(void *priv)
 
        devc = priv;
        g_free(devc->data_buf);
-       g_free(devc);
 }
 
 static int dev_clear(const struct sr_dev_driver *di)
index da27f09295b396083503e7945c3f297c23fa8eed..d824b02223bb69a7fd2a5173761cd98d449779e9 100644 (file)
@@ -351,7 +351,6 @@ static void clear_helper(void *priv)
 
        devc = priv;
        g_slist_free(devc->enabled_analog_channels);
-       g_free(devc);
 }
 
 static int dev_clear(const struct sr_dev_driver *di)
index afdeb456ed6b6c5a9054d413e6f707bdd643ab26..7e062b8d6f258feb05989dc9571ec67e5298a665 100644 (file)
@@ -120,8 +120,6 @@ static void clear_helper(void *priv)
 
        g_free(devc->analog_groups);
        g_free(devc->digital_groups);
-
-       g_free(devc);
 }
 
 static int dev_clear(const struct sr_dev_driver *di)
index ad407a2e29825983a70550f00b03615d0a04f187..aa0977ad3cbbc457bf56ff20325bf5a80c7bfb0e 100644 (file)
@@ -162,7 +162,6 @@ static void clear_helper(void *priv)
 
        devc = priv;
        g_slist_free(devc->enabled_channels);
-       g_free(devc);
 }
 
 static int dev_clear(const struct sr_dev_driver *di)
index 335768eba0f63fb7fc4e24138ad07a679481eb19..2078b8015c4e8a61d1115af31b6418a5ce63c78a 100644 (file)
@@ -160,7 +160,6 @@ static void clear_helper(void *priv)
 
        libusb_free_transfer(devc->xfer_in);
        libusb_free_transfer(devc->xfer_out);
-       g_free(devc);
 }
 
 static int dev_clear(const struct sr_dev_driver *di)
index 8ea351824460700abbe2861dfcf88ff3ec924c14..dd5d09ffef51342acb8a26a7af226387c2859310 100644 (file)
@@ -52,7 +52,6 @@ static void clear_helper(void *priv)
        ftdi_free(devc->ftdic);
        g_free(devc->compressed_buf);
        g_free(devc->sample_buf);
-       g_free(devc);
 }
 
 static int dev_clear(const struct sr_dev_driver *di)
index cf9df601e36932d233296fe513dddc9bd4a12aee..e00154b9bef88bc87d1d9066a4a5b31fa6daf65b 100644 (file)
@@ -124,8 +124,6 @@ static void clear_helper(void *priv)
        lecroy_xstream_state_free(devc->model_state);
 
        g_free(devc->analog_groups);
-
-       g_free(devc);
 }
 
 static int dev_clear(const struct sr_dev_driver *di)
index 59e29fb9e2a36624234683ca9b0fab89dad14caf..221b1234ac6177c30bccfc92b56cba6c00f60ca8 100644 (file)
@@ -288,7 +288,6 @@ static void clear_helper(void *priv)
        g_free(devc->trigger_source);
        g_free(devc->trigger_slope);
        g_free(devc->analog_groups);
-       g_free(devc);
 }
 
 static int dev_clear(const struct sr_dev_driver *di)
index f4514d8e0e225efb313e1b4410ad24c057a4baef..3642b31e9acb039dacac3d3f7c7566336a61b6e9 100644 (file)
@@ -295,7 +295,6 @@ static void clear_helper(void *priv)
        devc = priv;
        g_free(devc->channels);
        g_free(devc->channel_groups);
-       g_free(devc);
 }
 
 static int dev_clear(const struct sr_dev_driver *di)
index 6cd38a13a05a41891401ff60d13dc3636585d0fa..ea98a234eb388f1a3456ac0f629b8a8d5ff52de1 100644 (file)
@@ -203,25 +203,6 @@ static GSList *scan(struct sr_dev_driver *di, GSList *options)
        return std_scan_complete(di, devices);
 }
 
-static void clear_helper(void *priv)
-{
-       struct dev_context *devc;
-
-       devc = priv;
-
-       if (devc->acquisition) {
-               sr_err("Cannot clear device context during acquisition!");
-               return; /* Leak and pray. */
-       }
-
-       g_free(devc);
-}
-
-static int dev_clear(const struct sr_dev_driver *di)
-{
-       return std_dev_clear_with_callback(di, clear_helper);
-}
-
 /* Drain any pending data from the USB transfer buffers on the device.
  * This may be necessary e.g. after a crash or generally to clean up after
  * an abnormal condition.
@@ -750,7 +731,7 @@ static struct sr_dev_driver sysclk_lwla_driver_info = {
        .cleanup = std_cleanup,
        .scan = scan,
        .dev_list = std_dev_list,
-       .dev_clear = dev_clear,
+       .dev_clear = std_dev_clear,
        .config_get = config_get,
        .config_set = config_set,
        .config_channel_set = config_channel_set,
index 95a345b9a6718c3410c58c76e7bdcf5434a77f2c..cace9e586db695dbd7f68fa34471a315c2fd1e4e 100644 (file)
@@ -131,7 +131,6 @@ static void clear_helper(void *priv)
 
        g_free(devc->analog_groups);
        g_free(devc->digital_groups);
-       g_free(devc);
 }
 
 static int dev_clear(const struct sr_dev_driver *di)
index 1433f30fb923dcf8b765a33c1d4539255bcfc7e5..a4f5d8835e25b0026e29693f6bf7e9eca58300d5 100644 (file)
--- a/src/std.c
+++ b/src/std.c
@@ -333,16 +333,18 @@ SR_PRIV int std_serial_dev_acquisition_stop(struct sr_dev_inst *sdi)
  * This function can be used to implement the dev_clear() driver API
  * callback. dev_close() is called before every sr_dev_inst is cleared.
  *
- * The only limitation is driver-specific device contexts (sdi->priv).
+ * The only limitation is driver-specific device contexts (sdi->priv / devc).
  * These are freed, but any dynamic allocation within structs stored
  * there cannot be freed.
  *
  * @param[in] driver The driver which will have its instances released.
  *                   Must not be NULL.
  * @param[in] clear_private If not NULL, this points to a function called
- *            with sdi->priv as argument. The function can then clear
+ *            with sdi->priv (devc) as argument. The function can then clear
  *            any device instance-specific resources kept there.
- *            It must also clear the struct pointed to by sdi->priv.
+ *            It must NOT clear the struct pointed to by sdi->priv (devc),
+ *            since this function will always free it after clear_private()
+ *            has run.
  *
  * @retval SR_OK Success.
  * @retval SR_ERR_ARG Invalid argument.
@@ -388,12 +390,13 @@ SR_PRIV int std_dev_clear_with_callback(const struct sr_dev_driver *driver,
                        if (sdi->inst_type == SR_INST_MODBUS)
                                sr_modbus_free(sdi->conn);
                }
+
+               /* Clear driver-specific stuff, if any. */
                if (clear_private)
-                       /* The helper function is responsible for freeing
-                        * its own sdi->priv! */
                        clear_private(sdi->priv);
-               else
-                       g_free(sdi->priv);
+
+               /* Clear sdi->priv (devc). */
+               g_free(sdi->priv);
 
                sr_dev_inst_free(sdi);
        }