Recent

Author Topic: why KillThread cause memory leak ?  (Read 14708 times)

mohsenti

  • Jr. Member
  • **
  • Posts: 58
Re: why KillThread cause memory leak ?
« Reply #30 on: January 08, 2017, 03:35:04 pm »
Hi,

@marcov, Can you explain me how to implement multi process application ?

I know about pipe lines and messages passing between processes but to implement multi process application with one executable file,I must override some functions of TApplication like runLoop and ... Is this true ?!

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11383
  • FPC developer.
Re: why KillThread cause memory leak ?
« Reply #31 on: January 08, 2017, 04:24:48 pm »
You don't really divide the application into multiple ones, you just use a very simple guardian process that calls things that can potentially hang or die (like activex objects like excel or adobe reader) that you can easily so that crashes at least don't crash the main app.

In our case, we used simple shared memory that contained all memory to initialize the new read into Excel, and in a different case (an ISAPI dll that tended to die because of connection with a dodgy activex object) we started a service with a all crucial state of  the ISAPI dll, and the isapi DLL would use and update it.

If it would die, the service would simply respawn the process that would use the state from shared memory to (mostly) continue where it died.

As said, I don't have any code anymore, I can only recommend shared memory as a IPC method because that way it is easy to implement a state machine and pass simple data.

ASerge

  • Hero Member
  • *****
  • Posts: 2222
Re: why KillThread cause memory leak ?
« Reply #32 on: January 10, 2017, 02:38:44 am »
note : That codes works fine in delphi 7.0+.
I have D7. Because original code NOT compiled under D7, i changed it to:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.   I: Integer;
  4. begin
  5.   for I := 0 to 3 do
  6.   begin
  7.     tt := TTestThread.Create(True);
  8.     tt.FreeOnTerminate := False;
  9.     tt.Resume;
  10.     TerminateThread(tt.Handle, 0);
  11.     tt.Free;
  12.     Sleep(1000);
  13.   end;
  14. end;
  15.  
  16. { TTestThread }
  17.  
  18. procedure TTestThread.Execute;
  19. begin
  20.   Sleep(2000);
  21. end;
  22.  
  23. constructor TTestThread.Create(CreateSuspended: boolean);
  24. begin
  25.   inherited;
  26.   p := TPicture.Create;
  27. end;
  28.  
  29. destructor TTestThread.Destroy;
  30. begin
  31.   OutputDebugString('TTestThread.Destroy');
  32.   p.Free;
  33.   inherited Destroy;
  34. end;

So, sometimes threads not started between Resume and Terminate, so it's free normal by code tt.Free. Sometimes started and... application HUNG! It's not "works fine" :).

 

TinyPortal © 2005-2018