Jump to content
  • Advertisement
Sign in to follow this  
nethackpro

Segmentation Fault

This topic is 3590 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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]

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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..

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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];
};

Share this post


Link to post
Share on other sites
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);

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!