Releasing the Direct3DRM Objects

The last task performed by the BuildScene function in Egg.c is to release the Direct3DRM objects it creates—the frames for the mesh and lights, the mesh itself, and the two lights:

RELEASE(egg);

RELEASE(lights);

RELEASE(egg_builder);

RELEASE(light1);

RELEASE(light2);

The RELEASE macro calls the Release method for the appropriate interface. It is defined in C++ and C versions in the Rmdemo.h header file. The syntax of the C version looks like this:

#define RELEASE(x) if (x != NULL) {x->lpVtbl->Release(x); x = NULL;}

Rmmain.cpp also releases any objects it creates, in its CleanUpAndPostQuit function. The code calls CleanUpAndPostQuit whenever initialization has been completed and the application must exit: when a WM_QUIT message is received, when the user chooses the Exit command, on fatal errors, and so on.

void CleanUpAndPostQuit(void)

{

myglobs.bInitialized = FALSE;

RELEASE(myglobs.scene);

RELEASE(myglobs.camera);

RELEASE(myglobs.view);

RELEASE(myglobs.dev);

RELEASE(lpD3DRM);

RELEASE(lpDDClipper);

myglobs.bQuit = TRUE;

}