OmniThreadLibrary forum
News: SMF - Just Installed!
 
*
Welcome, Guest. Please login or register. May 17, 2012, 05:55:07 PM


Login with username, password and session length


Pages: [1]   Go Down

Author Topic: ThreadPool.CancelAll - Memory Leaks?  (Read 768 times)

Kryvich

  • Newbie
  • *
  • Posts: 11
    • View Profile
    • Email
ThreadPool.CancelAll - Memory Leaks?
« on: July 24, 2011, 09:18:05 AM »

I have memory leaks when close my application, when an user stopped the program with ThreadPool.CancelAll
The code:

Code: [Select]
procedure MyWorker(const task: IOmniTask);
begin
  Form1.Log(Format('Task started ID=%d Name=%s', [task.UniqueID, task.Name]));
  Sleep(2000);
end;

procedure TForm1.btnCreateClick(Sender: TObject);
var
  i: integer;
begin
  GlobalOmniThreadPool.MaxExecuting := 5;
  for i := 0 to 100 do begin
    CreateTask(MyWorker, 'Test task #' + IntToStr(i))
      .Unobserved
      .Schedule;
  end;
  Log('Tasks are sheduled...');
end;

procedure TForm1.btnCancelClick(Sender: TObject);
begin
  GlobalOmniThreadPool.CancelAll;
end;

In Test.dpr:
Code: [Select]
  ReportMemoryLeaksOnShutdown := DebugHook <> 0;

Press Create, Cancel, and close the window.
Logged

Kryvich

  • Newbie
  • *
  • Posts: 11
    • View Profile
    • Email
Re: ThreadPool.CancelAll - Memory Leaks?
« Reply #1 on: July 25, 2011, 07:49:40 AM »

I've found out that the problem doesn't concern neither ThreadPool.CancelAll nor ThreadPool.MaxExecuting. The leaks appear only when more than 80 tasks are sheduled. If 79 tasks are sheduled - the leaks disappear.
Logged

Primoz Gabrijelcic

  • Administrator
  • Hero Member
  • *****
  • Posts: 569
    • View Profile
    • Email
Re: ThreadPool.CancelAll - Memory Leaks?
« Reply #2 on: July 26, 2011, 02:18:50 AM »

Yes, there's a weird problem with Windows message queues which I cannot pin down. Usually it helps if you call Application.ProcessMessages between two CreateTasks.
Logged

Kryvich

  • Newbie
  • *
  • Posts: 11
    • View Profile
    • Email
Re: ThreadPool.CancelAll - Memory Leaks?
« Reply #3 on: July 26, 2011, 08:54:41 AM »

Perhaps, this issue concerns the order of threads termination? I've changed the code a little:
Code: [Select]
procedure MyWorker(const task: IOmniTask);
begin
  Form1.Log(Format('Task started ID=%d Name=%s', [task.UniqueID, task.Name]));
  Sleep(3000);
end;

procedure TForm1.btnCreateClick(Sender: TObject);
var
  i: integer;
begin
  GlobalOmniThreadPool.MaxExecuting := 1;
  for i := 1 to 51 do begin
    CreateTask(MyWorker, 'Test task #' + IntToStr(i))
      .MonitorWith(OmniEventMonitor)
      .Schedule;
  end;
  Log('Tasks are sheduled...');
end;

procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
  GlobalOmniThreadPool.CancelAll;
end;

Press Create button, then after a queuing of the tasks close the window.

If we create less than 51 tasks - there are no leaks. But if we create 51 tasks - there will be a leak of one instance of the TOmniContainerWindowsMessageObserver class. Tested on Windows 7 x64 and Windows XP.

Event Log fragment:
Code: [Select]
...
Debug Output: <<<Execute thread 02327650:5052 Process Test2.exe
Thread Exit: Thread ID: 5052. Process Test2.exe
Debug Output: Thread 02327650:4212 completed request :3 Process Test2.exe
Debug Output: Destroying :3 Process Test2.exe
Debug Output: Asy_TerminateWorkItem thread 02327650:4212 Process Test2.exe
Debug Output: Destroying thread 02327650:4212 Process Test2.exe
...

Is it normal that the worker's thread is terminated before a task thread is destroyed?
Logged

Primoz Gabrijelcic

  • Administrator
  • Hero Member
  • *****
  • Posts: 569
    • View Profile
    • Email
Re: ThreadPool.CancelAll - Memory Leaks?
« Reply #4 on: July 26, 2011, 10:05:11 AM »

Are you doing anything task-related in OnCloseQuery or OnClose?
Logged

Kryvich

  • Newbie
  • *
  • Posts: 11
    • View Profile
    • Email
Re: ThreadPool.CancelAll - Memory Leaks?
« Reply #5 on: July 26, 2011, 11:14:50 AM »

Sorry, my assumption was mistaken.
And no, nothing special in OnCloseQuery and OnClose. Sample application is attached.
Logged

Kryvich

  • Newbie
  • *
  • Posts: 11
    • View Profile
    • Email
Re: ThreadPool.CancelAll - Memory Leaks?
« Reply #6 on: July 27, 2011, 03:09:59 AM »

I give up. I'll write a replacement for the TOmniThreadPool, lightweight, without using of interfaces.
Logged

Primoz Gabrijelcic

  • Administrator
  • Hero Member
  • *****
  • Posts: 569
    • View Profile
    • Email
Re: ThreadPool.CancelAll - Memory Leaks?
« Reply #7 on: July 27, 2011, 03:17:02 AM »

I'm sorry I can't take a look at that immediately. I'll fix it as soon as possible.
Logged

Kryvich

  • Newbie
  • *
  • Posts: 11
    • View Profile
    • Email
Re: ThreadPool.CancelAll - Memory Leaks?
« Reply #8 on: July 27, 2011, 06:27:04 AM »

No problem at all, Primoz! I have created already my thread pool. It works great with the OtlTaskControls, though not so rapid as your TOmniThreadPool. Have tested on the multi-threaded web spider application - no issues.
Logged

Primoz Gabrijelcic

  • Administrator
  • Hero Member
  • *****
  • Posts: 569
    • View Profile
    • Email
Re: ThreadPool.CancelAll - Memory Leaks?
« Reply #9 on: July 27, 2011, 08:30:03 AM »

Create 1 + Cancel + Close: Works fine. (At least with the today's fix to OtlEventMonitor.pas.)
Create 2 + Cancel + Close: Lost memory. I'll look into it.
Logged

Kryvich

  • Newbie
  • *
  • Posts: 11
    • View Profile
    • Email
Re: ThreadPool.CancelAll - Memory Leaks?
« Reply #10 on: July 27, 2011, 11:14:27 AM »

Yes, Create1 creates 50 tasks - no leaks, while Create2 creates 51 tasks and produces one leak. I couldn't understand why. A multithreaded code is really hard to debug.
Logged

Kryvich

  • Newbie
  • *
  • Posts: 11
    • View Profile
    • Email
Re: ThreadPool.CancelAll - Memory Leaks?
« Reply #11 on: December 29, 2011, 04:12:59 PM »

The problem is resolved! See http://www.thedelphigeek.com/2011/12/how-to-find-missing-release.html for more details.
Logged
Pages: [1]   Go Up
 
 

Powered by MySQL Powered by PHP Powered by SMF 2.0.2 | SMF © 2006-2009, Simple Machines LLC

Valid XHTML 1.0! Valid CSS! Dilber MC Theme by HarzeM