WoWInterface (
-   Lua/XML Help (
-   -   Calling SetText on a FontString inside OnUpdate (

Mayron 02-07-19 05:18 AM

Calling SetText on a FontString inside OnUpdate
I am calling a function every 0.05 seconds in an OnUpdate script and it has this line in it:

Lua Code:
  1. timeRemaining:SetText(math.random());

This causes a memory leak issue where my AddOn's memory will keep on increasing persistently (not a lot but it builds up and up forever...).

If SetText is always a newly created string but the value is the same then this problem does not occur. Also, I can create many new strings in the OnUpdate function but that does not seem to be a problem. It only occurs when used with a widget to update the UI.

Is there a better memory optimized way of handling this?

Mayron 02-07-19 05:35 AM

I worked out that, for each unique string, it must store this in memory. So, if it is a random number (using math.random), and converted to a string, then the addon memory will perpetually increase, but if you're tracking down an aura's time remaining, for example, then it stores a range of strings between 1 and the maximum aura time remaining value (i.e. the total duration).

So, if an aura has a duration of 1 minute, and you're displaying the seconds, then it stores 1, 2, 3 ... 59, 60 as strings in addOn memory meaning it keeps increasing UNTIL it can reuse a string.

The weird thing is that it only seems to do this when using SetText. If I just create these strings as a local variable then after the function is executed the memory is freed. It even does this if I assign the value to a shared variable outside of the function.

All times are GMT -6. The time now is 10:33 AM.

vBulletin © 2020, Jelsoft Enterprises Ltd
© 2004 - 2020 MMOUI