segmentation fault, tried GDB, failed miserably.

Posted by Will Sayin on Mon 27 Jun 2005 08:23 AM — 3 posts, 14,715 views.

USA #0
OK, right now I am just messing around with SMAUG-FUSS in CYGWIN, and I decided to make a new skill, with new code and everything. Now, just so you know, I am new to coding, I have dabbled for a while, but I have never delved into something so intricate as SMAUG.

I added the following code:

(the code is supposed to stop combat(like flee) then cause the user to go into hide, but will only work if the victim is affected by blindness)


void do_retreat( CHAR_DATA * ch, char *argument )
{
   int percent;
   CHAR_DATA *victim;
   if( ch->fighting )
   {
        percent = number_percent(  ) + ( IS_AFFECTED( victim, AFF_BLIND ) ? 10 : -50 )
      - ( get_curr_lck( ch ) - 15 ) + ( get_curr_lck( victim ) - 13 );
        if( !IS_AFFECTED( victim, AFF_BLIND ) || !can_use_skill( ch, percent, gsn_retreat ))
        {
            send_to_char( "You try to jump out of combat, but are unable to get out in time!\n\r", ch );
            act(AT_ACTION, "$n tries to jump out of combat, but you catch $m!", ch, NULL, victim, TO_VICT );
            act(AT_ACTION, "$n tries to jump out of combat, but $N catches $m!", ch, NULL, victim, TO_NOTVICT );
            WAIT_STATE( ch, 15 );
            return;
        }
        
        stop_fighting( ch, TRUE );
        xSET_BIT( ch->affected_by, AFF_HIDE );
        send_to_char( "You jump out of combat and seek refuge in the shadows.\n\r", ch);
        WAIT_STATE( ch, 10);
   }
        
            
}           

to skills.c

this to tables.c:(line 2098)


if( skill == do_retran )
      return "do_retran";
   if( skill == do_retreat )  <------these two
      return "do_retreat";    <------   lines
   if( skill == do_return )   
      return "do_return";


as well as this to tables.c:(line 966)

if( !str_cmp( name, "do_retran" ) )
            return do_retran;
         if( !str_cmp( name, "do_retreat") ) <--- this line
            return do_retreat;               <--- and this
         if( !str_cmp( name, "do_return" ) )
            return do_return;


in mud.h, line 3905:

DECLARE_DO_FUN( do_retran );
DECLARE_DO_FUN( do_retreat ); <----- this line
DECLARE_DO_FUN( do_return );




when I fight a mob, gouge them, and type retreat, a segmentation fault occurs. I was able to get this much with GDB:


Program received signal SIGSEGV, Segmentation fault.
0x004c3eea in get_Trust (ch=0x440f708) at handler.c:136
136        if( ch->trust !=0 )
(gdb) list
131 short get_trust( CHAR_DATA * ch )
132{
133   if( ch->desc && ch->desc->original )
134      ch = ch->desc->original;
135
136   if( ch->trust != 0 )
137      return ch->trust;
138
139   if( IS_NPC( ch ) && ch->level >= LEVEL_AVATAR )
140      return LEVEL_AVATAR;
141
142   if( ch->level >= LEVEL_NEOPHYTE && IS_RETIRED( ch ) )
143      return LEVEL_NEOPHYTE;
144
145   return ch->level;
146   }
(gdb) print ch->trust
Cannot access memory at address 0x440f7c0
(gdb)


Thanks to Nick Gammon's GDB tutorial, I have figured out that this has something to do with ch->trust(I hope!)... just what, I have no clue.

Could anyone help give me some idea of what is going on, and how I could fix it?

Thanks a lot,
Will Sayin
telnet://darkstone.betterbox.net:5432
USA #1
Hi there. :)

You also need to look at the backtrace from GDB, that is, a path of function calls that will tell you how you got there.

One problem, probably the culprit, is that you declare 'victim' but you never set it to anything. You probably meant to set it to whomever ch is fighting.

The error you're getting looks like ch is bad somehow in get_trust. My hunch is that one of the functions you're calling with uninitialized 'victim' as a parameter is dying. A backtrace will help you out there; and also you can type 'up' and 'down' to navigate the backtrace. That way you know exactly what line of code was being run and where.
USA #2
Thanks Ksilyan,

I looked at do_gouge and copy/pasted:

victim = who_fighting( ch );


and it all worked fine from there.

Thank You,
Will Sayin
telnet://darkstone.betterbox.net:5432