Microsoft KB Archive/61777

{|
 * width="100%"|

Why GlobalAlloc Returns a Handle to a 0-Byte Segment

 * }

Q61777

-

The information in this article applies to:


 * Microsoft Windows Software Development Kit (SDK) versions 3.0, 3.1

-

SUMMARY
When Calling GlobalAlloc with a segment of size 0 (zero), Windows returns a handle to the memory. Attempting a GlobalLock using that handle returns a NULL pointer. Since a 0-byte block of memory is treated by the system as if it is discarded, the following explains how these memory handles can be reused.

Consider the following scenario:


 * 1) hMemory = GlobalAlloc (GMEM_MOVEABLE, 0L);

This returns a handle to a block of memory of length 0 (zero).
 * 1) wFlags = GlobalFlags (hMemory);

wFlags is 0x4000, which is GMEM_DISCARDED. Thus, the memory object is thought of as if it has already been discarded.
 * 1) lpszMemory = GlobalLock (hMemory);

This fails. lpszMemory == 0.
 * 1) hMemory = GlobalReAlloc (hMemory, 1L, GMEM_MOVEABLE);

This returns the handle, and sets the size of the block to 1 byte.
 * 1) wFlags = GlobalFlags (hMemory);

wFlags = 0 (zero), which is GMEM_FIXED (Windows protected mode or in Windows 3.1, GMEM_FIXED and GMEM_MOVEABLE are the same).
 * 1) lpszMemory = GlobalLock (hMemory);

This returns a valid far pointer to a 1-byte buffer.

When the lock count of a GMEM_DISCARDABLE object reaches 0 (zero), it is a candidate for discarding. If it has been discarded, it cannot be locked or used because the memory it refers to has been deallocated. However, it can be reallocated and then locked. As a result, the memory handles are reused after having been discarded.

Additional query words: 3.00 3.10

Keywords : kb16bitonly

Issue type :

Technology : kbAudDeveloper kbWin3xSearch kbSDKSearch kbWinSDKSearch kbWinSDK300 kbWinSDK310