i
i
i
i
i
i
i
i
300 12. Tools, Libraries and Templates for VR
interface may be freed, and when all the interfaces supported by a COM
object are released, the memory used by the COM object itself may be freed.
But herein lies a major problem! Get the reference counting wrong and your
program could try to use an object that no longer exists, or it could go on
creating new instances of the object. Not even exiting the program helps;
COM objects exist outside specific applications. Now, you may be a good
programmer who is very careful about freeing up the m emory that you use,
but it is not always easy to tell with the DirectX library how many COM
objects are created when you call for a particular interface.
You may be asking: what has this got to do with me? The answer
is simple: you will be developing programs to make your virtual
world a reality. Those programs will want to run in real time for
a long time (days or weeks of continuous execution). You need
to use COM if you want to use any component of DirectX (and
you will), so if you get reference counting wrong, your program
will either crash the computer or cause it to g rind to a halt!
Luckily, a recent development called the C++ Active Template Library (ATL)
has a special mechanism for automatically releasing COM objects when a
program no longer needs them. We shall employ this strategy in many of our
example programs. Of course, this adds yet another, and visually different,
way of writing COM code to the list, confusing the programmer even more.
ToreadmoreaboutCOM,andthereisalotmoretoberead,thebookby
Templeman [13] shows how COM fits in with the MFC, and Grimes et al. [6]
cov er using COM in the context of the ATL.
Additional details of how to use the COM and a template for writing
COM programs is given in Appendix F.
12.5 Summary
In this chapter, we have raced through a wide range of topics, topics that could
easily occupy several volumes on their own. We have tried to keep the topics
as brief as possible and referred to other sources where each of them is explored
in much greater detail. We hope that you have gained an appreciation of the
way the application programs in the rest of the book will be structured. It
is not essential to have a comprehensive understanding of the detail here in
order to appreciate the practical examples we are now turning to, but we hope
this will make it easier to find your way through the code and thus focus on