{"action":"opened","issue":{"url":"https://api.github.com/repos/albertodemichelis/squirrel/issues/50","repository_url":"https://api.github.com/repos/albertodemichelis/squirrel","labels_url":"https://api.github.com/repos/albertodemichelis/squirrel/issues/50/labels{/name}","comments_url":"https://api.github.com/repos/albertodemichelis/squirrel/issues/50/comments","events_url":"https://api.github.com/repos/albertodemichelis/squirrel/issues/50/events","html_url":"https://github.com/albertodemichelis/squirrel/issues/50","id":155146732,"number":50,"title":"duplicate local variables hide prior values without unreferencing","user":{"login":"zeromus","id":102701,"avatar_url":"https://avatars.githubusercontent.com/u/102701?v=3","gravatar_id":"","url":"https://api.github.com/users/zeromus","html_url":"https://github.com/zeromus","followers_url":"https://api.github.com/users/zeromus/followers","following_url":"https://api.github.com/users/zeromus/following{/other_user}","gists_url":"https://api.github.com/users/zeromus/gists{/gist_id}","starred_url":"https://api.github.com/users/zeromus/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/zeromus/subscriptions","organizations_url":"https://api.github.com/users/zeromus/orgs","repos_url":"https://api.github.com/users/zeromus/repos","events_url":"https://api.github.com/users/zeromus/events{/privacy}","received_events_url":"https://api.github.com/users/zeromus/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":0,"created_at":"2016-05-16T23:42:19Z","updated_at":"2016-05-16T23:42:19Z","closed_at":null,"body":"consider this:\r\n```\r\nlocal test = mynativeclass();\r\nlocal test = mynativeclass();\r\n```\r\nAssume mynativeclass has a release hook. The release hook won't be called during this code. It will be called when the local scope ends. That's just what I noticed. Really what's going on is the old local is hidden and there's no way to get access to it anymore but it stays referenced in the function's locals.\r\n\r\nThis causes a problem in case there is a scarce resource associated with the `mynativeclass`. The scripter will mistakenly reuse a variable name, and the script may work just fine, but resources will be leaked (temporarily) unexpectedly. \r\n\r\nI think declaring a 2nd local variable with the same name should be an error. That's easy to implement, but I would understand if you don't like that design. However, it should be implemented so that the same variable slot is reused; or at least so that the previous slot is nulled.\r\n\r\nIMO The best possible implementation would null the variable before evaluating the new expression, so no more than one resource is created at a time, as well as reuse the variable slot to keep the stack leaner.  Here's code which might do this, or it might be horribly bugged.. just an experiment:\r\n\r\n```\r\n        //LocalDeclStatement(), non-function codepath\r\n        do {\r\n            varname = Expect(TK_IDENTIFIER);\r\n            //null the existing variable that's getting shadowed\r\n            if(_fs->GetLocalVariable(varname) != -1) \r\n            {\r\n                 SQInteger tmp = _fs->PushTarget();\r\n                 _fs->AddInstruction(_OP_LOADNULLS, tmp,1);\r\n                 _fs->AddInstruction(_OP_MOVE, _fs->GetLocalVariable(varname), tmp);\r\n                 _fs->PopTarget();\r\n            }\r\n            if(_token == _SC('=')) {\r\n                Lex(); Expression();\r\n                SQInteger src = _fs->PopTarget();\r\n\r\n                //try to reuse an existing variable slot\r\n                SQInteger dest = _fs->GetLocalVariable(varname);\r\n                if(dest == -1)\r\n                {\r\n                    addLocal = true;\r\n                    dest = _fs->PushTarget();\r\n                }\r\n                if(dest != src) _fs->AddInstruction(_OP_MOVE, dest, src);\r\n            }\r\n            else{\r\n                _fs->AddInstruction(_OP_LOADNULLS, _fs->PushTarget(),1);\r\n            }\r\n            \r\n            if(addLocal) { _fs->PopTarget();_fs->PushLocalVariable(varname); }\r\n            if(_token == _SC(',')) Lex(); else break;\r\n        } while(1);\r\n```\r\n\r\nQuestion: what happens when a local shadows a function argument? Any different treatment?"}}