March 97 - The OpenDoc Road
THE OPENDOC ROAD
Making the Most of Memory in OpenDoc
Troy Gaul and Vincent Lo
In Issue 28, we discussed how the OpenDoc Memory Manager works and how part
editors manage Toolbox memory. This time we'll examine ways to use memory more
efficiently in the OpenDoc environment.
We'll begin by talking about how to avoid memory leaks. Memory leaks, which can be a
problem when developing traditional Macintosh applications, are as much a concern in
OpenDoc. But because OpenDoc uses reference counting, there are a few extra things to
pay attention to. We'll also discuss how to handle parameters correctly to avoid
memory leaks, and we'll take a look at ways you can set up your part editor to
maximize memory usage.
AVOIDING MEMORY LEAKS
OpenDoc objects and part editors use a reference-counting scheme that enables
OpenDoc to keep track of which objects are in use. Each time a client acquires an object
(through the object's Acquire method), the object's reference count is incremented by
1. When the object is no longer being used, the client releases it (by calling the
object's Release method) and its reference count is decremented. The object's
reference count indicates how many references to the object are being held by clients.
When the reference count goes down to 0, the object can be destroyed without affecting
any other objects. For more information on how reference counting works in OpenDoc,
see the OpenDoc Road column in develop Issue 27, "Facilitating Part Editor Unloading.
If the acquired object doesn't get released when it should, the reference count doesn't go
to 0 and the object remains in memory until the session ends. As a result, a memory
leak occurs because the occupied memory can't be used during the session.
To avoid reference count errors, it helps to keep in mind which classes are
reference-counted and which methods affect an object's reference count. OpenDoc uses
reference counting on classes whose objects often have more than one client. These
classes are subclasses of ODRefCntObject, and many are classes that part editors
interact with directly.
In general, if a method name starts with "Acquire," the reference count of the object
named in the method is incremented when the method is called. When the object is no