How do I free objects after the script execution? #455
-
I have set of scripts that produce and use objects like this:
If I comment the last line, rb seems not to be released automatically. |
Beta Was this translation helpful? Give feedback.
Replies: 8 comments 4 replies
-
Run:
I don't know how to do it automatically |
Beta Was this translation helpful? Give feedback.
-
AFAIR |
Beta Was this translation helpful? Give feedback.
-
The following code solves my issue. I hope it will be useful for someone else. I would also appreciate to receive any comments on this method, if it has mistakes or vulnerables.
|
Beta Was this translation helpful? Give feedback.
-
here is how to get globals w/o calculating 'globals()'
|
Beta Was this translation helpful? Give feedback.
-
also: you call GetPythonEngine 10 times. cache it to var. |
Beta Was this translation helpful? Give feedback.
-
also: |
Beta Was this translation helpful? Give feedback.
-
You can change your code to the following so that you do not pollute the main module dictionary. def dowork():
from pc1 import Pc as pc
rb = pc.ReceiptBuilder()
# work with receipt builder
dowork() |
Beta Was this translation helpful? Give feedback.
-
If you want to clean up your created global variables, the best way is to use the following ExecString/EvalString overloads: procedure ExecString(const command: AnsiString; locals, globals: PPyObject; const FileName: string = '<string>'); overload;
function EvalString(const command: AnsiString; locals, globals: PPyObject; const FileName: string = '<string>'): PPyObject; overload; and provide a new locals dictionary (PyDict_New), which you destroy (Py_DECREF) after the calling the above functions. Note that if globals is nil, it becomes the same as locals. |
Beta Was this translation helpful? Give feedback.
If you want to clean up your created global variables, the best way is to use the following ExecString/EvalString overloads:
and provide a new locals dictionary (PyDict_New), which you destroy (Py_DECREF) after the calling the above functions.
Note that if globals is nil, it becomes the same as locals.