Page 1 of 1

Hidden mem leak?

Posted: Fri May 04, 2012 5:16 pm
by jesperbrannmark
I am trying this code
And while doing this the memory on the process in task manager is not increasing.
BUT if i look in the tab "performance" I see that the memory is rapidly being eaten by something (but no process is taking up the memory).
When I kill the process of the compiled program, the memory gets released.
Anyone know how this could be?
(i just got a out of memory message from windows, thats why i started looking at it)

Code: Select all

; http://www.purearea.net/pb/CodeArchiv/Windows_System/Tasks&Processes/KillProcess.pb

; English forum: http://www.purebasic.fr/english/viewtopic.php?t=8086&start=15
; Author: Hi-Toro
; Date : 30. November 2003
; OS : Windows
; Demo : No

; ------------------------ Get PID
Structure PROCESSENTRY32s
  dwsize.l
  cntusage.l
  th32ProcessID.l
  th32DefaultHeapID.l
  th32ModuleID.l
  cntThreads.l
  th32ParentProcessID.l
  pcPriClassBase.l
  dwFlags.l
  szExeFile.s{1024}
EndStructure
#TH32CS_SNAPPROCESS = $2

Procedure.l FindPid(s.s)
  Process.PROCESSENTRY32s
  ProcSnap.l
  ProcSnap = CreateToolhelp32Snapshot_(#TH32CS_SNAPPROCESS, 0)
  If ProcSnap <> 0
    Process\dwsize=SizeOf(Process)
    Process32First_(ProcSnap, Process)
    While Process32Next_(ProcSnap, Process) > 0
      If Process\szExeFile = s
        ProcedureReturn Process\th32ProcessID
        Break
      EndIf
    Wend
  EndIf
EndProcedure
; --------------------- Get PID end

; Take a look at ListTaskbarWindows.pb before...
; ... and if a program refuses to close like that, you could instead 'force' it closed
; via this, passing the 'pid' variable from the above code : 

#PROCESS_TERMINATE = $1
#PROCESS_CREATE_THREAD = $2
#PROCESS_VM_OPERATION = $8
#PROCESS_VM_READ = $10
#PROCESS_VM_WRITE = $20
#PROCESS_DUP_HANDLE = $40
#PROCESS_CREATE_PROCESS = $80
#PROCESS_SET_QUOTA = $100
#PROCESS_SET_INFORMATION = $200
#PROCESS_QUERY_INFORMATION = $400
#PROCESS_ALL_ACCESS = #STANDARD_RIGHTS_REQUIRED|#SYNCHRONIZE|$FFF

; This appears to be pretty much how Windows kills a program if you 'End Process'
; from the Task Manager. Note that this is 'unfriendly'!

Procedure KillProcess(pid)
  phandle = OpenProcess_(#PROCESS_TERMINATE, #False, pid)
  If phandle <> #Null
    If TerminateProcess_(phandle, 1)
      result = #True
    EndIf
    CloseHandle_(phandle)
  EndIf
  ProcedureReturn result
EndProcedure
Repeat
  If Not FindPid("PureBasic.exe")
    End
  EndIf
ForEver

Re: Hidden mem leak?

Posted: Fri May 04, 2012 6:15 pm
by Fred
You seem to miss a CloseHandle(ProcSnap) in your FindPid() routine. And remove all the '.l' it will fail on x64 ;)