Win32 Subsystem Object Cleanup

Last reviewed: December 17, 1996
Article ID: Q89290
The information in this article applies to:
  • Microsoft Win32 Software Development Kit (SDK) for Windows NT versions 3.1, 3.5, 3.51, and 4.0

SUMMARY

The Win32 subsystem guarantees that all Win32 objects owned by a process will be freed when an application terminates. To accomplish this, the Win32 subsystem keeps track of who owns these objects; it also keeps a reference count. Reference counts are used when the object is owned by more then one process. For example, a memory mapped file can be used to provide interprocess communication, where more than one process would own that object. The subsystem must make sure that the reference count is zero before the object can be freed.

Freeing of Win32 objects can occur at different times. In general, it occurs at process termination, but for some objects, it occurs at thread termination.

NOTE: When running Win32-based applications with Windows 3.1 using the Win32s environment, it is the responsibility of the Win32-based application to ensure that all allocated GDI objects are deleted before the program terminates. This is different from the behavior of the application with Windows NT. With Windows NT, the GDI subsystem cleans up all orhphaned GDI objects. Because there is no GDI subsystem with Windows 3.1, this behavior is not supported.

MORE INFORMATION

At process or thread termination, the Win32 subsystem searches its lists to find objects owned by this process or thread. Those that are owned by the terminating process or thread and whose reference counts will be set to zero when the process or thread is fully terminated will be freed.

The freeing of objects is slightly different for Win32-based applications running under Win32s on Windows 3.1. The 16-bit objects (GDI objects, windows, global memory, etc.) follow the same clean-up rules as Windows- based applications do under Window 3.1. The 32-bit objects, such as memory allocated via VirtualAlloc(), shared memory via mapped file I/O, 32-bit modules, thunks allocated on the fly (for hook procedures, wndprocs etc.) are all handled by Win32s and freed at process termination.

The following is a list of Win32 objects. Note that it may not be complete.

   BASE: console, event, file (including file mapping), mutex,
         semaphore, thread, process, pipe (including named pipes)

   GDI:  device context (DC), bitmap, pen, brush, font, region, palette

   USER: window, cursor, icon, menu, accelerator table, desktop,
         DDE communication objects, DDE conversation objects, dialog


KBCategory: kbprg
KBSubcategory: SubSys
Additional reference words: 3.10 3.50 3.51 4.00


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: December 17, 1996
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.