20.5 Rules for Windows Object Ownership

Windows memory objects can be in global or local memory. Windows objects include the following:

Bitmaps

Metafiles

Application code segments

Resources (except fonts)

Windows treats memory objects as follows:

An application that allocates memory owns that memory.

When a dynamic-link library allocates a global object, the application that called the library owns that object.

When an application or dynamic-link library terminates, Windows purges the system of all objects and window classes owned by that application or library.

Data sharing should be performed by using the clipboard or dynamic data exchange (DDE), although you can also share data by using the data segment of a dynamic-link library. When using the clipboard or DDE, Windows copies the data into the private address space of the receiving application.

GDI objects (pens, brushes, device contexts, and regions) are not typical Windows objects in that they are not purged when the owning application terminates. For this reason, an application or dynamic-link library must explicitly destroy any GDI objects it created before terminating.