Safe Pointers

Memory management can be perilous in C++. It's important that objects allocated on a heap are deleted once and only once. Given the fact that objects are so easily passed around within the CommonPoint application system, your programs will often have multiple references to the same object. It is equally important to ensure that object memory is returned to the heap when objects are no longer needed. Memory leaks can waste system resources if objects are not deleted at the appropriate time.

Safe pointers encapsulate various memory management policies. Most safe pointers automatically delete the objects they point to when the pointer itself goes out of scope. Further, most types of safe pointers check to see if it is acceptable to delete referenced objects before calling delete. The specific type of safe pointer you use depends on the memory management policy you want to practice for a particular object or set of objects. For example an alias pointer points to an object, but does not own it and therefore will not delete the object when the pointer goes out of scope. An instance pointer, on the other hand, owns the object it points to and will delete it automatically at the appropriate time. A reference counted pointer shares the object it points to and will only delete it if no other pointers currently refer to the object when the pointer goes out of scope.

After reading this chapter, you will know:

Pointers and memory management
TPointerTo<>
TAliasTo<>
TOnlyPointerTo<>
TInstanceOf<>

[Contents] [Previous] [Next]
Click the icon to mail questions or corrections about this material to Taligent personnel.
Copyright©1995 Taligent,Inc. All rights reserved.

Generated with WebMaker