Question

For some reason, when I use the destructor for my Update class, a debug assertion fail message displays...

here is my Update class with some code omitted for brevity. Placed in a header file:

using namespace std;

class Update
{
private:
    int day, month, year;
static const int FIELD_SIZE = 3, DEFAULT_DAY = 12, DEFAULT_MONTH = 12,
    DEFAULT_YEAR = 1999, DAYS_IN_MONTH = 30, MONTHS_IN_YEAR = 12, DAYS_IN_YEAR = 365;

int * date;

public:
static int dateUpdate;

Update(int D, int M, int Y)
{
    day = D;
    if (day < 1 || day > DAYS_IN_MONTH)
        day = DEFAULT_DAY;
    month = M;
    if (month < 1 || month > MONTHS_IN_YEAR)
        month = DEFAULT_MONTH;
    year = Y;
    if (year < 1)
        year = DEFAULT_YEAR;

    date = new int [FIELD_SIZE];
    date[0] = day, date[1] = month, date[2] = year;

    dateUpdate++;
}

~Update()
{
    delete [] date;

    dateUpdate--;
}

};

and here is my tester class in a cpp file:

#include <iostream>
#include "Update.h"

int Update::dateUpdate = 0;

int main()
{
Update u1(29, 12, 2000);
u1.Update::~Update();

return 0;
}

I've read through other questions involving debug assertion failures but something tells me a debug assertion failure can occur in various ways. As a result, I have little clue as to why the error message is displaying for my code... Is there something wrong with my destructor as I suspect at the moment? Thank you so much for your help in advance!

Was it helpful?

Solution 2

You should change this line:
date[0] = day, date[1] = month, date[2] = year;

To:

date[0] = day;
date[1] = month;
date[2] = year;

You are using the comma operator, which returns the result of the last expression. This is not the same as how the comma operates in an initialization.

Also, in your main function, you do not need to explicitly call the destructor.

Your date method can't handle January 31 nor December 31.

You can access the parameters of the function directly instead of making a copy in the function. For example: day = D; is not needed; access the parameter directly: if ((D < 1) ....

If you used unsigned integers you would not need to check for negative numbers. I've never experienced a negative day, month or year in my life.

Since this is C++ and not Java or C#, you don't need to dynamically allocate variables. So rather than using int * date you could use int date[3].

OTHER TIPS

The problem is because you are calling destructor explicitly:

u1.Update::~Update();

this way it is called twice causing undefined behaviour, I suppose delete [] date; is called twice, the second time on alreade freed memory.

Another problem in your code is that you are using bare pointer for you array:

int * date;

this is actually quite low level programming style in C++ and can cause lots of problems. You should implement class copy constructor and assignment operator (*) that will allocate new date array when your Update class will be copied, otherwise you will have again problems with multiple date pointer deletions.

The best way is to use vector like

std::vector<int> date;

(*) I think good link where rule of three (or since C++11 rule of five) that applies here is explained: Rule-of-Three becomes Rule-of-Five with C++11?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top