From b343bc88035e7bc3dcbc0deb92b518c624e4bf0d Mon Sep 17 00:00:00 2001 From: Holger Vogt Date: Sat, 11 Jan 2020 11:25:14 +0100 Subject: [PATCH] plug memory leaks for the 'plot' command --- src/frontend/misccoms.c | 5 +++++ src/frontend/plotting/plotit.c | 34 +++++++++++++++++++++++++++------- 2 files changed, 32 insertions(+), 7 deletions(-) diff --git a/src/frontend/misccoms.c b/src/frontend/misccoms.c index 6d306f964..40f35ede9 100644 --- a/src/frontend/misccoms.c +++ b/src/frontend/misccoms.c @@ -38,6 +38,7 @@ extern void rem_controls(void); extern IFsimulator SIMinfo; extern void spice_destroy_devices(void); /* FIXME need a better place */ +extern void pl_rempar(void); /* plotit.c */ static void byemesg(void); static int confirm_quit(void); @@ -56,6 +57,7 @@ com_quit(wordlist *wl) gr_clean(); cp_ccon(FALSE); + /* Make sure the guy really wants to quit. */ if (!ft_nutmeg) if (!noask && !confirm_quit()) @@ -90,6 +92,9 @@ com_quit(wordlist *wl) cp_destroy_keywords(); destroy_ivars(); #else + /* remove plotting parameters */ + pl_rempar(); + while (ft_curckt) com_remcirc(NULL); #endif diff --git a/src/frontend/plotting/plotit.c b/src/frontend/plotting/plotit.c index ba3c1039d..95fb45207 100644 --- a/src/frontend/plotting/plotit.c +++ b/src/frontend/plotting/plotit.c @@ -18,11 +18,28 @@ #include "graf.h" static bool sameflag; - +/* All these things are static so that "samep" will work. + They are outside of plotit() to allow deleting */ +static double *xcompress = NULL, *xindices = NULL; +static double *xlim = NULL, *ylim = NULL; +static double *xdelta = NULL, *ydelta = NULL; +static char *xlabel = NULL, *ylabel = NULL, *title = NULL; #ifdef TCL_MODULE #include "ngspice/tclspice.h" #endif +/* remove the malloced parameters upon ngspice quit */ +void pl_rempar(void) +{ + txfree(xcompress); + txfree(xindices); + txfree(xlim); + txfree(ylim); + txfree(xdelta); + txfree(ydelta); + txfree(xlabel); + txfree(ylabel); +} static struct dvec *vec_self(struct dvec *v); static struct dvec *vec_scale(struct dvec *v); @@ -258,11 +275,6 @@ bool plotit(wordlist *wl, const char *hcopy, const char *devname) return FALSE; } - /* All these things are static so that "samep" will work. */ - static double *xcompress = NULL, *xindices = NULL; - static double *xlim = NULL, *ylim = NULL; - static double *xdelta = NULL, *ydelta = NULL; - static char *xlabel = NULL, *ylabel = NULL, *title = NULL; static bool nointerp = FALSE; static GRIDTYPE gtype = GRID_LIN; static PLOTTYPE ptype = PLOT_LIN; @@ -317,8 +329,10 @@ bool plotit(wordlist *wl, const char *hcopy, const char *devname) /* Build the plot command. This construction had been done with wordlists * and reversing, and flattening, but it is clearer as well as much more * efficient to use a dstring. */ - rc_ds |= ds_cat_printf(&ds_cline, "plot %s", wl_flatten(wwl->wl_next)); + char *flatstr = wl_flatten(wwl->wl_next); + rc_ds |= ds_cat_printf(&ds_cline, "plot %s", flatstr); wl_free(wwl); + tfree(flatstr); /* Add title, xlabel or ylabel, if available, with quotes ''. */ if (nxlabel) { @@ -343,6 +357,7 @@ bool plotit(wordlist *wl, const char *hcopy, const char *devname) sameflag = getflag(wl, "samep"); if (!sameflag || !xlim) { + txfree(xlim); xlim = getlims(wl, "xl", 2); if (!xlim) { xlim = getlims(wl, "xlimit", 2); @@ -354,6 +369,7 @@ bool plotit(wordlist *wl, const char *hcopy, const char *devname) } if (!sameflag || !ylim) { + txfree(ylim); ylim = getlims(wl, "yl", 2); if (!ylim) { ylim = getlims(wl, "ylimit", 2); @@ -365,6 +381,7 @@ bool plotit(wordlist *wl, const char *hcopy, const char *devname) } if (!sameflag || !xcompress) { + txfree(xcompress); xcompress = getlims(wl, "xcompress", 1); if (!xcompress) { xcompress = getlims(wl, "xcomp", 1); @@ -376,6 +393,7 @@ bool plotit(wordlist *wl, const char *hcopy, const char *devname) } if (!sameflag || !xindices) { + txfree(xindices); xindices = getlims(wl, "xindices", 2); if (!xindices) { xindices = getlims(wl, "xind", 2); @@ -387,6 +405,7 @@ bool plotit(wordlist *wl, const char *hcopy, const char *devname) } if (!sameflag || !xdelta) { + txfree(xdelta); xdelta = getlims(wl, "xdelta", 1); if (!xdelta) { xdelta = getlims(wl, "xdel", 1); @@ -397,6 +416,7 @@ bool plotit(wordlist *wl, const char *hcopy, const char *devname) } if (!sameflag || !ydelta) { + txfree(ydelta); ydelta = getlims(wl, "ydelta", 1); if (!ydelta) { ydelta = getlims(wl, "ydel", 1);