Search code examples
cncursescurses

del_panel function of ncurses library causes segfault


I have a code that allocates panels in one function with new_panel and tries to deallocate them with del_panel in another function. The code sample is below

void medical_cards(int regid){ //work with patient's medical cards
/*...*/
PANEL *pmedcards[cards];
WINDOW *wmedcards[cards];

bind_windows(pmedcards, wmedcards, cards); 
//this function allocates panels
/*...*/
update_panels();
doupdate();

/*...*/

i = 0;
while (i < cards)
   del_panel(pmedcards[i++]); 
  /*here I get segfault with backtrace pointing 
  to wtouchln function of the ncurses library*/
i = 0;
while (i < cards)
    delwin(wmedcards[i++]);
return;
}

void bind_windows(PANEL **pmedcards, WINDOW **wmedcards, int cards){ 
int height = 15, width = 40, ypos = LINES - 20, xpos = COLS - 45;

int i = 0;
while (i < cards) {
    wmedcards[i] = newwin(height, width, ypos, xpos + i);
    box(wmedcards[i++], 0, 0);
}
i = 0;
while (i < cards) 
    pmedcards[i] = new_panel(wmedcards[i++]);
}

The problem is that I get segfault while trying to deallocate panels, debugger points to wtouchln function as a source of trouble. It seems that no one encountered such a problem before, and man pages have scarce description of del_panel function, any help is appreciated. What I expect is that del_panel will do its job of freeing resources and return normally, without causing the program to crash.


Solution

  • pmedcards[i] = new_panel(wmedcards[i++]);
    

    What is first i or i++? This is UB.