Garbage Collection

Overview

The Excel implementation of ObjectHandler supports function ohRepositoryCollectGarbage which can be called to cull objects that have been orphaned in the repository by events such as the closing of a workbook.

A cell formula invokes the constructor of an object, causing that object to be added to the repository. The object is assigned an ID which is returned to the calling cell, allowing the object to be accessed, e.g. the ID of one object could be passed as an input to the constructor of another.

An object persists in the repository even after the formula which instantiated the object is deleted - this happens, for example, when a workbook is closed. The object is not lost, its ID can still be retrieved and used to access the object.

However the object is likely redundant and a mechanism for cleaning it up is desirable. Function ohRepositoryCollectGarbage iterates through all the objects in the repository and culls those objects which have been orphaned by the deletion of the calling cell.

Diagnostics

  • ohObjectCallerAddress may be called on an object ID to retrieve the address of the cell in which the object was instantiated. If this address comes back as a #REF it indicates that the object is orphaned and will be culled next time ohRepositoryCollectGarbage is called.
  • ohRepositoryLogAllObjects dumps the complete contents of the ObjectHandler repository to the log file, this is followed by a summary of all of the references maintained by ObjectHandler in support of garbage collection.

Acknowledgements

ObjectHandler's garbage collection functionality is an implementation of a technique presented in Excel Add-in Development in C/C++: Applications in Finance by Steve Dalton.