Page 1 of 1

Polling infinite loop

PostPosted: 30 Jul 2008, 04:55
by Robert Pope
Since I've moved to Vista, I've started getting messages that my program has stopped responding. Digging in, it happens at the end of the game, after I get "result" and "quit". It goes into an infinite loop reading and parsing "quit" over and over again.

I think the key is that the first time it reads "quit" (and every poll before that), it is returning BiosKey()=TRUE via the "#C" path below, but then whenever I check BiosKey() after that, it continues to return TRUE, but via the "#B" path.

Is the
Code: Select all
      if (!PeekNamedPipe(inh, NULL, 0, NULL, &dw, NULL)) {
        fprintf(logStream,"#B");
        return 1; }
correct? Why isn't it return 0; ? This is the standard BiosKey() function that I think a lot of people are using.

Is this related to it being the end of game, and maybe the pipe being closed?

Code: Select all
//ReadInput and Bioskey were copied from a snippet posted on the WinBoard forum
int XBoard=1;   // Set to 1 for winboard, 0 for testing in console mode

int Bioskey(void) {
  static int init = 0, pipe;
  static HANDLE inh;
  DWORD dw;
  if (XBoard) {
    if (!init) {
      init = 1;
      inh = GetStdHandle(STD_INPUT_HANDLE);
      pipe = !GetConsoleMode(inh, &dw);
      if (!pipe) {
        SetConsoleMode(inh, dw & ~(ENABLE_MOUSE_INPUT | ENABLE_WINDOW_INPUT));
        FlushConsoleInputBuffer(inh);
      }
    }
    if (stdin->_cnt > 0) {
      fprintf(logStream,"#A");
      return stdin->_cnt;
      }
    if (pipe) {
      if (!PeekNamedPipe(inh, NULL, 0, NULL, &dw, NULL)) {
        fprintf(logStream,"#B");
//        return 1;
        return 0; // will this fix timing error?
        }
        fprintf(logStream,"#C");
        return dw;
    }
    else {
      GetNumberOfConsoleInputEvents(inh, &dw);
      fprintf(logStream,"#D");
      return dw <= 1 ? 0 : dw;
    }
  }
  else {
    fprintf(logStream,"#E");
    return _kbhit();
    }
}

Re: Polling infinite loop

PostPosted: 30 Jul 2008, 13:17
by Robert Pope
Well, by switching the "return 1" to "return 0" like I did in the quoted code, I haven't hung in 100 games (compared to 1 game in 5), so I think that fixed it. If you use BiosKey(), you may want to check if this could affect you too.