blish-hud / Blish-HUD

A Guild Wars 2 overlay with extreme extensibility through compiled modules.
https://blishhud.com
MIT License
311 stars 60 forks source link

Tooltip's need revised for GC #941

Open dlamkins opened 5 months ago

dlamkins commented 5 months ago

There is an issue with Tooltips that prevents them from being properly collected. Some initial investigation is discussed here: https://discord.com/channels/531175899588984842/599270434642460753/1208556624902492250

ynzenu commented 5 months ago

The problem is indeed the _allTooltips property on the Tooltip class. The solution could be as simple as removing the tooltip from the collection when it's disposed.

This is the solution I came up with for the Mystic Crafting module:

public class DisposableTooltip : Tooltip
   {
       public DisposableTooltip(ITooltipView tooltipView) : base(tooltipView) { }

       private void RemoveFromCollection()
       {
           var type = typeof(Tooltip);
           var field = type.GetField("_allTooltips", BindingFlags.NonPublic | BindingFlags.Static);

           if (field == null)
               return;

           var tooltips = (ControlCollection<Tooltip>)field.GetValue(null);

           if (tooltips == null)
               return;

           tooltips.Remove(this);
       }

       protected override void DisposeControl()
       {
           RemoveFromCollection();

           base.DisposeControl();
       }
   }