I keep getting Seg Faults because my program tries to free the memory after a function call. Here's whats going on: I have class A that looks like this:
1 2 3 4 5
|
class A
{
char name[];
list page;
};
|
For those who don't know what a list is its a sort of a chain linked by pointers. Each member of the chain contains some info and a pointer towards the next element of the chain. Now here is my class B that uses class A:
1 2 3 4 5
|
class B
{
B(): Index = new A*[100]{};
private:
A **Index;
|
That's roughly what it is. Now here's the problem. I start reading a file and keep going with while(!file.eof()). In this loop I'm creating new A's but I'm only initializing their name. I don't have a constructor for the A class so I guess it generates automatically. Now when the program reads the whole file and tries to exit do while{} I get a Seg Fault. After running it with gdb I found that when exiting the while it tries to delete the newly created A's and it crashes when deleting the list part of A.
Here is the default constructor and the destructor for the list class:
CONSTR
1 2 3 4
|
Liste::Liste()
{
tetePtr = NULL;
}
|
DESTR
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
void Liste::supprimer(Place p)
{
Elem *i = p.elem;
if (i == tetePtr)
tetePtr = tetePtr->suiv;
else
{
Elem *j = tetePtr, *k = NULL;
while (j != i)
{
k = j;
j = j->suiv;
}
k->suiv = i->suiv;
}
delete i;
}
|
Note: this isn't the actual ~list but it leads to this one.
I'm thinking that something goes wrong when I do:
Index[i] = new A;
I don't know how it initializes the members but apparently something goes wrong when initializing the list member. I've tested class A in a stand-alone setup without class B.
1 2
|
A *ptrA;
ptrA = new A;
|
works just fine and it calls the destructor properly, but when I use class A inside class B I get the Seg Fault. I also wonder why does the program wants to delete Entries after the while{} which is in the middle of a function? Is there a way to bypass this?