The Old New Thing by Raymond Chen

The Old New Thing by Raymond Chen

Author:Raymond Chen
Language: eng
Format: epub
Publisher: Pearson Education Limited (US titles)
Published: 2011-11-15T05:00:00+00:00

What was the point of the GMEM_SHARE flag?


In 16-bit Windows, the GMEM_SHARE flag controlled whether the memory should outlive the process that allocated it. By default, all memory allocated by a process was automatically freed when that process exited.

Passing the GMEM_SHARE flag suppresses this automatic cleanup. That’s why you have to use this flag when allocating memory to be placed on the clipboard or when you transfer it via OLE to another process. Because the clipboard exists after your program exits, any data you put on the clipboard needs to outlive the program. If you neglect to set this flag, then when your program exits, the memory that you put on the clipboard will be cleaned up, resulting in a crash the next time someone tries to read that data from the clipboard. (The GMEM_SHARE flag also controls whether the memory can be freed by a process other than the one that allocated it. This makes sense given the preceding semantics.)

Note that the cleanup rule applies to global memory allocated by DLLs on behalf of a process. Authors of DLLs had to be careful to keep track of whether any particular memory allocation was specific to a process (and should be freed when the process exited) or whether it was something the DLL was planning on sharing across processes for its own internal bookkeeping (in which case it shouldn’t be freed). Failure to be mindful of this distinction would lead to puzzling crashes.

Thank goodness this is all gone in Win32.


Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.