Rules for Windows Object Ownership

Windows memory “objects” can be in global or local memory. Windows objects include:

Bitmaps

Metafiles

Application code segments

Resources (except fonts)

Windows treats memory objects as follows:

An application that allocates memory owns that memory.

When a DLL allocates a global object, the application that called the DLL owns that object.

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

Data sharing should be performed using the Clipboard or dynamic data exchange (DDE), although you can also share data using the data segment of a DLL. 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 DLL must explicitly destroy any GDI objects it created before it terminates.