Microsoft KB Archive/83808

{|
 * width="100%"|

INFO: Multiple References to the Same Resource

 * }

Q83808

-

The information in this article applies to:


 * Microsoft Windows Software Development Kit (SDK)
 * Microsoft Win32 Software Development Kit (SDK)

-

SUMMARY
Windows supports multiple references to a given resource. For example, suppose that an application has two top-level menus that each contain the same submenu. (An application can use the AppendMenu or SetMenu functions to add a submenu to another menu at run time.)

Normally, destroying a menu destroys all of its submenus. In the case above, however, when one menu is destroyed, the other menu has a lock on the common submenu. Therefore, the common submenu remains in memory and is not destroyed. The handle to the submenu remains valid until all references to the submenu are removed. The submenu either remains in memory or is discarded, while its handle remains valid.

MORE INFORMATION
Windows maintains a lock count for each resource, including menus. When the lock count falls to zero, Windows can free (destroy) the object. Each time an application loads a resource, its lock count is incremented. If a resource is loaded more than once, only one copy is created; subsequent loads only increment the lock count. Each call to free a resource decrements its lock count.

When the LoadResource function determines if a resource has already been loaded, it also determines if the resource has been discarded. If so, LoadResource loads the resource again. The resource is not necessarily present in memory at all times. However, if the lock count is not zero and the resource is discarded, Windows will automatically reload the resource. All resources are discardable and will be discarded if required to free memory.

Therefore, in the example above, the application's call to the DestroyMenu function calls FreeResource, which checks the lock count. This process is analogous to LoadMenu, which calls LoadResource.

Additional query words:

Keywords : kbOSWinNT kbOSWin2000 kbResource kbSDKWin32 kbGrpDSUser kbOSWin kbwin32sdkfaq

Issue type : kbinfo

Technology : kbWin32SDKSearch kbAudDeveloper kbSDKSearch kbWin32sSearch kbWinSDKSearch