Here's something that's been bugging me. If I do
local a=1
local a
What happens? Is the second local a ignored? Does it create a new variable? It does not cause a crash and nothing "bad" seems to happen.
This sort of "mistake" can happen when the two "local a" statements are in the same block but separated by lots of code. The PIL seems to say nothing about this.
Comments
Seemly it is legal code, and what it do is create a new variable named a, and it hides (not delete) the old one.
if you do:
local a=1;
print(a);
local a;
print(a);
it will print:
1
nil
https://github.com/gideros/gideros
https://www.youtube.com/c/JohnBlackburn1975
I think the same as @speeder_kidoteca, This is a way to restart the local variable, I also got the same results as @speeder_kidoteca.
[-] Liasoft
they will be garbage collected once out of scope.
they are not immediately erased to don't break tables that have multiple names
ie:
if you do
local a=someOtherTable;
local a;
If we just deleted the first A outright, we might end accidentally deleting the "someOtherTable" thus a (the first) is only hidden.
In this case, it seems, no harm is done. There are two versions of "r" etc and both point to different tables in memory. When the function returns, both "r"'s will be destroyed but, since I have used addChild, references are held to these tables and they will (correctly) not be garbage collected.
So it seems no harm is done, but can you think of a case where this might result in a memory leak? Personally, I think this is an oddity of Lua. Redefinition should result in a run time error IMO.
Likes: hgvyas123
https://github.com/gideros/gideros
https://www.youtube.com/c/JohnBlackburn1975
Thanks for share
I wanted to reflect on what you have posted and I wrote the following code, for I am also looking for memory leak in my application
why? I don't know
Also I tried to see if changed some in global variables (before and after) with this code
But see this post
http://giderosmobile.com/forum/discussion/799/addingremoving-children-and-memory-usage/p1
and if you change above code
I think all this is summed up, as you administer local variables to avoid memory leaks, if you you can have them assigned "nil" to local variables defined before, then you can save Memory.
Likes: hgvyas123
[-] Liasoft
a={}
a[1]={1,2,3}
a[2]={4,5,6}
a[1]=nil
a[2]=nil
The tables {1,2,3} and (4,5,6} can now be garbage collected but a is still a table with entries "1" and "2". I don't think Lua deletes variables which are nil.
But your loop with a local in it worries me as well.
https://github.com/gideros/gideros
https://www.youtube.com/c/JohnBlackburn1975
Put this code to debug the local. When you define 2 b, you can see 2 b.
The holder is something else, not "b" itself.
:-?
http://giderosmobile.com/forum/discussion/comment/35572#Comment_35572
I extrapolated your example and I got the following
[-] Liasoft
In this site I've seen as you can print all local variables
http://stackoverflow.com/questions/2834579/print-all-local-variables-accessible-to-the-current-scope-in-lua
[-] Liasoft