Inbox uses HeapCreate and HeapAlloc to allocate a private heap for each mail message; however, Inbox opens only one mail message at a time, in order to conserve memory. When Inbox is finished working with a message, it frees the allocated memory by calling HeapDestroy. If a service or application needs to create a memory heap, following this method can help to minimize memory fragmentation.
When working with a mail message, if Inbox has already allocated a heap for a message, then the service can reallocate memory on the existing heap. For example, if the handle hHeap in the MailMsg structure is not NULL, that is, if the handle exists, the service can allocate additional memory to this existing heap. In this way, Inbox takes care of freeing memory when it calls the MailFree function.
When following the method just described, the TransportRecv routine should create a private heap using HeapCreate for each mail message. A good initial size is 2 Kb. TransportRecv should store this heap handle in the hHeap member of the MailMsg structure. All dynamic storage in the MailMsg structure will use this private heap. Heap allocated storage should include the szSvcId, szSvcNam, pwcHeaders, and szBody members of the MailMsg structure.
The TransportFreeMsg routine should check for a NULL heap handle, delete the heap, and set the hHeap member of the MailMsg structure to NULL.