Why GlobalAlloc() Returns a Handle to a 0-Byte Segment (61777)



The information in this article applies to:

  • Microsoft Windows Software Development Kit (SDK) 3.0
  • Microsoft Windows Software Development Kit (SDK) 3.1

This article was previously published under Q61777

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).
  2. wFlags = GlobalFlags (hMemory);

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

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

    This returns the handle, and sets the size of the block to 1 byte.
  5. 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).
  6. 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.

Modification Type:MajorLast Reviewed:10/30/2003
Keywords:kb16bitonly KB61777