Re: OLC Memory errors

From: Daniel Koepke (dkoepke@CALIFORNIA.COM)
Date: 03/08/98


On Sun, 8 Mar 1998, Dan Argent wrote:

->>From what I can see here, it seems to be some problem with the mud
->trying to work out who is doing stuff. - I tried a complete PWipe and
->that didn't solve it.

<blink> I have no idea what made you think that would solve it...

->181     if (d->olc && OLC_NUM(d) == number) {
->====================================================================
->  * Check that whatever it is isn't already being edited.
->   */
->  for (d = descriptor_list; d; d = d->next)
->    if (d->connected == olc_scmd_info[subcmd].con_type)
->      if ((number != NOTHING) && d->olc && OLC_NUM(d) == number) {
->        if (subcmd == SCMD_OLC_AEDIT)
->          sprintf(buf, "Actions are already being editted by %s.\r\n",
->                  (CAN_SEE(ch, d->character) ? GET_NAME(d->character)
->                  : "someone"));
->          return;
->      }

This is the end of the for loop, all code beyond is not part of it,

->if (d->olc && OLC_NUM(d) == number) {

which means that this is referring to 'd' after the for loop has
exited (the for loop exits when d == NULL, which means you are looking
for part of a structure when the structure equals nothing.  That is
something like looking for a piece of a non-existing pie.  The fix is
trivial: put braces around the code in the for loop.

->(gdb) info local
->ch = (struct char_data *) 0x81acc28
->number = 2500
->save = 1
->real_num = 17
->d = (struct descriptor_data *) 0x0

This is what gave away that it was a NULL pointer, 0x0 is NULL.  Right
away I knew that somehow d = NULL when you referred to it.  So I
looked at lines preceeding that, straightened out the code a little
(it was very poorly spaced and written in a style contrasting
CircleMUD's--you should either reformat all of Circle or write your
code in Circle's style), and saw that it's just a simple case of
either (a) a stupid mistake, (b) not understanding how braces (or, as
the case may be, lack of braces) affects code.

Brief synopsis of braces in for loops:

  char *string[maximum];
  .
  .
  .
  for (i = 0; i < maximum; i++)
    printf("%d\r\n", i);
  printf("%s\r\n", string[i]);

Will crash because you have an array of strings (char pointers,
actually) that is size 'maximum'.  Without braces around the code,
that second printf in the code is called after the for loop is
finished, which means i == maximum.  And as anyone on the list can
tell you (<cough>), C starts numbering from 0, so in "int array[32];"
array[31] refers to the 32nd entry (and array[32] would refer to the
non-existent 33rd entry).  The above for loop should actually be,

  for (i = 0; i < maximum; i++) {
    printf("%d\r\n", i);
    printf("%s\r\n", string[i]);
  }


-dak : Circle questions anyone...?


     +------------------------------------------------------------+
     | Ensure that you have read the CircleMUD Mailing List FAQ:  |
     | http://democracy.queensu.ca/~fletcher/Circle/list-faq.html |
     +------------------------------------------------------------+



This archive was generated by hypermail 2b30 : 12/15/00 PST