flat assembler
Message board for the users of flat assembler.

Index > Windows > Memory leak in CreateProcess

Author
Thread Post new topic Reply to topic
ChrisLeslie



Joined: 04 Jun 2006
Posts: 50
Location: Australia
ChrisLeslie
I am experiencing a gradual chewing away of memory when calling an "execute" procedure. Since my application runs continuously and the procedure is called regularly I run out of memory within a couple of hours! I have confined the leak to this procedure but cannot see why it goes wrong. Can anybody help?

Code:
proc execute prognamePtr:DWORD
  local dwTemp:DWORD
    mov esi,[prognamePtr]
    invoke CreateProcess,0,esi,NULL,NULL,NULL,NULL,NULL,NULL,st_info,pr_info
    .if eax = 0
      ; some error handling stuff
      mov eax,0
    .else
      .L1:
      invoke GetExitCodeProcess,[pr_info.hProcess],dwTemp
      invoke Sleep,1
      cmp [dwTemp],STILL_ACTIVE
      je .L1
      invoke CloseHandle, pr_info.hThread
      invoke CloseHandle, pr_info.hProcess
      mov eax,1
    .endif
    ret
endp    


The structs are declared globally elsewhere.

Regards

Chris
Post 03 Oct 2006, 06:22
View user's profile Send private message Reply with quote
Garthower



Joined: 21 Apr 2006
Posts: 158
Location: Ukraine
Garthower
Show, how you fill structs please.
Post 03 Oct 2006, 07:52
View user's profile Send private message Visit poster's website MSN Messenger ICQ Number Reply with quote
ChrisLeslie



Joined: 04 Jun 2006
Posts: 50
Location: Australia
ChrisLeslie
Ah! I am not filling any members. Are there any that definitely need filling?

Chris
Post 03 Oct 2006, 08:18
View user's profile Send private message Reply with quote
Garthower



Joined: 21 Apr 2006
Posts: 158
Location: Ukraine
Garthower
Yes, it's need to fill them. Otherwise consequences are unpredictable.
Post 03 Oct 2006, 08:24
View user's profile Send private message Visit poster's website MSN Messenger ICQ Number Reply with quote
ChrisLeslie



Joined: 04 Jun 2006
Posts: 50
Location: Australia
ChrisLeslie
I notice in other examples that STARTUPINFO member cb is initialised to the size of the struct. How is the size determined using FASM?
Also, I am a bit confused by this because I think that the size should be constant.
Post 03 Oct 2006, 08:48
View user's profile Send private message Reply with quote
madmatt



Joined: 07 Oct 2003
Posts: 1045
Location: Michigan, USA
madmatt
if you are using fasm's 'struct' then a label is automatically created: sizeof.STARTUPINFO

also:
invoke CloseHandle, pr_info.hThread
invoke CloseHandle, pr_info.hProcess
should be:
invoke CloseHandle, [pr_info.hThread]
invoke CloseHandle, [pr_info.hProcess]
Post 03 Oct 2006, 08:55
View user's profile Send private message Reply with quote
Garthower



Joined: 21 Apr 2006
Posts: 158
Location: Ukraine
Garthower
Or you are not using fasm's "struct" macro you can do this with labels, for example so:

Code:

.....

pr_info:
       cb dd ? 
       lpReserved dd ? 
       lpDesktop dd ? 
       lpTitle dd ? 
       dwX dd ? 
       dwY dd ? 
       dwXSize dd ? 
       dwYSize dd ? 
       dwXCountChars dd ? 
       dwYCountChars dd ? 
       dwFillAttribute dd ? 
       dwFlags dd ? 
       wShowWindow dw ? 
       cbReserved2 dw ? 
       lpReserved2 dd ? 
       hStdInput dd ? 
       hStdOutput dd ? 
       hStdError dd ? 
End_CB:

.....

mov [cb],End_CB-pr_info

.....

invoke CreateProcess,0,esi,NULL,NULL,NULL,NULL,NULL,NULL,st_info,pr_info

.....

    
Post 03 Oct 2006, 09:06
View user's profile Send private message Visit poster's website MSN Messenger ICQ Number Reply with quote
ChrisLeslie



Joined: 04 Jun 2006
Posts: 50
Location: Australia
ChrisLeslie
Thankyou to all who replied.

I have fully initialised all the struct members, and remembered to put in the square brackets (stupid me!!) and run the application continuously for a period of six hours so far with no loss of memory. Looks like that problem has just bitten the dust. Very Happy

