Auto-destruction of classes within classes

Mar 22, 2012 at 8:04pm
Hello all,

This may be a simple question but I wanted to ask those who know more than me. I have two classes made. Each one is a queue using linked lists and so they use new and delete to build and destroy the chain as memory management is very important.

Each class is different with different variables stored privately but one of the queues has an instance of the other one inside of it. If we call the first queue #1 and label it the parent queue, it has an instance of #2 inside of it. Each queue separately has a destructor function to clear itself out when the class is finished with.

I have a function called from main that creates one instance of queue #1 which of course creates an instance of queue #2. Nothing is static or global and so when I leave this function, I am hoping the destructors are all activated to remove every bit of memory allocated here.

Do you think it will work this way? This is a program I want to build and leave running for months and I am hoping that I won't end up with giant memory leaks.

Thank you so much!


Orad
Mar 22, 2012 at 8:32pm
The pseudocode to represent your idea is something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class FirstClass {
 // ....
};
class ContainerOfFirstClass {
  private:
    FirstClass * ClassPointer;
  public:
    ContainerOfFirstClass() { // Constructor
      ClassPointer = new FirstClass(); // Generate a FirstClass, but it will not get deleted by himself,
      // so we need to create a destructor for ContainerOfFirstClass
   }
   ~ContainerOfFirstClass() { // Destructor
     if(ClassPointer)
       delete ClassPointer; // In the destructor, we delete our FirstClass pointer named ClassPointer
     ClassPointer = 0; // Make sure you cannot access delete'd data.
   }
}
Mar 22, 2012 at 8:34pm
@OradFarez: I don't like the sound of what you have written. This is the only case where mamory management is automatic:
1
2
3
4
5
6
7
8
9
10
int main()
{
    MyQueueClass mqc; //construct on the stack
    MyQueueClass mqc2 (mqc); //copy construct on the stack
    MyQueueClass *mqcp = new MyQueueClass; //construct on the heap

    //...

    delete mqcp; //destruct from the heap
}//destruct mqc and mqc2 from the stack 
What you wrote *sounds* like you have separate construction and destruction functions than the in-class C++ ones. Please clarify.
Last edited on Mar 22, 2012 at 8:35pm
Mar 22, 2012 at 8:50pm
Hi!

It's not quite like that. Class #2 is really simple (I don't see that one in your example code). It has one data element and a pointer to the next element (which is NULL at first). It is accessible and searchable in one direction only (head to tail).

Class #1 has other data elements and a pointer to the next list element for #1. It also has an instance of Class #2 declared like this: SecondClass class2;

I didn't use a pointer, just a straight declaration as if it were any normal variable, struct, etc.

The destructor in each queue type just 'pops' the elements out of the list out and deletes their reserved space until there is nothing left in the list anymore. I assume the head and tail pointers declared inside the class are deleted on their own in the 'normal' way and returning function dumps temporary variables.

I should try to be as specific as I can. Here is more pseudo code of the function call:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int main()
{
    while(1) {
        function_with_queues();
        //when I get back here, I want all the memory used by the queues to be freed
    }
}

void function_with_queues(void)
{
    QueueOne  q1;

    //Work with q1, build it up based on data from user
    //Process the long list of q1 with a long list of q2 elements in each one
    //Provide user with some solution
}


I'm basically just trying to make a linked list with another linked list embedded inside of it so that each link in the chain of #1 can have a list of dynamically created integers stored in list #2. There might be code examples out there for this but I haven't found one yet.

Thanks for jumping in to help,


Orad

Last edited on Mar 22, 2012 at 9:02pm
Topic archived. No new replies allowed.