C++ destructors causing crash's
ok, so i got a some what 开发者_StackOverflow社区intricate program that simulates the uni systems of students, units, and students enrolling in units.
Students are stored in a binary search tree, Units are stored in a standard list.
Student has a list of Unit Pointers, to store which units he/she is enrolled in Unit has a list of Student pointers, to store students which are enrolled in that unit.
The unit collections (storing units in a list) as made as a static variable where the main function is, as is the Binary search tree of students.
when its finaly time to close the program, i call the destructors of each. but at some stage, during the destructors on the unit side,
Unhandled exception at 0x002e4200 in ClassAllocation.exe: 0xC0000005: Access violation reading location 0x00000000.
UnitCollection destructor:
UnitCol::~UnitCol()
{
list<Unit>::iterator itr;
for(itr = UnitCollection.begin(); itr != UnitCollection.end();)
{
UnitCollection.pop_front();
itr = UnitCollection.begin();
}
}
Unit Destructor
Unit::~Unit()
{
}
now i got the same sorta problem on the student side of things
BST destructors
void StudentCol::Destructor(const BTreeNode * r)
{
if(r!= 0)
{
Destructor(r->getLChild());
Destructor(r->getRChild());
delete r;
}
}
StudentCol::~StudentCol()
{
Destructor(root);
}
Student Destructor
Student::~Student()
{
}
so yeah any help would be greatly appreciated
If your UnitCollection
is std::list<Unit>
then you don't have to manually remove items - the list itself with destroy contained objects and deallocate the memory in its own destructor.
Take a look at std::list
documentation.
I would also suggest that you post complete code - some of your description is contradictory.
精彩评论