From: Daniel J S. <dan...@ie...> - 2004-07-27 16:09:38
|
Hans-Bernhard Broeker wrote: >>In any case, if the change is scrapped, you might still want to fix that >>memory leak in the "show_key" function. >> >> > >I'm lost: what exactly was the leak? > Below is the first part of the code from "show_key()". Notice that the memory is allocated every time the function is called. However, it is only conditionally freed in the case of KEY_AUTO_PLACEMENT, but not in the case of KEY_USER_PLACEMENT. So, as a little test, launch gnuplot and also bring up a system monitor for watching memory usage. In gnuplot type show key and the memory usage momentarily goes up but comes back to where it was. But do a set key 30,30 show key and the memory usage goes up but doesn't come back down. In this patch I've put together, I've simply eliminated the need for dynamically allocating memory. (Just send all the little substrings directly to stderr rather than creating the whole string first.) Another fix would be to make the char * "str" static and test if it is zero or not before allocating memory. Do you want to fix this first in the code independent of what I'm doing? (That is, is this something you want to go back to 4.0 branch and fix, but have the same change in 4.1?... I don't know CVS branching too well.) Or do you want me to include it in the key cleanup patch I'm doing so you can move that into CVS quick? In the latter case, I don't think it is too critical of a problem to remain in 4.0. "show key" in user placement mode shouldn't happen enough to chew up too much memory. Dan /* process 'show key' command */ static void show_key() { legend_key *key = &keyT; char *str = gp_alloc(30, "show_key"); SHOW_ALL_NL; if (!(key->visible)) { fputs("\ \tkey is OFF\n", stderr); return; } switch (key->flag) { case KEY_AUTO_PLACEMENT: if (key->vpos == TUNDER) { strcpy(str, "below"); } else if (key->vpos == TTOP) { strcpy(str, "top"); } else { strcpy(str, "bottom"); } if (key->hpos == TOUT) { strcpy(str, "outside (right)"); } else if (key->hpos == TLEFT) { strcat(str, " left"); } else { strcat(str, " right"); } if (key->vpos != TUNDER && key->hpos != TOUT) { strcat(str, " corner"); } fprintf(stderr, "\ \tkey is ON, position: %s\n", str); free(str); break; case KEY_USER_PLACEMENT: fputs("\tkey is at ", stderr); show_position(&key->user_pos); putc('\n', stderr); break; } |