12

In WPF and WinRT it is well known that behaviors do not call their Detach method reliably. The only way to avoid a memory leak is to subscribe to the Unloaded event of the AssociatedObject and unhook all events (example).

So far it works.

But I realized (as did someone else who commented on the blog entry I linked) that in this way unloaded behaviors are never attached again. Take a MenuFlyout for instance. If there is a behavior attached to a MenuFlyoutItem it is unloaded when the MenuFlyoutItem is closed. If you open the Menu again it won't be reattached.

This also happen with other UserControls. I "lose" behaviors when navigating in my WinRT App from one Page to another and back again although all Controls are recreated anew. I don't see how I can use them in a WinRT App where a lot of Controls are recreated by navigating around.

Is there any solution known to this?

Amenti
  • 1,511
  • 1
  • 13
  • 29
  • Maybe I'm missing something, but if you're detaching on Unload, wouldn't you just have to also attach on Load? – McGarnagle Jul 08 '14 at 20:23
  • @McGarnagle Thanks for your comment. Since my behaviors are created in Xaml I have no control of the time they are attached. Holding a reference to attach them manually at a later time again could in itself be the cause of a memory leak. Futher I'm not sure how I would do this short of creating a new behavior in code. – Amenti Jul 08 '14 at 20:28
  • It seems to me you have to distinguish between two cases. First, where your objects, once unloaded, are kept in memory and re-used when they are next needed. Second, where unloaded objects are discarded, and new ones created later. In the first case, you need to keep listening to the "Loaded" event. In the second case, doing so would prevent the old objects from being GC'd (memory leak), so you should detach from the "Loaded" event. – McGarnagle Jul 08 '14 at 21:05
  • Has there been a solution to this? I have the exact same problem where navigation doesn't detach behaviors. Any known workarounds?? – Maximus Nov 16 '15 at 14:16
  • I'm working in a UWP app and have the same problem where my behaviors don't detach and when navigating back, they don't work anymore. Any solution to this ? – Maximus Nov 18 '15 at 18:32
  • 5
    @MassimoCacchiotti, the fix is simple - wait for another 10 days for the new **Behaviors SDK for UWP** to be released. :) Currently the issue is with the older version (8.1). In the new version, the `Detach` function will be called properly (yes, I've tested it). – Justin XL Nov 20 '15 at 12:19
  • 1
    That's Fantiastic news, thanks @Justin XL !! :) – Maximus Nov 20 '15 at 14:13
  • @Justin XL, I forgot to ask you, how do you know when they will release a new version? Do you have a link to a blog or site that specifes when the update is coming? thanks again! – Maximus Nov 23 '15 at 17:07
  • 1
    https://twitter.com/unnir/status/667005259730948096 I got the pre-release sdk from them. :) Sorry I cannot share it but if you want to test it now, maybe ask Unni to send you the pre-release link. – Justin XL Nov 23 '15 at 19:43
  • Great, thanks Justin!! :) – Maximus Nov 23 '15 at 20:06

1 Answers1

4

A big thanks to @Justin XL, Microsoft has updated the Behaviors SDK for UWP and made it open source. The new libraries fix the navigation detach problems! :)

http://blogs.windows.com/buildingapps/2015/11/30/xaml-behaviors-open-source-and-on-uwp/

https://twitter.com/hashtag/BehaviorsGoOpenSource?src=hash

Maximus
  • 1,441
  • 14
  • 38