Copy link to clipboard
Copied
in my scripts i use the app.doScript method which allows me to have a single undo for the entire script.
In a persistent engine i noticed (using the $.summary) that every time a script is running, the functions count increase.
Even after a double $.gc() the memory is not released.
#targetengine 'test'
function test(v) {
return v + 1;
}
app.doScript(test);
$.gc();
$.gc();
alert($.summary());
there's a way to prevent this sort of memory leak?
Something like this seems to avoid the problem:
#targetengine test
var test;
!test && test = function(v) {
return v + 1;
}
app.doScript(test);
$.gc();
$.gc();
alert($.summary());
Copy link to clipboard
Copied
Hi Roberto,
did you test different undo modes?
https://www.indesignjs.de/extendscriptAPI/indesign-latest/#UndoModes.html
What's your version of InDesign and operating system?
Regards,
Uwe Laubender
( ACP )
Copy link to clipboard
Copied
Hi Uwe,
I'm on Macbook Pro, with High Sierra.
I tried all the 4 undo modes, in InDesign CC2019 and CC2020.
Same results. Functions count increase every time, garbage collector seems to fail to release the memory used, even with simple function that does not involve the InDesign DOM.
thank you, Roberto
Copy link to clipboard
Copied
I notice that if you do not use a custom target engine, and use Main instead, the function count doesn't increase.
Copy link to clipboard
Copied
i know.
But I have to use a persistent engine, loading tons of other functions, create menu, menuaction, etc.
Copy link to clipboard
Copied
Something like this seems to avoid the problem:
#targetengine test
var test;
!test && test = function(v) {
return v + 1;
}
app.doScript(test);
$.gc();
$.gc();
alert($.summary());
Copy link to clipboard
Copied
yes!! .... and (maybe) no..
I thought the problem was the app.doScript() but is the redeclaration of the test function.
But leaving the redeclaration and calling test() (without the app.doScript) the functions count does not increase. Very strange.
thank you, Roberto
Copy link to clipboard
Copied
When you run the script, a new function with the same name is defined.
With your call of doScript, the ExtendScript engine passes a reference to that function to InDesign.
Apparently InDesign never releases that reference, so the persistent target engine has to keep it around. For example other places where such references are passed are event handlers / listeners, and there you want to stick them around for long.
There are multiple solutions:
invoke the function by name, passing a script code instead of the function object.
app.doScript("test()");
Define the function once, and in later invokations refer to the existing function.