Hi, Im new to assembly and need a little help. I have a simple program in which the user uses the "File" menu to select "Download" and a file is then downloaded to their temp folder. Nothing is wrong with the program but the memory it uses I am a little unsure over since I'm not familiar freeing/using memory etc. I will paste the important parts of the code:

In the first program I define szPath in .data section:

szPath              BYTE MAX_PATH dup(?) 
szFile      BYTE "file.zip", 0
szURL        BYTE "", 0

DownloadFile PROC
invoke GetTempPath, MAX_PATH, addr szPath
invoke lstrcat, addr szPath, addr szFile
invoke URLDownloadToFile, NULL, addr szURL, addr szPath, 0, NULL
DownloadFile ENDP

And then in WndProc I have.......
.if eax==IDM_FILE_DL
call DownloadFile

In the second program I use GlobalAlloc to define the size of Path instead of sticking it in the .data section:

AllocMem MACRO howbig
    invoke GlobalAlloc,GHND,howbig
    mov hMemory,eax
    invoke GlobalLock,eax
    mov pMemory,eax

DeAllocMem MACRO
    invoke GlobalUnlock,pMemory
    invoke GlobalFree,hMemory
    mov hMemory,0

szFile      BYTE "file.zip", 0
szURL        BYTE "", 0

hMemory DWORD ?
pMemory DWORD ?

DownloadFile PROC

AllocMem MAX_PATH ; MAX_PATH IS A CONST FOR 256 I believe..

invoke GetTempPath, MAX_PATH, pMemory
invoke lstrcat, pMemory, addr szFile
invoke URLDownloadToFile, NULL, addr szURL, pMemory, 0, NULL


;Very simple test to see if it worked
.if eax==NULL
PrintText "Memory DeAllocated"

DownloadFile ENDP

.if eax==IDM_FILE_DL
call DownloadFile

What I don't understand is that when I'm defining szPath with GlobalAlloc and freeing it afterwards it uses MORE memory than just allocating the data from the start of the program.

Am I doing something wrong?

Posted on 2007-03-25 11:53:26 by infinite
Your "szPath" should be a LOCAL variable in a procedure, that way it's allocated on the stack instead of having global lifetime. No reason for dynamic memory allocation for something like this (do use HeapAlloc if you need it, Local/GlobalAlloc are deprecated).
Posted on 2007-03-25 17:25:07 by f0dder
Thanks for the reply f0dder.

I was kind of skeptical about using it like that. Most of the time I see memory operations paired up with file manipulations.

The reason behind my question was after hitting "Download" the memory goes from 2000K to 4000K and stays at 4000K. I was just curious about how I could get it back down to 2000K. I guess it would be better to focus on other things..

Posted on 2007-03-25 17:43:19 by infinite
Well, "memory usage" covers a lot of things. I'd recommend you to get Process Explorer from sysinternals, as it can show more detailed and precise statistics. There's also "perfmon.msc" if you're on an NT system.

The memory statistic you should focus mostly on is "private bytes", which is the amount of memory your application is "really using". The figure that goes from 2meg to 4meg is probably your "working set size", which covers a lot of stuff - even a "hello world" app is at least 2meg on a typical system.

The reason why the memory usage "explodes" like that is probably because of URLDownloadToFile - try commenting that out, just for fun. That call depends on a lot of other coding, including winsock, so this ends up being added to your process working set.

Don't worry too much about this - unused memory is wasted memory, and windows will trim the working set if it needs to :)
Posted on 2007-03-25 18:00:05 by f0dder