Register forum user name Search FAQ

Gammon Forum

Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the password reset link.

Due to spam on this forum, all posts now need moderator approval.

 Entire forum ➜ Programming ➜ General ➜ An annoying error that i can't lose

An annoying error that i can't lose

It is now over 60 days since the last post. This thread is closed.     Refresh page


Posted by Trom   (82 posts)  Bio
Date Fri 23 Jan 2004 09:10 PM (UTC)
Message
i added a new class structure to my char_data. The error occurs when i try and read the class information from the pfile, when it tries to give the value to the class variables, it crashes. In gdb it gives this error.

Program received signal SIGSEGV, Segmentation fault.
0x004eec5c in fread_class (classx=0x0, fp=0x10021494) at save.c:2068
2068 KEY ( "Level", tempclass->level, fread_number ( fp ) );

in the pfile its "Level 1" and it should read correctly. I've also tried fscanf and another thing like the following;

tempclass->level = fread_number(fp);

They all crash the mud. Sometimes it shows a blank line from gdb. I can't seem to get past this, the creation of characters works, the saving also works, just not the loading of the classes.

Classes in my char_data look like this.

CLASS_DATA *classes;

Its a link list, each node on the list has variables 'level' and 'id', as long as 'next' of coarse.

The information trying to be read in is this:

#CLASS
Level 1
ID 0
End

It seems to get to the point were it read in level and just freezes.

Any help would be appreciated greatly since i probably won't be coding again until this is solved.
Top

Posted by Trom   (82 posts)  Bio
Date Reply #1 on Fri 23 Jan 2004 09:12 PM (UTC)
Message
the 'id' variable is basically ch->class from the old way of doing classes.
Top

Posted by David Haley   USA  (3,881 posts)  Bio
Date Reply #2 on Fri 23 Jan 2004 09:18 PM (UTC)
Message
Make sure that "tempclass" is defined, i.e. there is memory allocated for it when you call that key.

David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone

http://david.the-haleys.org
Top

Posted by Trom   (82 posts)  Bio
Date Reply #3 on Fri 23 Jan 2004 09:34 PM (UTC)

Amended on Fri 23 Jan 2004 09:46 PM (UTC) by Trom

Message
I've tried that just now, this is the problem function (when KEY is used).

void fread_class ( CLASS_DATA * classx, FILE * fp )
{
char *word;
bool fMatch = FALSE;
CLASS_DATA *tempclass;
tempclass = alloc_mem ( sizeof ( *tempclass ) );
tempclass = classx;

while (tempclass != NULL)
tempclass = tempclass->next;

for ( ;; )
{
word = feof ( fp ) ? "End" : fread_word ( fp );

switch ( UPPER ( word[0] ) )
{
case '*':
fread_to_eol ( fp );
break;

case 'L':
KEY ( "Level", tempclass->level, fread_number ( fp ) );
break;

case 'I':
KEY ( "ID", tempclass->id, fread_number ( fp ) );
break;

case 'E':
if ( !str_cmp ( word, "End" ) )
return;

break;

}
}

}

Cygwin gives an error from gdb mode saying that the line with 'Level' being read in (the first thing to be read in) is causing a segmentation error.


The goal of the function is to reach the end of the link list of classes so it finds an empty spot, then it assigns the class being read. It uses this function multiple times, but can't get by the first time.
Top

Posted by David Haley   USA  (3,881 posts)  Bio
Date Reply #4 on Fri 23 Jan 2004 10:06 PM (UTC)
Message
Your code has a few issues... The first error is that you allocate memory for tempclass, and then just as soon change what tempclass points to! Bzzt...! memory leak.

Second problem is that your loop's terminating condition is when tempclass is NULL. So, when you loop ends, tempclass == NULL.

So, when you use your KEY function, it is trying to dereference a null pointer... bzzzt! Program crash. :)

What you actually want to do is find the last node in your list, and then set its next to new memory that you will allocate.

David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone

http://david.the-haleys.org
Top

Posted by Trom   (82 posts)  Bio
Date Reply #5 on Fri 23 Jan 2004 10:15 PM (UTC)
Message
it worked, thanks a lot :)

This was my first attempt at this kind of thing (loading using memory allocations and such).
Top

Posted by Trom   (82 posts)  Bio
Date Reply #6 on Fri 30 Jan 2004 01:42 AM (UTC)
Message
I'm having problems with something similar. This is the function with the problem inside it.


CH_CMD ( do_score )
{
char buf[MSL];
CLASS_DATA *class = NULL;

class = ch->classes;
send_to_char ( "{D+-----------------------------------------------------------------------------{x\n\r", ch );
while (class != NULL) {
sprintf ( buf, "{D| {cClasses{x:{g %s{x", class_table[class->id].name );
send_to_char( buf, ch );
class = class->next;
}
send_to_char ( "\n\r", ch );

}

when i run it in gdb, its crashes pointing to cygwin1.dll

I've tried allocating memory to it before assiging it the classes, but it crashes the same way. Please help if you can, thanks in advance for any help.
Top

Posted by Nick Gammon   Australia  (23,140 posts)  Bio   Forum Administrator
Date Reply #7 on Fri 30 Jan 2004 02:51 AM (UTC)
Message
This is printing the classes. Where do you set them up?

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Trom   (82 posts)  Bio
Date Reply #8 on Fri 30 Jan 2004 04:55 PM (UTC)

Amended on Fri 30 Jan 2004 04:57 PM (UTC) by Trom

Message
typedef struct class_data CLASS_DATA;

struct class_data
{
CLASS_DATA *next;
int level;
int id;
}

struct char_data
{
// the normal declarations
CLASS_DATA *classes;
}

When the character logs in, it loads all the classes from the pfile (class_data is a link list of classes). Each class contains its level and the class number so it can reference the class from the table in const.c

When the character logs out or saves, classes are saved into the pfile "#CLASS" just like how "#OBJECT" works. That is not causing the prob though (saving and loading seems to work).

The problem i'm guessing is when 'class = ch->classes'. It doesn't like assigning the values from structure to structure... I have posted the code which is causing the prob.

Top

Posted by Nick Gammon   Australia  (23,140 posts)  Bio   Forum Administrator
Date Reply #9 on Fri 30 Jan 2004 07:52 PM (UTC)
Message
Assigning a pointer like that is perfectly normal. I can't see much wrong with your code, assuming the data is OK which I can't tell by looking at the code.

I suggest you look at my recent post on gdb, compile with debugging information (see that page for how to do that) and then set a breakpoint on the problem function and step through it, checking variables like "class" and "class->id" as you go.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


23,425 views.

It is now over 60 days since the last post. This thread is closed.     Refresh page

Go to topic:           Search the forum


[Go to top] top

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.