I have a doubly threaded linked list made up of winery
elements, winery
being a class with its own data fields. The problem that I was having was with winery
's destructor:
winery::~winery()
{
delete[] name;
delete[] location;
}
I received a "double free or corruption (out): 0x0000000000402940" error in Linux, and I received this error in Windows Visual Studio:
So I put a breakpoint on my winery
's destructor and found that it was being called every time I referenced my winery object. Strangely, the only time an error was thrown was when winery
's destructor was called because my list
's destructor had been called, even if winery had already been called two or three times. I would like to know why my code didn't crash the second time winery
's destructor was called when it wasn't called by its parent destructor.
As a side note/question: I was able to solve this issue of memory leakage by removing winery
's destructor and putting its calls in it parent destructor like this:
list::~list()
{
for (node* next; headByName; headByName = next)
{
next = headByName->nextByName;
delete[] headByName->item.getName();
delete[] headByName->item.getLocation();
delete headByName;
}
}
winery
is a type with name 'item'
and getName()
and getLocation()
are functions of winery which return those cstrings. This works but it's counter-intuitive, and it certainly seems inefficient. Is it bad code? Should I be using `winery's destructor and overriding when it's called somehow?