]> sigrok.org Git - libsigrok.git/commitdiff
output/text: Fix memory leak of internal state buffers.
authorpoljar (Damir Jelić) <redacted>
Tue, 19 Nov 2013 11:03:20 +0000 (12:03 +0100)
committerBert Vermeulen <redacted>
Wed, 20 Nov 2013 21:03:02 +0000 (22:03 +0100)
The text output module keeps buffers for internal state, upon receiving a DF_END
packet it frees the internal context but the buffers are never freed.

This adds a text_cleanup() helper function and registers it as the cleanup
function within all the text output modules.

output/text/ascii.c
output/text/bits.c
output/text/hex.c
output/text/text.c
output/text/text.h

index bd87024d19a108136ce18072f05cd44bf9f9b786..147ff60e94586abb5df1dae3619898aa11295442 100644 (file)
@@ -133,4 +133,5 @@ SR_PRIV struct sr_output_format output_text_ascii = {
        .init = init_ascii,
        .data = data_ascii,
        .event = event,
+       .cleanup = text_cleanup,
 };
index 3afb90169104eaa20037b709378871fc4a054347..e0a9812c94aed21e904dae0b779a3e45d2c8ec2a 100644 (file)
@@ -116,4 +116,5 @@ SR_PRIV struct sr_output_format output_text_bits = {
        .init = init_bits,
        .data = data_bits,
        .event = event,
+       .cleanup = text_cleanup,
 };
index 3ddaf47fc6c20eee48f533290ece3a8289aeb7f8..1c2757cc0e84e3845b4014b4f0aecfa71f452813 100644 (file)
@@ -109,4 +109,5 @@ SR_PRIV struct sr_output_format output_text_hex = {
        .init = init_hex,
        .data = data_hex,
        .event = event,
+       .cleanup = text_cleanup,
 };
index 9be3397400f41c56c384c9bc8874d23337946ab0..7263224d1874f65c8df2bac2b3e0066f49dc2571 100644 (file)
@@ -171,6 +171,31 @@ err:
        return ret;
 }
 
+SR_PRIV int text_cleanup(struct sr_output *o)
+{
+       struct context *ctx;
+
+       if (!o)
+               return SR_ERR_ARG;
+
+       ctx = o->internal;
+
+       g_free(ctx->header);
+       g_free(ctx->linebuf);
+       g_free(ctx->linevalues);
+
+       if (ctx->prevsample)
+               g_free(ctx->prevsample);
+
+       g_slist_free(ctx->probenames);
+
+       g_free(ctx);
+
+       o->internal = NULL;
+
+       return SR_OK;
+}
+
 SR_PRIV int event(struct sr_output *o, int event_type, uint8_t **data_out,
                  uint64_t *length_out)
 {
@@ -195,8 +220,6 @@ SR_PRIV int event(struct sr_output *o, int event_type, uint8_t **data_out,
                flush_linebufs(ctx, outbuf);
                *data_out = outbuf;
                *length_out = strlen((const char *)outbuf);
-               g_free(o->internal);
-               o->internal = NULL;
                break;
        default:
                *data_out = NULL;
index 8d5c98760924a004c9031ad09abf9e32f4ae8816..8e96318d5481356c92667ec093157c513b94ca6f 100644 (file)
@@ -48,6 +48,7 @@ struct context {
 
 SR_PRIV void flush_linebufs(struct context *ctx, uint8_t *outbuf);
 SR_PRIV int init(struct sr_output *o, int default_spl, enum outputmode mode);
+SR_PRIV int text_cleanup(struct sr_output *o);
 SR_PRIV int event(struct sr_output *o, int event_type, uint8_t **data_out,
                  uint64_t *length_out);