View Single Post
08-08-19, 12:27 AM   #1
Lybrial
A Flamescale Wyrmkin
AddOn Compiler - Click to view compilations
Join Date: Jan 2010
Posts: 120
Frame:Show() increases memory usage on each toggle?

Hi Guys,

I wanted to implement a grid toggle, I started with the vertical lines and tried it out.
Here the code, it is quite simple:

Lua Code:
  1. LybrialAnchors = LybrialUI:NewModule("Lybrial Anchors");
  2.  
  3. local LybrialAnchors = LybrialAnchors;
  4.  
  5. LybrialAnchors.grid = nil;
  6.  
  7. -- Initialize Addon
  8. function LybrialAnchors:OnInitialize()
  9. end
  10.  
  11. -- Get Options
  12. function LybrialAnchors:GetOptions()
  13.     return {
  14.         toggleGrid = {
  15.             type = "toggle",
  16.             order = 1,
  17.             name = "Toggle Grid",
  18.             set = function(_, value)
  19.                 self:ToggleGrid(value);
  20.             end,
  21.             width = "full"
  22.         },
  23.     };
  24. end
  25.  
  26. function LybrialAnchors:ToggleGrid(toggle)
  27.     if (toggle) then
  28.         if (not self.grid) then
  29.             print("Create Grid");
  30.  
  31.             self:CreateGrid();
  32.         else
  33.             print("Show Grid");
  34.  
  35.             self.grid:Show();
  36.         end
  37.     else
  38.         print("Hide Grid");
  39.  
  40.         self.grid:Hide();
  41.     end
  42. end
  43.  
  44. function LybrialAnchors:CreateGrid()
  45.     if (not self.grid) then
  46.         self.grid = CreateFrame("Frame", "Grid", UIParent);
  47.         self.grid:SetFrameStrata("BACKGROUND");
  48.     end
  49.  
  50.     local steps = 128;
  51.     local width, height = UIParent:GetSize();
  52.     local widthStep = (width / steps);
  53.  
  54.     self.grid:SetPoint("CENTER", UIParent);
  55.     self.grid:SetSize(width, height);
  56.     self.grid:Show();
  57.  
  58.     self:CreateVerticalLines(steps, widthStep);
  59. end
  60.  
  61. function LybrialAnchors:CreateVerticalLines(steps, widthStep)
  62.     for i = 0, steps do
  63.         local region = self.grid:CreateTexture();
  64.         region:SetColorTexture(0, 0, 0);
  65.         region:SetDrawLayer("BACKGROUND", 0);
  66.         region:ClearAllPoints();
  67.         region:SetPoint("TOPLEFT", self.grid, "TOPLEFT", (i * widthStep), 0);
  68.         region:SetPoint("BOTTOMRIGHT", self.grid, "BOTTOMLEFT", (i * widthStep), 0);
  69.     end
  70. end

Let me click the toggle input 5 times. The output is:

Code:
Create Grid
Hide Grid
Show Grid
Hide Grid
Show Grid
So as you can see the first time I enable the toggle the grid is getting created. After that the grid frame
is only showing or hiding. So it should work exactly as I want it to work.

But the problem is: On each `self.grid:Show()` the memory usage of my addon is increasing.
I toggled 50 times and my addon memory usage was increased by 25 MB.

I dont understand why a `Show()` on an already existing `Frame` is increasing memory usage.
Anything anyone can tell me about that?
  Reply With Quote