]> granicus.if.org Git - nethack/commitdiff
curses: plug most memory leaks
authorPatR <rankin@nethack.org>
Fri, 8 Feb 2019 23:50:59 +0000 (15:50 -0800)
committerPatR <rankin@nethack.org>
Fri, 8 Feb 2019 23:50:59 +0000 (15:50 -0800)
This takes care of a lot of the leaked memory in the curses interface.
It still needs to free memory allocated for status fields when the
status window is destroyed at game end; likewise for message history
when the message window is destroyed.

win/curses/cursdial.c
win/curses/cursmesg.c

index b4c5e66f188357967ac4a31265a0c428bdf2d756..52083ffc1b3332bf0289cf54ca2d7800fddedecc 100644 (file)
@@ -115,6 +115,7 @@ curses_line_input_dialog(const char *prompt, char *answer, int buffer)
                 height++;
             }
         }
+        free(tmpstr);
     }
 
     if (iflags.window_inited) {
@@ -231,9 +232,7 @@ curses_character_input_dialog(const char *prompt, const char *choices,
 
         wrefresh(askwin);
     } else {
-        linestr = curses_copy_of(askstr);
-        pline("%s", linestr);
-        free(linestr);
+        pline("%s", askstr);
         curs_set(1);
     }
 
@@ -305,9 +304,7 @@ curses_character_input_dialog(const char *prompt, const char *choices,
         /* Kludge to make prompt visible after window is dismissed
            when inputting a number */
         if (digit(answer)) {
-            linestr = curses_copy_of(askstr);
-            pline("%s", linestr);
-            free(linestr);
+            pline("%s", askstr);
             curs_set(1);
         }
 
@@ -466,14 +463,16 @@ curses_create_nhmenu(winid wid)
         if (menu_item_ptr != NULL) {
             while (menu_item_ptr->next_item != NULL) {
                 tmp_menu_item = menu_item_ptr->next_item;
+                free((genericptr_t) menu_item_ptr->str);
                 free(menu_item_ptr);
                 menu_item_ptr = tmp_menu_item;
             }
+            free((genericptr_t) menu_item_ptr->str);
             free(menu_item_ptr);        /* Last entry */
             new_menu->entries = NULL;
         }
         if (new_menu->prompt != NULL) { /* Reusing existing menu */
-            free((char *) new_menu->prompt);
+            free((genericptr_t) new_menu->prompt);
         }
         return;
     }
@@ -692,9 +691,11 @@ curses_del_menu(winid wid)
     if (menu_item_ptr != NULL) {
         while (menu_item_ptr->next_item != NULL) {
             tmp_menu_item = menu_item_ptr->next_item;
+            free((genericptr_t) menu_item_ptr->str);
             free(menu_item_ptr);
             menu_item_ptr = tmp_menu_item;
         }
+        free((genericptr_t) menu_item_ptr->str);
         free(menu_item_ptr);    /* Last entry */
         current_menu->entries = NULL;
     }
@@ -711,6 +712,7 @@ curses_del_menu(winid wid)
         tmpmenu->prev_menu = current_menu->prev_menu;
     }
 
+    free((genericptr_t) current_menu->prompt);
     free(current_menu);
 
     curses_del_wid(wid);
@@ -940,8 +942,8 @@ menu_display_page(nhmenu *menu, WINDOW * win, int page_num)
 {
     nhmenu_item *menu_item_ptr;
     int count, curletter, entry_cols, start_col, num_lines, footer_x;
+    char *tmpstr;
     boolean first_accel = TRUE;
-
     int color = NO_COLOR;
     int attr = A_NORMAL;
     boolean menu_color = FALSE;
@@ -964,12 +966,13 @@ menu_display_page(nhmenu *menu, WINDOW * win, int page_num)
 
     werase(win);
 
-    if (strlen(menu->prompt) > 0) {
+    if (menu->prompt && *menu->prompt) {
         num_lines = curses_num_lines(menu->prompt, menu->width);
 
         for (count = 0; count < num_lines; count++) {
-            mvwprintw(win, count + 1, 1, "%s",
-                      curses_break_str(menu->prompt, menu->width, count + 1));
+            tmpstr = curses_break_str(menu->prompt, menu->width, count + 1);
+            mvwprintw(win, count + 1, 1, "%s", tmpstr);
+            free(tmpstr);
         }
     }
 
@@ -1029,9 +1032,9 @@ menu_display_page(nhmenu *menu, WINDOW * win, int page_num)
             start_col += 2;
         }
 #endif
-        if (iflags.use_menu_color && (menu_color = get_menu_coloring
-                                      ((char *) menu_item_ptr->str, &color,
-                                       &attr))) {
+        if (iflags.use_menu_color
+            && (menu_color = get_menu_coloring(menu_item_ptr->str,
+                                               &color, &attr)) != 0) {
             if (color != NO_COLOR) {
                 curses_toggle_color_attr(win, color, NONE, ON);
             }
@@ -1045,11 +1048,12 @@ menu_display_page(nhmenu *menu, WINDOW * win, int page_num)
         num_lines = curses_num_lines(menu_item_ptr->str, entry_cols);
 
         for (count = 0; count < num_lines; count++) {
-            if (strlen(menu_item_ptr->str) > 0) {
-                mvwprintw(win, menu_item_ptr->line_num + count + 1,
-                          start_col, "%s", curses_break_str(menu_item_ptr->str,
-                                                            entry_cols,
-                                                            count + 1));
+            if (menu_item_ptr->str && *menu_item_ptr->str) {
+                tmpstr = curses_break_str(menu_item_ptr->str,
+                                          entry_cols, count + 1);
+                mvwprintw(win, menu_item_ptr->line_num + count + 1, start_col,
+                          "%s", tmpstr);
+                free(tmpstr);
             }
         }
         if (menu_color && (color != NO_COLOR)) {
index f5aa98f094d8869370bd15da9c21131b2b20abb1..8f3368f0ac3f34b1f95624df542d1ba4285928db 100644 (file)
@@ -617,11 +617,11 @@ mesg_add_line(char *mline)
     current_mesg->prev_mesg = last_mesg;
     last_mesg = current_mesg;
 
-
     if (num_messages < max_messages) {
         num_messages++;
     } else {
         tmp_mesg = first_mesg->next_mesg;
+        free(first_mesg->str);
         free(first_mesg);
         first_mesg = tmp_mesg;
     }