1

In one of my wp8 apps I am using BindingReflector to enable binding on converter parameters. One issue I ran into was is that any converter that is above (In XAML) the converter that uses binding on the parameter does not get GC'd at all. I noticed the page that was using this specific ordering get more and more memory when navigating to it then back and repeat.

After so many attempts on trying to figure what's going on I decided to try and change their order in XAML making the one using the reflector at the top and the problem was solved. I was wondering if anyone could have any explanation on this.

When I was having this mem leak problem, the profiler, under GC Roots, was showing that my converters were all referencing (not directly) the converter using the reflector with a Handle (WeakRef). No clue how could that happen but now once they're bellow it everything is fine and getting GC'd properly.

Note the converters were all in siblings controls, the structure was basically a listbox like this:

<ListBox ItemsSource={Binding...}>
   <!-- ... item template like below -->
   <controlX propX={Binding xxx, converter=....}/>
   <controlY propY={Binding yyy, converter=....}/>
   ...
</ListBox>

EDIT: I have created and uploaded a simple test project which you can download the zip from here. Run the app, once MainPage loads click the button that takes you to Page1.xaml, hit the back button then click the button that takes you to Page1.xaml again and repeat this process.

I have included counters under the title of each page which show current memory usage, ofc you won't notice anything there but the next 3 counters are

  1. Current instance count of Page1 objects (yellow)
  2. Current instance count of the random converter objects (red)
  3. Current instance count of the converter using the binding reflector objects (green)

You will notice while yellow and green never go above 3, Red will keep going up as you go back and forth between MainPage.xaml and Page1.xaml.

Now, switch the position of the 2 button controls using the converters in Page1.xaml, just comment them out and uncomment the ones bellow them and you'll notice all 3 counters never going above 3.

EDIT 2: Apparently this isn't just about binding reflector. Having both buttons use the same converter will cause that specific converter instances not being GC'd. I don't know why this is happening..

3
  • The weak references don't prevent an object from being collected. So having those simply shows that the object hasn't been GC'd yet. Commented Mar 27, 2013 at 19:12
  • Also, I'd appreciate if you could somehow upload a small project that reproduces the issue. It's intriguing. Commented Mar 27, 2013 at 19:26
  • Interestingly, it seems the leak is related to the DataTemplate. Just putting the buttons in a datatemplate in the page's resources is enough to cause the leak, even though the datatemplate isn't used anywhere. But I have yet to understand what is the exact cause. Commented Mar 31, 2013 at 11:01

1 Answer 1

2

What I've found so far:

  • The leak is somehow tied to the DataTemplate. I'm unable to reproduce it if I put the controls outside of a DataTemplate. Also, the leak will happen even if the controls are declared in a unused DataTemplate in the page's resources
  • There's no GCRoot that can explain that the object isn't garbage collected. So the reference is probably held by some internal native object. I've already noticed a case where that could happen: http://blogs.codes-sources.com/kookiz/archive/2013/02/17/wpdev-memory-leak-with-bitmapimage.aspx
  • As you mentionned in the comments, the leak can be reproduced by having two buttons referencing the same converter. If the two buttons reference a separate instance of the same converter type, the leak won't happen. Therefore, I guess the Silverlight runtime somehow messes up its reference counting when initializing a DataTemplate.

The workaround:

I have the guts feeling that the leak comes from referencing in a DataTemplate an object that has been declared from outside the scope of the template (in your case, the converter, but it also reminds me of that similar leak involving an attached property). While searching a way to declare the converter in the same scope, I managed to find a workaround:

Sit down, breath deeply. Open the leaky converter's class (in your case, RandomConverter). Make it inherit form FrameworkElement. Leak fixed.

... Don't ask.

This workaround actually is a nice lead to find out where the memory allocation problem lies. I'll try digging further in the next few days, but don't expect too much.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.