From: poljar (Damir Jelić) Date: Tue, 19 Nov 2013 11:03:20 +0000 (+0100) Subject: output/text: Fix memory leak of internal state buffers. X-Git-Tag: libsigrok-0.3.0~547 X-Git-Url: https://sigrok.org/gitweb/?a=commitdiff_plain;h=8c273ac57ce34d5a8c8b5093413564af69968041;p=libsigrok.git output/text: Fix memory leak of internal state buffers. 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. --- diff --git a/output/text/ascii.c b/output/text/ascii.c index bd87024d..147ff60e 100644 --- a/output/text/ascii.c +++ b/output/text/ascii.c @@ -133,4 +133,5 @@ SR_PRIV struct sr_output_format output_text_ascii = { .init = init_ascii, .data = data_ascii, .event = event, + .cleanup = text_cleanup, }; diff --git a/output/text/bits.c b/output/text/bits.c index 3afb9016..e0a9812c 100644 --- a/output/text/bits.c +++ b/output/text/bits.c @@ -116,4 +116,5 @@ SR_PRIV struct sr_output_format output_text_bits = { .init = init_bits, .data = data_bits, .event = event, + .cleanup = text_cleanup, }; diff --git a/output/text/hex.c b/output/text/hex.c index 3ddaf47f..1c2757cc 100644 --- a/output/text/hex.c +++ b/output/text/hex.c @@ -109,4 +109,5 @@ SR_PRIV struct sr_output_format output_text_hex = { .init = init_hex, .data = data_hex, .event = event, + .cleanup = text_cleanup, }; diff --git a/output/text/text.c b/output/text/text.c index 9be33974..7263224d 100644 --- a/output/text/text.c +++ b/output/text/text.c @@ -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; diff --git a/output/text/text.h b/output/text/text.h index 8d5c9876..8e96318d 100644 --- a/output/text/text.h +++ b/output/text/text.h @@ -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);