-2

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.

Babur Makhmudov
  • 307
  • 1
  • 8
  • 2
    Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a [mcve]. – Swordfish Feb 24 '19 at 13:33

2 Answers2

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

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

0

The problem was not related to ncurses library, rather a silly error while allocating new panel that caused undefined behavior, as pointed out Michal Marszalek. Problem solved

Babur Makhmudov
  • 307
  • 1
  • 8