Herdo / AdaptiveTriggerLibrary

A library for adaptive triggers for Windows universal apps (Universal Windows Platform).
MIT License
63 stars 7 forks source link

AdaptiveTriggers that set TargetElement in code-behind must be nulled otherwise they cause a memory leak #3

Closed JSmyth886 closed 8 years ago

JSmyth886 commented 8 years ago

I've been having an issue with memory leaks in my UWP app and noticed that quite a few of the AdaptiveTriggers I was using in my applications were being help onto in memory. After some investigation I found that nulling the TargetElement when the view is Unloaded the memory was freed.

Below is an example fix to your MainPage.xaml.cs

    private void OnUnloaded(object sender, RoutedEventArgs routedEventArgs) {
      HorizontalRectangleFillTrigger.TargetElement = null;
      VerticalRectangleFillTrigger.TargetElement = null;
      Unloaded -= OnUnloaded;
    }

*Edit Not necessarily an issue, just something I wanted to flag to you and anyone else using your AdaptiveTriggerLibrary. Maybe it's something you could apply to your Wiki.

Herdo commented 8 years ago

@JSmyth886 Thanks for the notice.

The setters of the TargetElement properties attach to certain events in order to update the trigger state depending on the underlying FrameworkElement. Attaching to the Loaded and Unloaded events of those FrameworkElements won't solve this, sadly. So your note should be taken into consideration when using those specific triggers.

Gonna put that info somewhere useful.

Herdo commented 8 years ago

Created this wiki page: https://github.com/Herdo/AdaptiveTriggerLibrary/wiki/Usage-of-triggers-relying-on-a-TargetElement