From: Laurent Mazet Date: Tue, 25 Jun 2024 21:25:04 +0000 (+0200) Subject: fix memory leaks X-Git-Tag: v1.0~8 X-Git-Url: https://secure.softndesign.org/git/?a=commitdiff_plain;h=f518eca47b0e75b4b79cee99f69ee0f888cb90cb;p=gameoflife.git fix memory leaks --- diff --git a/function.c b/function.c index 2ce0816..43e61a6 100644 --- a/function.c +++ b/function.c @@ -200,7 +200,7 @@ board_t *loadboard (char *str, char **pname) board = initboard (xsize, ysize); memcpy (board->tab, tab, xsize * ysize); } - if ((board != NULL) && (*pname != NULL)) { + if ((board != NULL) && (pname != NULL)) { *pname = strdup (name); } @@ -356,4 +356,69 @@ board_t *putelement (board_t *board, board_t *element, int x, int y, int mode) return board; } +board_t *processfile (char *filename, char **pname) +{ + char *buffer = readdata (filename); + if (buffer == NULL) { + VERBOSE (ERROR, fprintf (stderr, "can't read file (%s)\n", filename)); + return NULL; + } + + board_t *board = loadboard (buffer, pname); + if (board == NULL) { + VERBOSE (ERROR, fprintf (stderr, "incorrect file (%s)\n", filename)); + } + + free (buffer); + + return board; +} + +library_t *processlibrary (char *libname) +{ + int n = 0; + library_t *lib = (library_t *) malloc ((n + 2) * sizeof (library_t)); + CHECKALLOC (lib); + + (lib + n)->board = initboard (3, 3); + (lib + n)->board->tab[4] = 'X'; + (lib + n)->name = strdup ("Cell"); + memset (lib + n + 1, 0, sizeof (library_t)); + n++; + + if (libname) { + char *saveptr; + char *filename = strtok_r (libname, ":", &saveptr); + while (filename) { + char *name; + lib = (library_t *) realloc (lib, (n + 2) * sizeof (library_t)); + CHECKALLOC (lib); + + (lib + n)->board = processfile (filename, &name); + if ((lib + n)->board != NULL) { + (lib + n)->name = name; + memset (lib + n + 1, 0, sizeof (library_t)); + n++; + } + + filename = strtok_r (NULL, ":", &saveptr); + } + } + + return lib; +} + +void freelibrary (library_t *lib) +{ + int n = 0; + + while ((lib + n)->board) { + freeboard ((lib + n)->board); + free ((lib + n)->name); + n++; + } + + free (lib); +} + /* vim: set ts=4 sw=4 et: */ diff --git a/function.h b/function.h index 5438c17..7179b71 100644 --- a/function.h +++ b/function.h @@ -39,6 +39,12 @@ board_t *mirrorelement (board_t *element, int mode); board_t *putelement (board_t *board, board_t *element, int x, int y, int mode); +board_t *processfile (char *filename, char **pname); + +library_t *processlibrary (char *libname); + +void freelibrary (library_t *lib); + #endif /* __FUNCTION_H__ */ /* vim: set ts=4 sw=4 et: */ diff --git a/gameoflife.c b/gameoflife.c index 7452e6e..bd7b4d8 100644 --- a/gameoflife.c +++ b/gameoflife.c @@ -66,58 +66,6 @@ int usage (int ret) return ret; } -board_t *processfile (char *filename, char **pname) -{ - char *buffer = readdata (filename); - if (buffer == NULL) { - VERBOSE (ERROR, fprintf (stderr, "can't read file (%s)\n", filename)); - return NULL; - } - - board_t *board = loadboard (buffer, pname); - if (board == NULL) { - VERBOSE (ERROR, fprintf (stderr, "incorrect file (%s)\n", filename)); - } - - free (buffer); - - return board; -} - -library_t *processlibrary (char *libname) -{ - int n = 0; - library_t *lib = (library_t *) malloc ((n + 2) * sizeof (library_t)); - CHECKALLOC (lib); - - (lib + n)->board = initboard (3, 3); - (lib + n)->board->tab[4] = 'X'; - (lib + n)->name = strdup ("Cell"); - memset (lib + n + 1, 0, sizeof (library_t)); - n++; - - if (libname) { - char *saveptr; - char *filename = strtok_r (libname, ":", &saveptr); - while (filename) { - char *name; - lib = (library_t *) realloc (lib, (n + 2) * sizeof (library_t)); - CHECKALLOC (lib); - - (lib + n)->board = processfile (filename, &name); - if ((lib + n)->board != NULL) { - (lib + n)->name = name; - memset (lib + n + 1, 0, sizeof (library_t)); - n++; - } - - filename = strtok_r (NULL, ":", &saveptr); - } - } - - return lib; -} - /* main function */ int main (int argc, char *argv[]) { @@ -201,11 +149,6 @@ int main (int argc, char *argv[]) return 1; } - /* load library */ - library_t *lib = processlibrary (listoffilename); - int n = 0; - board_t *element = (lib + n)->board; - /* load playground */ board_t *playground = NULL; if (playname) { @@ -215,6 +158,11 @@ int main (int argc, char *argv[]) } } + /* load library */ + library_t *lib = processlibrary (listoffilename); + int n = 0; + board_t *element = (lib + n)->board; + /* init curses window */ initscr (); noecho (); @@ -373,20 +321,15 @@ int main (int argc, char *argv[]) endwin (); - n = 0; - while ((lib + n)->board) { - freeboard ((lib + n)->board); - free ((lib + n)->name); - } - free (lib); + freelibrary (lib); freeboard (board); return 0; } /* test: gameoflife.exe -e 2>&1 | grep 'no board' */ -/* test: gameoflife.exe -e nofile.gol 2>&1 | grep "can't read file" */ -/* test: gameoflife.exe -e bogus.gol 2>&1 | grep 'incorrect file' */ +/* test: echo -n q | gameoflife.exe -e nofile.gol 2>&1 | grep "can't read file" */ +/* test: echo -n q | gameoflife.exe -e bogus.gol 2>&1 | grep 'incorrect file' */ /* test: gameoflife.exe -h | grep usage */ /* test: gameoflife.exe -p 2>&1 | grep 'no playground' */ /* test: gameoflife.exe -p nofile.gol 2>&1 | grep "can't read file" */ @@ -397,10 +340,10 @@ int main (int argc, char *argv[]) /* test: gameoflife.exe -x 2>&1 | grep 'no width' */ /* test: gameoflife.exe -y 2>&1 | grep 'no height' */ /* test: gameoflife.exe _ 2>&1 | grep invalid */ -/* test: echo -n q | gameoflife.exe -e glider.gol */ -/* test: { echo clllldckkkrcjjjjcxfcilililecs; sleep 5; echo -n q; } | gameoflife.exe -e glider.gol -s 2 */ +/* test: echo -n q | gameoflife.exe */ +/* test: { echo oclllldckkkrcjjjjcxfcilililecs; sleep 5; echo -n q; } | gameoflife.exe -e glider.gol -s 2 */ /* test: { echo icklckcjcjckjs; sleep 2; echo; sleep 1; echo -n q; } | gameoflife.exe -x 10 -y 10 -v 2 */ /* test: { sleep 4; echo -n q; } | gameoflife.exe -p board.gol -s 1 */ -/* test: { echo cllllrckkkkjjd; sleep 3; echo -n q; } | gameoflife.exe -e beehive.gol -s 1 */ +/* test: { echo uucllllrckkkkjjd; sleep 3; echo -n q; } | gameoflife.exe -e beehive.gol:block.gol -s 1 */ /* vim: set ts=4 sw=4 et: */