pyscripter / python4delphi

Free components that wrap up Python into Delphi and Lazarus (FPC)
MIT License
906 stars 310 forks source link

Python4Delphi in MultiThreads Memory Leaks #404

Closed Uefi1 closed 1 year ago

Uefi1 commented 1 year ago

hello on your resource that you suggested to me https://en.delphipraxis.net/topic/8189-python4delphi-memory-leaks/?do=findComment&comment=69149 no one can help me, so I decided to write here. I'm having huge memory leaks when using a component in threads here is the whole code i am using below :

program Project1;

{$APPTYPE CONSOLE}

uses
  SysUtils,
  Classes,
  System.syncobjs,
  PythonEngine;

type
th = class(TPythonThread)
protected
  procedure Execute; Override;
public
  constructor create(createsuspended:boolean);
end;

var
cs:TcriticalSection;

{ th }

constructor th.create(createsuspended: boolean);
begin
inherited Create(CreateSuspended);
//ThreadExecMode  := emNewInterpreter;
FreeOnTerminate:=True;
end;

procedure th.Execute;
var
val:string;
PythonEngine1:TPythonEngine;
begin
inherited;
while true do begin
  PythonEngine1:=TPythonEngine.Create(nil);
  PythonEngine1.InitThreads:=True;
  PythonEngine1.AutoLoad:= False;
  PythonEngine1.AutoFinalize:=False;
  PythonEngine1.AutoUnload:=False;
  PythonEngine1.RedirectIO:=False;
  PythonEngine1.LoadDll;
  PythonEngine1.ExecString('fun=("Hello World")');
  val:=(PythonEngine1.EvalStringAsStr('fun'));
  PythonEngine1.UnloadDll;
  FreeAndNil(PythonEngine1); // or .Free
cs.Enter;
Write(Format(#13'VALUE=%s',[val]));
cs.Leave;
if not Terminated then
Terminate;
DoTerminate;
end;
end;

var
i:integer;
begin
//ReportMemoryLeaksOnShutdown:=True;
cs:=TcriticalSection.Create;
for i:=0 to 1 do
th.create(false);
readln;
end.

Checked on the newest component update !

pyscripter commented 1 year ago

This a support request and not an issue report.

Uefi1 commented 1 year ago

This a support request and not an issue report.

I don't understand, do you intend to fix the memory leak in your component or not?