[comp.windows.ms.programmer] Memory management in protected mode

peterb@pnet51.orb.mn.org (Peter Bergstrom) (06/26/91)

I am trying to learn how to manage globally allocated memory and have been
working on a program that allocates a bunch of blocks and then deallocates
them.  The outline of the program is to have two menu choices: Get and Release
that cause my program to GlobalAlloc and GlobalFree a chunk of memory.  This
seems to work well (tracing using TDW shows the data structure being updated
correctly) until I exit.  After I GlobalAlloc the block, I GlobalLock it in
place.  Before I GlobalFree the block, I GlobalUnlock it.  Each block is
Alloc,Lock,Unlock, and Free'd in the correct order.  

The problem comes when I exit.  It usually takes a few minutes, but Windows
always crashes, usually hard (Ctl-Alt-Del).  This happens whether or not I

specifically release all the memory or let my WM_DESTROY message code free it
(it does the same thing for all the blocks that the Release menu choice does
for one.

I am baffled by this behavior - Metz' Freemem reports the correct amount of
memory free after my program exits (same as before), so I think it works OK. 
I can't seem to trace through the WM_DESTROY message with TDW - it skips a
loop that *should* be working.

So, I have the following questions and/or thoughts that I would appreciate
comments on:
1) Does Windows reclaim the memory that a program allocates automatically?
2) Is it impossible to debug using TDW reliably (I can trace through all the
rest of the program)?
3) Do you have specific hints on how to effectively use global memory or
references to same?

Note: I own and *have read* Petzold, THE Guide, THE Reference and Richter's
Developer's Guide.  I am really baffled by this and must learn more.

Thanks for any help,
Pete Bergstrom


UUCP: {amdahl!bungia, crash}!orbit!pnet51!peterb
ARPA: crash!orbit!pnet51!peterb@nosc.mil
INET: peterb@pnet51.orb.mn.org