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.
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.
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.
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.
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.
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.