Step 8: Deallocating the DirectDraw Objects

When you press the f12 key, the DDEX1 application processes the WM_DESTROY message before exiting the application. This message calls the finiObjects sample function, which contains all of the IUnknown::Release calls, as shown in the following example:

static void finiObjects(void) 
{ 
    if(lpDD != NULL) 
    { 
        if(lpDDSPrimary != NULL) 
        { 
            lpDDSPrimary->Release(); 
            lpDDSPrimary = NULL; 
        } 
        lpDD->Release(); 
        lpDD = NULL; 
    } 
} // finiObjects 
 

The application checks if the pointers to the DirectDraw object (lpDD) and the DirectDrawSurface object (lpDDSPrimary) are not equal to NULL. Then DDEX1 calls the IDirectDrawSurface3::Release method to decrease the reference count of the DirectDrawSurface object by 1. Because this brings the reference count to 0, the DirectDrawSurface object is deallocated. The DirectDrawSurface pointer is then destroyed by setting its value to NULL. Next, the application calls IDirectDraw2::Release to decrease the reference count of the DirectDraw object to 0, deallocating the DirectDraw object. This pointer is then also destroyed by setting its value to NULL.