Prevent destructor delete member pointer from constructor

Hi,

I have little problem which causing memory leaks.

Parent > Multiple Child(Parent parent) > Child destructor deleting parent => next Child destructor crash

Example code: without using:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
class Parent
{
public:
  Parent()
  {
     for(int i = 0; i < x; ++i)
     {
        for(int j = 0; j < y; ++j)
           childs[i][j] = new Child(this);
     }
  }

  ~Parent()
  {
     for(int i = 0; i < x; ++i)
     {
        for(int j = 0; j < y; ++j)
         {  delete childs[i][j]; childs[i][j] = null; }
     }
  }

private:
  Child* childs[x][y];
}

class Child()
{
public:
   Child(Parent* parent)
   {
      _parent = parent;
   }

  ~Child()
  {
  }

private:
   Parent* _parent;
}


If you read code, on Parent destructor i = 0 & j = 1 its going crash.

Parent will be deleted aswell, but it give me assert: _block_type_is_valid(phead- nblockuse)

Thanks for help!
http://www.cplusplus.com/forum/general/112111/

> on Parent destructor i = 0 & j = 1 its going crash.
¿why?

> Child destructor deleting parent
Your `Child' destructor does nothing.


Consider using std::vector<Child> (or std::vector< std::smart_ptr<Child> > if `Child is an abstract class)
Child is not abstract class, also desturctor delete every member on call without any char in it, if im wrong, how can my Parent be deleted on second deletion call?

Also Im using Qt so if you know some library, I test QSharedPointer and that didnt work :/
> Child is not abstract class
¿then why are you storing pointers?

> also desturctor delete every member on call without any char in it
~Child() is empty, it deletes nothing.
I don't understand what you mean with `without any char in it'

> if im wrong, how can my Parent be deleted on second deletion call?
The code that you posted is not deleting any `Parent' object
Topic archived. No new replies allowed.