Segmentation Fault

Started by
6 comments, last by MaulingMonkey 15 years, 4 months ago
When I run the program containing this code, it crashes immediately as it calls CmdHandler. Ive spent a reasonable amount of time trying to figure out whats wrong... but I honestly dont know. Any ideas?

 for(int i = 0; i < MAX_NUM_PLAYERS; i++)
 {
   if((n = read(Paveria->Players.Sock, Paveria->Players.LastCmd, MAX_INPUT_LENGTH)) > 0)
   {
     write(Paveria->Players.Sock, "Reading...\n", 11);
     CmdHandler(Paveria->Players);
   }
 }

void CmdHandler(Player Ch)
{
  switch(Ch.State)
  {
    /* Waiting for user ID */
    case STATE_NAME:
      break;
    default:
      write(Ch.Sock, "Error! Invalid State!\n", 22);
  }
};
[Edited by - nethackpro on December 22, 2008 10:53:37 PM]
Advertisement
Look at this in your debugger.

Is Paveria pointing to the thing you expect? What is the value of i when it crashes? Is your array at least i+1 in length? Is Paveria->Players a valid Player* ? Is the Ch valid, along with its State and Sock values?
Paveria points to what I'd expect, the array is of length MAX_NUM_PLAYERS, and the loop bails out as soon as i >= MAX_NUM_PLAYERS. Paveria->Players is a valid(and the only) player, everything is valid, it compiles without any errors or warnings... Im just totally stumped..
Your code in CmdHandler has changed. Is this your real source code? Please always use copy and paste to avoid transcription errors.

Your new code is copying the Player class. Does this have a copy constructor, and does Player contain any pointers itself? I could see this crashing on the second run through if Player's destructor deleted something, but it wouldn't be immediately on the line that calls CmdHandler -- a crash which seems to be impossible both in your new and old code. In the new code, in Player's copy constructor, I could understand, but...

...you are finding out where it crashes from your debugger's stack trace, and not by trying to deduce things by what you change, right?
Could be an issue with copy construction, when your passing the obj Player as a param. Does Player allocate any memory in it's constructor?

The code only seems to be checking Player.State, which I guess is an enum or something so I'm not sure how it'd seg fault, but it's the only thing I can think of.
For some reason I can't run it through my debugger. When I run the program, Ive been testing it via telnet. When I run it through gdb, it doesnt let me connect.

Maybe it'll help if i post this
class Player: public Creature{public:  int State;  int Sock;  char LastCmd[MAX_INPUT_LENGTH];  Player (int, int, string);};class World{public:  string Name;  Player Players[MAX_NUM_PLAYERS];};
I found out the problem. Noob mistake all the way. Ima blame it on being tired... :P Anyways, what I was supposed to be doing was
CmdHandler(&Paveria->Players);
Quote:Original post by nethackpro
For some reason I can't run it through my debugger. When I run the program, Ive been testing it via telnet. When I run it through gdb, it doesnt let me connect.

This is a very bad problem, which I would try to fix straight away.

However...

Quote:Original post by nethackpro
I found out the problem. Noob mistake all the way. Ima blame it on being tired... :P Anyways, what I was supposed to be doing was
CmdHandler(&Paveria->Players);


If that change fixes things, it sounds like your code wasn't even compiling in the first place. Which means it shouldn't even be possible to run, and thus connect to. You may want to try running it in gdb again, just to check that this was indeed the problem?

This topic is closed to new replies.

Advertisement