Destructor is crashing

08 Dec 2011

Hello, I'm working on a class that has a "queue" in which each instance waits to be serviced. The queue and all of its related variables are static, so all instances have access to the same queue. The queue itself is a dynamically allocated array of pointers to the class instances pointed to by a static pointer.

All I need the destructor to do is check to see if "this" is in the queue, if so remove it, and finally delete and reallocate the static pointer to a new array of the remaining queue.

This all seems to work, debugging shows that the code executes all the way to the ending bracket of the destructor. However the code does not execute after the delete statement in the main code. What is going on between the last line in the destructor and the next line in main?

Is there something more I need to do in the destructor? Am I using it wrong? Please help, I'm not very confident in my understanding of destructors and this problem is making my blood pressure very high X(.

int main{
myClass* myPointer;
myPointer = new myClass;
delete myPointer;
///////////////////////////////////////////code stops here

//...more code...
}

myClass::~myClass
{
    if(QueueSize == 1)
    {
        QueueIndex = 0;
        QueueCount = 0;
        QueueSize = 0;
        delete[] Queue;
    }
    else
    {
        myClass** QueueStorage = new myClass*[QueueSize];

        if (PendingStatus)    //PendingStatus is local, not static
        {
            unsigned char i = 0;
            while (Queue[(QueueIndex + i) % QueueSize] != this)
            {
                i++;
            }
            QueueCount--;
            while (i < QueueCount)
            {
                Queue[(QueueIndex + i) % QueueSize]
                    = Queue[(QueueIndex + i + 1) % QueueSize];
                i++;
            }
        }
        for (unsigned char i = 0; i < QueueCount; i++)
        {
            QueueStorage[i]
                = Queue[(QueueIndex + i) % QueueSize];
        }
        delete[] Queue;
        QueueSize--;
        Queue = new myClass*[QueueSize];
        for (unsigned char i = 0; i < QueueCount; i++)
        {
            Queue[i] = QueueStorage[i];
        }
        delete[] QueueStorage;
        QueueIndex = 0;
    }
    myComputer.printf("done");///////////////////////this will execute!
}
12 Dec 2011

After a lot of research, experimentation, and debugging I managed to answer my own question. So I will post it here just in case anyone wants to know.

There was a memory leak in one of the members of myClass. This member is a class object, and the destructor of that class did not handle the deletion of a couple of its pointers correctly. So upon the execution of myClass's destructor it began releasing the resources of its members, including the class with a faulty destructor. This caused the code to fail between the end of the destructor and the next line in the main code.

I'm sure this is pretty elementary for most of you, but I just learned some important things about destructors.

12 Dec 2011

Hi Thomas,

Great work for taking the time to write up your findings; I'm sure you won't be the last!

Simon

12 Dec 2011

Thank you Simon.

I'm a newbie X) but I refuse to quit!

13 Dec 2011

Thomas Hamilton wrote:

I'm a newbie X) but I refuse to quit!

That's the right attitude! :)