Errors while trying to free memory

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?
Why isn't B::Index defined as vector<A>?
If you mean like A* Index[100]: I'm doing Index = new A*[100] in B(), If I declare A* Index[100] I won't be needing the Index = new A*[100].
That's an array. This is vector: http://www.cplusplus.com/reference/stl/vector/

Did you define a destructor, copy constructor and assignment operator for B? They are needed.

Here are a lot of unsaid things.

The struct in A is called "list", but you provided ctor's and dtors for an struct called "Liste".

You mention a segfault in a destructor but never says what are you deleting at all (or what goes out of scope). An instance of B? Then where is the destructor of B? Maybe you delete "Index" using a normal "delete Index" in the destructor? You have to use "delete [] Index". But this usually doesn't cause a segfault but memory leak. (But it could cause a segfault! ;). Or better use the STL for vectors. Much easier and not much slower.

1
2
3
4
5
class B
{
     B(): Index(100){};
private:
     std::vector<A*> Index;



Please, try to simplify your example down to a self-containing piece of code fitting on one page and then post the whole simplified version (if the error didn't already became visible to you during the process of simplification ;-)
Topic archived. No new replies allowed.