Page 1 of 1

[Done] - Increased consuming of handles in Threadsafe mode

Posted: Mon Jun 21, 2010 1:14 pm
by rlow
Hi,

I noticed an increasing number of handles in taskmanager of my application when using the threadsafe mode. I striped down the code until I was able to locate the problematic line.

The number of handles is always increased by 1 after finishing a thread, if this thread is calling a procedure.
Here is some demo code to show the problem:

Code: Select all

Global finishThread.b = 0

; dummy
Procedure AProcedure()
EndProcedure

Procedure ThreadTest(*value)  
  Repeat   
    AProcedure() ; <== this call is causing the increasement of handles!
    Delay(10)     
  Until finishThread   
EndProcedure

OpenConsole()
t = CreateThread(@ThreadTest(), 1)
PrintN("Test-thread is started. Continue with Return.")
Input()

count = 0
Repeat 
  finishThread = 1
  WaitThread(t)
  PrintN("Thread is finished. Check number of handles now in taskmanager! Continue with Return.")
  Input()
  
  finishThread = 0
  t = CreateThread(@ThreadTest(), 1)
  PrintN("Thread is started again. Continue with Return to finish the thread.")
  Input()
  count = count + 1
  
Until count = 5

finishThread = 1
WaitThread(t)

PrintN("Thread Test finished.") 
Input()
Normally I would expect the number of handles before threadcreation are equal to the number after a the thread has finished if all resources are freeded correctly.
This only happens in threadsafe mode and leads to an increased consuming of handles for applications using threads.

Maybe someone else could confirm this behaviour? It should probably be considered as a PB bug.

System: PB 4.50 x86 - 32 bit WindowsXP

Best regards,
rlow

Re: Problem: Increased consuming of handles in Threadsafe mo

Posted: Mon Jun 21, 2010 2:41 pm
by cas
Same thing here on Windows 7 x64 & PB4.50 x86, but only if debugger is disabled. If i enable debugger then it doesn't increment.

Re: Problem: Increased consuming of handles in Threadsafe mo

Posted: Mon Jun 21, 2010 2:53 pm
by rlow
Addionally to the handle issue I noticed that there are always 2 new threads created instead of just 1. (threadsafe mode on, debugger off)

Re: Problem: Increased consuming of handles in Threadsafe mo

Posted: Mon Jun 21, 2010 3:18 pm
by cas
I quickly opened Process Explorer to see what is happening with handles and it looks like that there are thread handles still opened after thread ends execution :shock: .

Re: Problem: Increased consuming of handles in Threadsafe mo

Posted: Wed Jun 23, 2010 12:58 pm
by rlow
This demo code is showing the resource leakage more dramatically:

Code: Select all

Global count = 100, finishThread.b = 0

Procedure AProcedure()
EndProcedure

Procedure Test(*value)  
  Repeat   
    AProcedure() ; <== this call seems to increase the number of handles! (threadsafe mode on, debbugger off)
    Delay(1)     ;     without the procedure call there is no handle leak
  Until finishThread   
EndProcedure

OpenConsole()
PrintN("Thread test started. Check the number of handles in taskmanager now. Continue with return.")
Input()
PrintN("Creating and finishing a thread now. " + Str(count) +" times.")
PrintN("")

For i = 1 To count
  finishThread = 0
  t = CreateThread(@Test(), 0)
  Delay(100)
  finishThread = 1
  WaitThread(t)
Next

PrintN("Test finished. Check the number of handles in taskmanager again.")
PrintN("The number of used handles is arround " + Str(count) + " more than before!")
PrintN("(using threadsafe mode on, debugger off)")
Input()
Greets,
rlow

[Done]Problem: Increased consuming of handles in Threadsafe

Posted: Wed Jun 23, 2010 3:03 pm
by rlow