How to Delete All Objects in a CObject Collection

To delete all the objects in a collection of CObjects (or of objects derived from CObject), you use one of the iteration techniques described above to delete each object in turn.

Note:

Note that objects in collections can be shared. That is, the collection keeps a pointer to the object, but other parts of the program may also have pointers to the same object. You must be careful not to delete an object that is shared until all the parts have finished using the object.

·To delete all objects in a CObList:

You can use the following technique to delete all objects in a CObList or a list derived from CObList.

1.Use GetHeadPosition and GetNext to iterate through the list.

2.Use the delete operator to delete each object as it is encountered in the iteration.

3.Call the RemoveAll function to remove all elements from the list after the objects associated with those elements have been deleted.

The following example shows how to delete all objects from a list of
CPerson
objects. Each object in the list is a pointer to a CPerson object that was originally allocated on the heap.

class CPersonList : public CObList {...};

CPersonList myList

POSITION pos = myList.GetHeadPosition();

while( pos != NULL )

{

delete myList.GetNext( pos );

}

myList.RemoveAll();

Summary: Remove an element after its object is deleted.

The last function call, RemoveAll, is a list member function that removes all elements from the list. The member function RemoveAt will remove a single element.

Notice the difference between deleting an element's object and removing the element itself. Removing an element from the list merely removes the list's reference to the object. The object still exists in memory. When you delete an object, its memory is reclaimed and it ceases to exist. Thus, it is important to remove an element immediately after the element's object has been deleted so that the list won't try to access objects that no longer exist.

·To delete all elements in an array:

1.Use GetSize and integer index values to iterate through the array.

2.Use the delete operator to delete each element as it is encountered in the iteration.

3.Call the RemoveAll function to remove all elements from the array after they have been deleted.

The code for deleting all elements of an array is as follows:

CObArray myArray;

int i = 0;

while (i < myArray.GetSize() )

{

delete myArray.GetAt( i++ );

}

myArray.RemoveAll();

Like the list example, you can call RemoveAll to remove all elements in an array or RemoveAt to remove an individual element.

·To delete all elements in a map:

1.Use GetStartPosition and GetNextAssociation to iterate through the array.

2.Use the delete operator to delete the key and/or value for each map element as it is encountered in the iteration.

3.Call the RemoveAll function to remove all elements from the map after they have been deleted.

The code for deleting all elements of a CMapStringToOb is as follows. Each element in the map has a string as the key and a CPerson object (derived from CObject) as the value.

CMapStringToOb myMap;

// ... add some key-value elements...

// now delete the elements

pos = myMap.GetStartPosition();

while( pos != NULL )

{

CObject* pObject;

CString string;

// gets key ( string ) and value ( pObject )

myMap.GetNextAssoc( pos, string, pObject );

delete pObject;

}

myMap.RemoveAll();

You can call RemoveAll to remove all elements in a map or RemoveKey to remove an individual element with the specified key.