Quantcast Will this be Garbage Collected? - WoWInterface
Thread Tools Display Modes
11-22-15, 01:00 PM   #1
A Fallenroot Satyr
Spyro's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2011
Posts: 20
Will this be Garbage Collected?

Hi guyz.

I have made a function to execute code in the next frame, mainly for getting correct GetLeft(), GetRight(), GetPoint() values after changing the anchor point of a Frame (because the data that those functions return is not updated until the next frame):
Lua Code:
  1. -- Frame to execute code in the next frame
  2. local _RunNextFrame = CreateFrame("Frame") -- Frame whose OnUpdate will execute the functions in the next frame
  3. _RunNextFrame:Hide() -- OnUpdate disabled by default
  5. -- OnUpdate script which iterates thru all the functions stored in the frame to execute them
  6. _RunNextFrame:SetScript("OnUpdate", function(self)
  7.   for i = 1, #self do
  8.     self[i]() -- Executing function
  9.     self[i] = nil -- Removing the reference to the function (to be garbage collected)
  10.   end
  11.   self:Hide() -- Disabling OnUpdate
  12. end)
  14. -- RunNextFrame()
  15. -- Executes a function in the next frame.
  16. local function RunNextFrame(Function)
  17.   _RunNextFrame[#_RunNextFrame + 1] = Function -- Storing a reference to the function in the frame
  18.   _RunNextFrame:Show() -- Enabling the OnUpdate of the frame
  19. end
  21. -- Example application
  22. -- Getting the distance from the left edge of the screen to the right edge of the PlayerFrame after changing its anchor point
  23. local PlayerFrameRight
  25. PlayerFrame:ClearAllPoints()
  26. PlayerFrame:SetPoint("CENTER", UIParent, "CENTER", -255, 20)
  28. RunNextFrame(function()
  29.   PlayerFrameRight = PlayerFrame:GetRight()
  30. end)
The question is: Will the anonymous function that I pass as argument to RunNextFrame() be garbage collected?
  Reply With Quote
11-22-15, 01:07 PM   #2
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
Yes, functions are garbage-collected. The only things that aren't are frames.

However, if your actual code is anything like your fake example code (seriously, why do you people post this crap? just post your real code ) then you should just assign your "anonymous" function to a variable so you can reuse it instead of creating endless duplicates of it, or better yet, don't bother caching the right position at all; if you actually need to determine the right position of a frame so often that just calling frame:GetRight() is slowing things down and caching offers a meaningful benefit, there's probably a more fundamental flaw with your program design.
Author/maintainer of Grid, PhanxChat, oUF_Phanx, and many more.
Troubleshoot an addonTurn any code into an addonMore addon resources
Need help with your code? Post all of your actual code! Attach or paste your files.
Please don’t PM me about addon bugs or code questions. Post a comment or forum thread instead!
  Reply With Quote
11-23-15, 02:26 PM   #3
A Pyroguard Emberseer
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 1,918
Relying on garbage collection is the last thing you should do in any circumstance. Memory expensive objects such as functions and tables can eat up a lot of memory if repeatedly created and left for garbage collection in a short amount of time. When garbage collection eventually does happen, it puts a heavy load on the CPU trying to free the associated memory of each object. Lua does do this in steps to try to keep the host program running stable, but it's still a CPU-intensive task nonetheless.
ESOUI AddOns | WoWInterface AddOns
"All I want is a pretty girl, a decent meal, and the right to shoot lightning at fools."
-Anders (Dragon Age: Origins - Awakening)
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » Will this be Garbage Collected?

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off