News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

read_disk_file usage

Started by Radly, July 02, 2007, 12:53:16 AM

Previous topic - Next topic

Radly

I'm working on an extended tutorial on Assembler, maybe becoming a book even, and am using read_disk_file and write_disk_file in an example.  The documentation on read_disk_file says the memory it allocates for the buffer should be freed with GlobalFree(), and I'm puzzled.  GlobalFree() wants a memory handle, not a memory pointer, which is what the user gets from read_disk_file.  Here's the excerpt from masmlib.hlp: The lpMem value should be deallocated using GlobalFree()...  I haven't actually tried this yet, but eventually I'll have to figure out how to release the memory.  Any clarification would be appreciated. 

rags

Radly,
The hMem(memory handle) is a pointer to the  first byte of allocated memory. In other words hMem =lpMem,
and you would use lpMem in the call to GlobalFree. It might help you understand better, by having a look at the routines in the m32lib folder of the masm32 installation.
Rags
God made Man, but the monkey applied the glue -DEVO

hutch--

Radly,

rags is right, just to expand on what he has told you, the GlobalAlloc() API function allocates memory in a couple of ways, in this instance it uses the FIXED memory flag and the return value when this flag is used is the actual pointer to the beginning of the memory allocated. With GlobalAlloc() the other forms of allocation are left overs from very old 16 bit windows and are usually not used in 32 bit Windows.

With the masm32 library function you are using it allocates the required memory with GlobalAlloc() and passes that address back as its return value, when you have finished using it you deallocate it with GlobalFree().
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

Radly

This reminds me of when I was a kid--I used to love sitting down with a dictionary and just running chains of definitions to see where they took me.  But I digress. :)

I started with read_disk_file and saw that it allocates memory with a call to 'alloc'.  After a slight diversion to 'Alloc', I came back to 'alloc' and found the macro that delegates to GlobalAlloc as described by hutch , but right next to that macro was the 'free' macro.  It seems to me that MUCH better would be for read_disk_file's documentation to recommend using 'free' to release the memory.  Yes, alloc is implemented with GlobalAlloc FIXED today, but that's an implementation detail that the user should not be held captive to.  If the maintainers of MASM32 decide to use some other scheme for alloc and free, the user of read_disk_file is affected.