J. Robroeks 1 Posted March 11, 2021 (edited) Hi, The following code has a memory leak (73-88 bytes: Unknown x 1). Why is that? PySUBJECT := TPythonDelphiVar.Create(nil); try PySUBJECT.Engine := self.PythonEngine1; PySUBJECT.VarName := 'SUBJECT'; PySUBJECT.Initialize; finally PySUBJECT.Free; end; The memory leak does not occure when setting the owner to the pythonengine. Moreover, not freeing the var and setting the owner to the pythonengine will just consume memory until the pythonengine if freed. TParallel.For(1, 10, procedure(I: Integer) var PySUBJECT : TPythonDelphiVar; Py : IPyEngineAndGIL; begin Py := TPyEngineAndGIL.Create; PySUBJECT := TPythonDelphiVar.Create(nil); try PySUBJECT.Engine := Py.GetPyEngine; PySUBJECT.VarName := 'SUBJECT'; PySUBJECT.Module := TGuid.NewGUID.ToString; PySUBJECT.Initialize; PySUBJECT.Value := randomvalue; Py.GetPyEngine.ExecModule := PySUBJECT.Module; Py.GetPyEngine.ExecStrings(script); finally PySUBJECT.Free; end; end); Thanks! Edited March 11, 2021 by J. Robroeks Share this post Link to post
J. Robroeks 1 Posted March 11, 2021 (edited) The var has to be finalized before freeing the object: PySUBJECT := TPythonDelphiVar.Create(nil); try PySUBJECT.Engine := self.PythonEngine1; PySUBJECT.VarName := 'SUBJECT'; PySUBJECT.Initialize; finally PySUBJECT.Finalize; PySUBJECT.Free; end; Edited March 11, 2021 by J. Robroeks 1 Share this post Link to post