-2

I am trying to make a simple console program using curses on ubuntu, but each time I try to call getch and make it wait for input, when I input the input, it throws a segmentation fault. I did initialize the library using the initscr() function in another part of my code.

Here is the part of code throwing the segmentation fault :

int ch;
ch = getch();

Any help is greatly appreciated,

Mindoo

EDIT: After switching out the int for a char, I get the following warnings that revolve arround this piece of code right after the previously mentioned getch().

char ch;
  ch = getch();

  //Get xObject position
  int xObjectX = getXObject()->getPosX();
  int xObjectY = getXObject()->getPosY();

  switch (ch) {
    case KEY_LEFT:
      getXObject()->move(xObjectY, xObjectX--);
      break;

    case KEY_RIGHT:
      getXObject()->move(xObjectY, xObjectX++);
      break;

    case KEY_UP:
      getXObject()->move(xObjectY++, xObjectX);
      break;

    case KEY_DOWN:
      getXObject()->move(xObjectY--, xObjectX);
      break;

    case KEY_BACKSPACE:
      game->stop();
      break;

I get these warnings :

TestGameState.cpp: In member function ‘virtual void TestGameState::handleInput()’:
TestGameState.cpp:21:5: warning: case label value exceeds maximum value for type
     case KEY_LEFT:
     ^
TestGameState.cpp:25:5: warning: case label value exceeds maximum value for type
     case KEY_RIGHT:
     ^
TestGameState.cpp:29:5: warning: case label value exceeds maximum value for type
     case KEY_UP:
     ^
TestGameState.cpp:33:5: warning: case label value exceeds maximum value for type
     case KEY_DOWN:
     ^
TestGameState.cpp:37:5: warning: case label value exceeds maximum value for type
     case KEY_BACKSPACE:
mindoo
  • 1
  • 6

1 Answers1

0

That function is unlikely to be the cause. How did you tell that line cause SF?

You may try: 1. use getchar instead 2. try not to use curses

Marcus Sun
  • 21
  • 2
  • I need curse to do the displaying. I realized that was the line causing the segfault by putting a printw("example") line on each end of that piece of code and it only displayed once. – mindoo Mar 14 '17 at 20:48
  • Try to isolate problems. Like remove anything, just this like to see if crash still happen. Then add piece by piece. Most likely, something instead of this caused the crash. – Marcus Sun Mar 14 '17 at 20:52
  • What do you mean, thats just what I did – mindoo Mar 14 '17 at 20:53
  • Tryo to use a debugger, printing message is not a reliable way to find out the exact fault point. – Marcus Sun Mar 14 '17 at 20:55