views:

85

answers:

1

ok, so i got a some what 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

+3  A: 

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.

Nikolai N Fetissov
ok so the desturctor will call the unit destructors of all the unit objects in the list?
Yes, when you put an object into STL container, a *copy* is made into container-managed memory. When container is destructed, that copy is destructed, and memory deallocated.
Nikolai N Fetissov