+/* Properly close and free all devices. */
+static void clear_instances(void)
+{
+ struct sr_dev_inst *sdi;
+ struct context *ctx;
+ GSList *l;
+
+ for (l = hdi->instances; l; l = l->next) {
+ if (!(sdi = l->data)) {
+ /* Log error, but continue cleaning up the rest. */
+ sr_err("hantek-dso: %s: sdi was NULL, continuing", __func__);
+ continue;
+ }
+ if (!(ctx = sdi->priv)) {
+ /* Log error, but continue cleaning up the rest. */
+ sr_err("hantek-dso: %s: sdi->priv was NULL, continuing", __func__);
+ continue;
+ }
+ dso_close(sdi);
+ sr_usb_dev_inst_free(ctx->usb);
+ g_free(ctx->triggersource);
+
+ sr_dev_inst_free(sdi);
+ }
+
+ g_slist_free(hdi->instances);
+ hdi->instances = NULL;
+
+}
+