WoWInterface

WoWInterface (https://www.wowinterface.com/forums/index.php)
-   Lua/XML Help (https://www.wowinterface.com/forums/forumdisplay.php?f=16)
-   -   Will this be Garbage Collected? (https://www.wowinterface.com/forums/showthread.php?t=52902)

Spyro 11-22-15 01:00 PM

Will this be Garbage Collected?
 
Hi guyz. :cool:

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
  4.  
  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)
  13.  
  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
  20.  
  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
  24.  
  25. PlayerFrame:ClearAllPoints()
  26. PlayerFrame:SetPoint("CENTER", UIParent, "CENTER", -255, 20)
  27.  
  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?

Phanx 11-22-15 01:07 PM

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 :mad:) 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.

SDPhantom 11-23-15 02:26 PM

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.


All times are GMT -6. The time now is 06:26 PM.

vBulletin © 2024, Jelsoft Enterprises Ltd
© 2004 - 2022 MMOUI