Skip to content

Commit

Permalink
Decided recursive deletion of subwindows stood an (admittedly small) …
Browse files Browse the repository at this point in the history
…risk of breaking existing code. delwin() now returns ERR if the window has subwindows, or if it wasn't allocated by PDCursesMod or has already been freed. See discussion at https://lists.gnu.org/archive/html/bug-ncurses/2022-08/msg00006.html .
  • Loading branch information
Bill-Gray committed Aug 11, 2022
1 parent 3b14813 commit c643c0d
Showing 1 changed file with 15 additions and 19 deletions.
34 changes: 15 additions & 19 deletions pdcurses/window.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,8 @@ window
ncols to COLS - begx. Create a new full-screen window by calling
newwin(0, 0, 0, 0).
delwin() deletes the named window, freeing all associated memory. In
the case of overlapping windows, subwindows should be deleted before
the main window.
delwin() deletes the named window, freeing all associated memory.
Subwindows must be deleted before the main window can be deleted.
mvwin() moves the window so that the upper left-hand corner is at
position (y,x). If the move would cause the window to be off the
Expand Down Expand Up @@ -260,22 +259,25 @@ WINDOW *newwin(int nlines, int ncols, int begy, int begx)
int delwin(WINDOW *win)
{
PDC_LOG(("delwin() - called\n"));
int i = 0;
int i;

assert( win);
if (!win)
return ERR;

assert( SP->opaque->n_windows);
/* recursively delete subwindows, if any */
while( i < SP->opaque->n_windows)
/* make sure win has no subwindows */
for( i = 0; i < SP->opaque->n_windows; i++)
if( SP->opaque->window_list[i]->_parent == win)
{
delwin( SP->opaque->window_list[i]);
i = 0; /* start from beginning of list again */
}
else
i++;
return( ERR);

i = 0; /* make sure win is in the window list */
while( i < SP->opaque->n_windows && SP->opaque->window_list[i] != win)
i++;
assert( i < SP->opaque->n_windows);
if( i == SP->opaque->n_windows)
return( ERR);
SP->opaque->n_windows--; /* remove win from window list */
SP->opaque->window_list[i] = SP->opaque->window_list[SP->opaque->n_windows];

/* subwindows use parents' lines */

Expand All @@ -288,12 +290,6 @@ int delwin(WINDOW *win)
if( win->_y)
free(win->_y);
free(win);
i = 0;
while( i < SP->opaque->n_windows && SP->opaque->window_list[i] != win)
i++;
assert( i < SP->opaque->n_windows);
SP->opaque->n_windows--;
SP->opaque->window_list[i] = SP->opaque->window_list[SP->opaque->n_windows];
return OK;
}

Expand Down

0 comments on commit c643c0d

Please sign in to comment.