OnEvent using constant resources

Hello,

Creating an addon page and the addon memory usage keeps increasing by 2k until it performs automatic garbage collection.

What am I doing wrong, if anything ?

local AddonName, Addon = ...

local C_VignetteInfo = C_VignetteInfo;
local GetVignetteInfo = C_VignetteInfo.GetVignetteInfo;
local GetVignettes = C_VignetteInfo.GetVignettes

local man = {}
local msgFrame = msgFrame or CreateFrame("FRAME", nil, UIParent, "BackdropTemplate")


function Addon:MJBBlah(msg, k)
    man.msgFrame = msgFrame
    msgFrame:SetWidth(300)
    msgFrame:SetHeight(14 * k + 20)
    msgFrame:SetPoint("TOPLEFT", UIParent, "TOPLEFT", 50, -300)
    msgFrame:SetFrameStrata("TOOLTIP")
    msgFrame:SetBackdrop({
        bgFile = "Interface/BUTTONS/WHITE8X8",
        edgeFile = "Interface/Tooltips/UI-Tooltip-Border",
        tile = true, tileSize = 32, edgeSize = 16,
        insets = { left = 2, right = 2, top = 2, bottom = 2 }
    })
    msgFrame:SetBackdropColor(0.1,0.1,0.1, 1)
    msgFrame.text = msgFrame.text or msgFrame:CreateFontString(nil, "OVERLAY", "GameFontHighlight")

    msgFrame.text:SetPoint("TOP", 0,-10)
    msgFrame.text:SetJustifyH("LEFT")
    msgFrame.text:SetText(msg)
end

Addon:MJBBlah("Why is this eating resources?!",2)


local EL = EL or CreateFrame("Frame", "", UIParent);

EL:RegisterEvent("UPDATE_UI_WIDGET");
EL:RegisterEvent("VIGNETTES_UPDATED");
EL:RegisterEvent("VIGNETTE_MINIMAP_UPDATED");
EL:RegisterEvent("PLAYER_ENTERING_WORLD");
EL:RegisterEvent("ZONE_CHANGED");
EL:RegisterEvent("ZONE_CHANGED_NEW_AREA");

local as = "";
local text = text or UIParent:CreateFontString(nil, "OVERLAY", "GameFontHighlight")

EL:SetScript("OnEvent",function(self,event, ...)
    Addon:MJBBlah(event,2)
end)

This is a simple code just to show my issue. This was what I used to “debug”…
If I comment out the EL:SetScript, memory is not constantly used. But as soon as I add it, memory usage increased incrementally.
Even with Addon:MJBBlah(event,2) commented out, memory increases.

Sigh
Is that just a part of the OnEvent / SetScript functions ?

Thanks,
Mike

theres slightly better ways of writing that but its fine as is.

MJBBlah doesnt just update the text it resets the frame attributes as well. its a bit overkill and uses up more resources but its not a big deal.

if you really wanted you could break that into two functions, one to setup the frame, the other to just update the text, then call that second function in the onevent script. that would use a little less resources.

unless theres an issue that its causing with game play dont worry too much about how much resources the addon is using.

Thank you.

I “thought” I have to do the entire function because the height of the frame is set based on a counter of items and can vary depending on what is found.

Just for knowledge, I can create the frame one time, and the second function in the OnEvent script, could I reset the height or just doesn’t matter and more memory will be used anyway?

As they dont change, all these things can be done once, right after the frame is created

local man = {}
local msgFrame = CreateFrame("FRAME", nil, UIParent, "BackdropTemplate")
man.msgFrame = msgFrame
msgFrame:SetWidth(300)
msgFrame:SetPoint("TOPLEFT", UIParent, "TOPLEFT", 50, -300)
msgFrame:SetFrameStrata("TOOLTIP")
msgFrame:SetBackdrop({
    bgFile = "Interface/BUTTONS/WHITE8X8",
    edgeFile = "Interface/Tooltips/UI-Tooltip-Border",
    tile = true, tileSize = 32, edgeSize = 16,
    insets = { left = 2, right = 2, top = 2, bottom = 2 }
})
msgFrame:SetBackdropColor(0.1,0.1,0.1, 1)
msgFrame.text = msgFrame.text or msgFrame:CreateFontString(nil, "OVERLAY", "GameFontHighlight")
msgFrame.text:SetPoint("TOP", 0,-10)
msgFrame.text:SetJustifyH("LEFT")

which leaves just doing the

msgFrame:SetHeight(14 * k + 20)
msgFrame.text:SetText(msg)

each OnEvent.

In the example, k is always 2 so

msgFrame:SetHeight(14 * k + 20)

Is never actually changing the height, but maybe because this is just pcode.

1 Like

Correct, in my example with one line, just to show the function. In the real code, the k value is the number of lines in the text string.