From bea29a448ce20da81676d8ecc42bff9de638ce02 Mon Sep 17 00:00:00 2001 From: Bert Vermeulen Date: Tue, 5 Mar 2013 21:48:34 +0100 Subject: [PATCH] fix SR_DF_END flow Output modules were getting SR_DF_END after their object was already free()d, and the output channel closed. --- sigrok-cli.c | 80 +++++++++++++++++++++++++++------------------------- 1 file changed, 41 insertions(+), 39 deletions(-) diff --git a/sigrok-cli.c b/sigrok-cli.c index bc29317..24fef94 100644 --- a/sigrok-cli.c +++ b/sigrok-cli.c @@ -640,45 +640,6 @@ static void datafeed_in(const struct sr_dev_inst *sdi, } break; - case SR_DF_END: - g_debug("cli: Received SR_DF_END"); - if (!o) { - g_debug("cli: double end!"); - break; - } - if (o->format->event) { - o->format->event(o, SR_DF_END, &output_buf, &output_len); - if (output_buf) { - if (outfile) - fwrite(output_buf, 1, output_len, outfile); - g_free(output_buf); - output_len = 0; - } - } - if (limit_samples && received_samples < limit_samples) - g_warning("Device only sent %" PRIu64 " samples.", - received_samples); - if (opt_continuous) - g_warning("Device stopped after %" PRIu64 " samples.", - received_samples); - - if (outfile && outfile != stdout) - fclose(outfile); - - if (opt_output_file && default_output_format) { - if (sr_session_save(opt_output_file, sdi, savebuf->data, - unitsize, savebuf->len / unitsize) != SR_OK) - g_critical("Failed to save session."); - g_byte_array_free(savebuf, FALSE); - } - - g_array_free(logic_probelist, TRUE); - if (o->format->cleanup) - o->format->cleanup(o); - g_free(o); - o = NULL; - break; - case SR_DF_META: g_debug("cli: received SR_DF_META"); meta = packet->payload; @@ -819,6 +780,47 @@ static void datafeed_in(const struct sr_dev_inst *sdi, } } + /* SR_DF_END needs to be handled after the output module's recv() + * is called, so it can properly clean up that module etc. */ + if (packet->type == SR_DF_END) { + g_debug("cli: Received SR_DF_END"); + + if (o->format->event) { + o->format->event(o, SR_DF_END, &output_buf, &output_len); + if (output_buf) { + if (outfile) + fwrite(output_buf, 1, output_len, outfile); + g_free(output_buf); + output_len = 0; + } + } + + if (limit_samples && received_samples < limit_samples) + g_warning("Device only sent %" PRIu64 " samples.", + received_samples); + + if (opt_continuous) + g_warning("Device stopped after %" PRIu64 " samples.", + received_samples); + + g_array_free(logic_probelist, TRUE); + + if (o->format->cleanup) + o->format->cleanup(o); + g_free(o); + o = NULL; + + if (outfile && outfile != stdout) + fclose(outfile); + + if (opt_output_file && default_output_format) { + if (sr_session_save(opt_output_file, sdi, savebuf->data, + unitsize, savebuf->len / unitsize) != SR_OK) + g_critical("Failed to save session."); + g_byte_array_free(savebuf, FALSE); + } + } + } /* Register the given PDs for this session. -- 2.30.2