Jump to content
Sign in to follow this  
J. Robroeks

Memory leak when freeing initialized PythonDelphiVar

Recommended Posts

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 by J. Robroeks

Share this post


Link to post

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 by J. Robroeks
  • Like 1

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×