Gubaer / josm-scripting-plugin

Task automation in the OpenStreetMap editor JOSM
https://gubaer.github.io/josm-scripting-plugin
GNU General Public License v3.0
26 stars 9 forks source link

Global variables outlive their script #61

Closed SafwatHalaby closed 7 years ago

SafwatHalaby commented 7 years ago
var util = require("josm/util");
if (typeof cnt === 'undefined')
    cnt = 0;
else
    cnt ++;

util.print("CNT: " + cnt);

This will print 0, then 1, then 2, etc. Although this is usually harmless, I think it is effectively a memory leak when there are global variables pointing to huge objects or datasets.

It can also cause cross-script interference if two scripts are using the same global variable names.

If this is technically impossible to resolve due to some limitation in Mozilla Rhino or the underlying JOSM code, then the user should at least be warned in the documentation, and advised to always wrap the code in a (function() {user code here}) (); if dealing with huge globals.

Gubaer commented 7 years ago

Good point! At least a hint should be added to the documentation.

Gubaer commented 7 years ago

Added a section about variables in the global scope in the documentation