DataTemplate is causing a memory leak when using event handlers in the code behind in UWP. Does anyone know why?

Mbrown_21 1 Reputation point
2020-12-09T17:10:14.527+00:00

I have a Page that has a Grid with a GridView. The GridViews ItemTemplate is set to a StaticResource DataTemplate. Within the DataTemplate I have several Bindings to data type properties but I also hook the DoubleTapped and RightTapped events from the DataTemplate Grid control to handlers that live in the code behind of the page. This set up causes the page to never get GC'd. This is not unique to Gridview, I've also seen this with Listview and Navigationview.

If I remove those events from the DataTemplate OR put the handler methods directly in the data type object itself, GC works fine and the page gets picked up. I also found that if I just use a data template selector class rather than setting the data template directly, the page gets picked up by GC!

Any idea why hooking those events up to handlers living in the code behind would cause this page to never get GC'd? Is this possibly some red herring and the problem is likely elsewhere?

I'm apparently not able to provide any code for reference as every time I do I just get the message: You're not allowed access on this server. Hopefully the description above will suffice.

Universal Windows Platform (UWP)
{count} votes