Skip to content

How do I free objects after the script execution? #455

Answered by MKostitsyn
MKostitsyn asked this question in Q&A
Discussion options

You must be logged in to vote

The following code solves my issue. I hope it will be useful for someone else.
fPyDelphiObjects - is a TDictionary<string, PPyObject>, where I keep all the P4D objects, that were created during script execution.
So this code is kinda my own garbage collector.

I would also appreciate to receive any comments on this method, if it has mistakes or vulnerables.

procedure TDeepyPythonModule.ReleasePyObjects;
begin
  var aScript := TStringList.Create;
  try
    var aGlobals := GetPythonEngine.EvalString('globals()');
    var aKeys := GetPythonEngine.PyDict_Keys(aGlobals);
    try
      for var I := 0 to GetPythonEngine.PySequence_Length(aKeys) - 1 do
      begin
        var aKey := GetPythonEngi…

Replies: 7 comments 4 replies

Comment options

You must be logged in to vote
1 reply
@MKostitsyn
Comment options

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by MKostitsyn
Comment options

You must be logged in to vote
1 reply
@MKostitsyn
Comment options

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
1 reply
@MKostitsyn
Comment options

Comment options

You must be logged in to vote
1 reply
@MKostitsyn
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants