Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to delete a dictionary?

Asked by 4 years ago

I read that dictionaries in lua are never garbage collected. Currently I am just going through and clearing every existing key of a dictionary through the pairs iterator when I'm done using it to prevent memory leaks, but that isn't very efficient. Is there a way to just set a dictionary to nil without having to manually clear every key/value pair?

0
Are you talking about tables like this{} firestarroblox123 440 — 4y
0
Yeah. A dictionary is where you store values by strings instead of by index. For example: foo = {["A"] = "Apple", ["B"] = "Baseball", ["C"] = "Cat"} aquathorn321 858 — 4y
0
And are you sure you cant just use foo = nil foo = {} firestarroblox123 440 — 4y
0
If you do that it just changes where the variable foo is pointing to. As far as i'm aware dictionaries aren't garbage collected, so removing all references from the dictionary wouldn't delete it from memory. aquathorn321 858 — 4y
View all comments (5 more)
0
Could you use a faster loop like for i = 1,#foo do local v = foo[i] end firestarroblox123 440 — 4y
0
No, because the values are stored under keys. The only way to iterate by key is to use the pairs iterator, unless I made another table that stored every key in the dictionary. I'm not sure if that's more efficient. aquathorn321 858 — 4y
0
Then im 90% sure there is no better way, roblox needs to fix this. firestarroblox123 440 — 4y
0
It's more of a lua problem. There's no 'clear' method in the table library as it stands. I read to make one yourself you have to make it in C, and I don't think that's possible in roblox. aquathorn321 858 — 4y
0
Oh. firestarroblox123 440 — 4y

Answer this question