It is important to not consume any memory in this application because when it is finally put to real use it will be running 24-7 in a data colection environment (actually, collecting Alpha particle emissions from soil samples and processing to radionuclide activities)

Regards

Chris
Post 04 Oct 2006, 04:26
View user's profile Send private message Reply with quote
madmatt



Joined: 07 Oct 2003
Posts: 1045
Location: Michigan, USA
madmatt
Glad I could help. Smile Wow. Surprised Fasm used in a professional setting, I don't think I've ever heard of fasm being used in this way before. Hope it happens a lot more Laughing Cool
Post 04 Oct 2006, 07:57
View user's profile Send private message Reply with quote
f0dder



Joined: 19 Feb 2004
Posts: 3170
Location: Denmark
f0dder
WaitForSingleObject on [pr_info.hProcess] instead of polling GetExitCodeProcess... and never copy code from m32lib again.
Post 04 Oct 2006, 12:20
View user's profile Send private message Visit poster's website Reply with quote
ChrisLeslie



Joined: 04 Jun 2006
Posts: 50
Location: Australia
ChrisLeslie
fOdder

1) The reason that the code resembles m32lib is that my project started using MASM. Then for various reasons I switched to FASM which required translation of the existing application code. The reuseable common procedures, which I put in a seperate include file, were all re-started in FASM from scratch except for about five of them which got translated from m32lib. I make no appologies for that.

2) Can you explain why you suggest WaitForSingleObject over GetExitCodeProcess?

Regards

Chris
Post 04 Oct 2006, 22:18
View user's profile Send private message Reply with quote
f0dder



Joined: 19 Feb 2004
Posts: 3170
Location: Denmark
f0dder
#1: be careful with anything that originates from the m32lib, since it's poorly "designed" and full of sub-optimal and bugged crap.

#2: because it's more optimal. When using the WAIT functions, your thread is moved off the ready-list and thus takes up 0% CPU processing time. No Sleep() hacks are needed (and they are needed in the GECP polling, unless you want to suffer abysmal performance). Also, of academic fun value, consider a program that does ExitProcess with the same value as STILL_ACTIVE Smile

_________________
Image - carpe noctem
Post 04 Oct 2006, 23:16
View user's profile Send private message Visit poster's website Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid
to M32LIB:
- DF is not preserved upon calls (memcopy, lcase)
- functions often access memory which they are not told to access, possibly causing GPF (memfill, strlen)
- functions not working for boundary values (revstr)
- leaks handles (filesize, imalloc)
- doesn't check if arguments are within bounds (locate)
- doesn't check for (not-obvious) errors returned by API calls (locate)
- improperly uses COM objects (imalloc)
- some procedures doesn't work at all, under any circumstances always crashes. this means they weren't even runned (stripx)

in fact, i found bug in almost every function i looked at (in 9 of 11). And the others, without bug, were at least badly designed, like allowing overflow etc.

i was looking at it here: http://www.cecs.csulb.edu/~hill/cecs325/MASM32/M32LIB/, probably there is newer version, but even realeasing something of this quality says a lot about author(s).
Post 05 Oct 2006, 07:31
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
f0dder



Joined: 19 Feb 2004
Posts: 3170
Location: Denmark
f0dder
vid: you ought to take a look at most recent version, at least some bugs have been fixed - although a lot won't, because hutch is a stubborn idiot Smile
Post 05 Oct 2006, 08:20
View user's profile Send private message Visit poster's website Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid
gimme direct link please
Post 05 Oct 2006, 09:41
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
f0dder



Joined: 19 Feb 2004
Posts: 3170
Location: Denmark
f0dder
I'll give you a link to a "legacy version" as he calls it - which means a winrar sfx so you don't have to deal with his crappy installer. (Which is the reason he keeps a "legacy" version anyway, since for some reason he can't figure out how to make an installer than works on both 9x and NT). Also notice how he depends on other people to host his files? </rant>
Post 05 Oct 2006, 09:58
View user's profile Send private message Visit poster's website Reply with quote
comrade



Joined: 16 Jun 2003
Posts: 1137
Location: Russian Federation
comrade
jokes
Post 07 Oct 2006, 15:58
View user's profile Send private message Visit poster's website AIM Address Yahoo Messenger MSN Messenger ICQ Number Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2020, Tomasz Grysztar. Also on GitHub, YouTube, Twitter.

Website powered by rwasa.