TextBlock.GetPositionFromPoint raises NullReferenceException

The TextBlock.GetPositionFromPoint raises a NullReferenceException in some cases. This method should return a TextPointer to the text that starts at the given position. In some cases it generates a NullReferenceException instead, which is clearly a bug. The internals of this method looks like:

public TextPointer GetPositionFromPoint(Point point, bool snapToText)
{
  if (this.CheckFlags(Flags.ContentChangeInProgress))
    throw new InvalidOperationException(SR.Get("TextContainerChangingReentrancyInvalid"));

  if (((ITextView)this._complexContent.TextView).Validate(point))
    return (TextPointer) this._complexContent.TextView.GetTextPositionFromPoint(point, snapToText);

  if (snapToText)
    return new TextPointer((TextPointer) this._complexContent.TextContainer.Start);

  return null;
}

The call fails in the TextBlock.GetPositionFromPoint according to the call-stack. The most obvious error is that the _complexContent field isn’t set. After some research, I found out that most methods that rely on this field call TextBlock.EnsureComplexContent() first. This has been neglected in the TextBlock.GetPositionFromPoint method. We cannot call TextBlock.EnsureComplexContent from usercode, because it is a private method. You can get the ContentStart or ContentEnd properties, which will call TextBlock.EnsureComplexContent.

...
var dummy = this.myTextBox.ContentStart;   // WORKAROUND
TextPointer start = this.myTextBox.GetPositionFromPoint(selection.TopLeft, true);
...

Conclusion
This bug should be fixed by Microsoft by adding a call to EnsureComplexContent before it accesses the _complexContent field. It’s fairly easy to prevent the exception, but it might have took you some time to find out about the error and get to this blog.

Response from Microsoft
I have submitted this bug to Microsoft and it is filed under feedback item #478575. Microsoft confirmed it is a bug and it will be fixed when .NET Framework 4 will hit RTM (the current beta still has the bug).

Sample application
Download the sample application.

ScrollViewer always handles the mousewheel

The ScrollViewer control is a convenient control, when the content doesn’t fit on screen and you need some kind of scrolling to make all content accessible. I tend to use the mousewheel to scroll up and down, but this sometimes doesn’t work in WPF applications. It fails when you nest multiple scrollviewers, because the deepest ScrollViewer will always handle the MouseWheel message. Even if there is no vertical scrollbar visible or when it is disabled.

You can see this behaviour when you paste the following code in XamlPad. When you scroll down using the mousewheel and you hit the white area, then it is impossible to scroll further using the mousewheel.

<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" Height="400">
 <ScrollViewer VerticalScrollBarVisibility="Auto">
  <StackPanel>
    <Border BorderBrush="Black" BorderThickness="1" Width="400" Height="200" Background="Red"/>
    <ScrollViewer VerticalScrollBarVisibility="Disabled">
      <Border BorderBrush="Black" BorderThickness="1" Width="400" Height="200" Background="White"/>
    </ScrollViewer>
    <Border BorderBrush="Black" BorderThickness="1" Width="400" Height="200" Background="Blue"/>
  </StackPanel>
</ScrollViewer>
</Page>

It’s a bad idea to have two vertical scrollbars that are nested, but sometimes you use a control that has a ScrollViewer inside its template. Because you have wrapped the control in your own ScrollViewer, the scrollbars will never be visible. But because it does handle the MouseWheel messages, the mousewheel will stop scrolling when the mouse hovers over it. This is very annoying to the end-user.

I used .NET Reflector to check how the MouseWheel message was handled. It looks like this:

protected override void OnMouseWheel(MouseWheelEventArgs e)
{
  if (!e.Handled && this.HandlesMouseWheelScrolling)
  {
    if (this.ScrollInfo != null)
    {
      if (e.Delta < 0)
        this.ScrollInfo.MouseWheelDown();
      else
        this.ScrollInfo.MouseWheelUp();
    }
    e.Handled = true;
  }
}

As you can see, it’s quite easy to prevent the MouseWheel message from being processed and that is to set the HandlesMouseWheelScrolling to false, when the vertical scrollbar is hidden or invisible. Unfortunately, Microsoft decided to mark this property as internal, so it isn’t accessible. This property is used exclusively by the TextBox control (it uses a ScrollViewer too), that disables the handling of the MouseWheel message of the internal ScrollViewer. The result is that a TextBox doesn’t block vertical scrolling when using the mousewheel.

The problem can also be solved a different way, but this requires is us to subclass the ScrollViewer:

public class ExtScrollViewer : ScrollViewer
{
  private ScrollBar verticalScrollbar;

  public override void OnApplyTemplate()
  {
    // Call base class
    base.OnApplyTemplate();

    // Obtain the vertical scrollbar
    this.verticalScrollbar = this.GetTemplateChild("PART_VerticalScrollBar") as ScrollBar;
  }

  protected override void OnMouseWheel(MouseWheelEventArgs e)
  {
    // Only handle this message if the vertical scrollbar is in use
    if ((this.verticalScrollbar != null) && (this.verticalScrollbar.Visibility == Visibility.Visible) && this.verticalScrollbar.IsEnabled)
    {
      // Perform default handling
      base.OnMouseWheel(e);
    }
  }
}

The new MouseWheel handling doesn’t do anything when the vertical scrollbar is either invisible or disabled. The drawback of this approach is that you need to use a custom ScrollViewer, instead of the default ScrollViewer. You can do this pretty easy for your own controls, but if you have third party controls that you don’t control, then this won’t work.

Sebastien Lambla describes a method on his blog that allows you to change the characteristics of the default ScrollViewer. Use whatever you like best.

I have submitted this bug with Microsoft. You can track it here.

Properties and data binding

Every WPF/Silverlight programmer that writes it’s own business objects or custom controls either uses dependency properties or implements the INotifyPropertyChanged interface. Both have their own advantages and disadvantages:

Dependency properties:

  • Full support for databinding (read and write).
  • Only accessible from the thread that created the object.
  • WPF/Silverlight dependant, so requires additional assemblies at runtime and requires .NET 3.0 (or later).
  • Support for attached dependency properties, so you can extend other DependencyObject derived types with your class’ properties.
  • Some additional plumbing necessary, which is less trivial then normal .NET properties (i.e. respond to property updated).

Normal .NET properties (with INotifyPropertyChanged support):

  • Only suitable as a source in WPF databinding.
  • Properties can be read and write from an arbitrary thread (of course, you need some thread synchronization to avoid problems).
  • Not related to a specific .NET technology, so it can be used in any architecture.
  • Additional plumbing necessary to fire the PropertyChanged event.

You might wonder which kind of property you need to use. You can use the following guidelines:

  • If you expect to use to instantiate the class in XAML, then stick to dependency properties. This makes databinding a lot easier.
  • If you expect to use the class also to be used outside the WPF/Silverlight environment, then .NET properties are the way to go.

Normal .NET properties can be written in a very convenient way, like:

public string FullName { get; set; }

But, if you want to support INotifyPropertyChanged, then you need at least the following code:

private string _fullName;
public string FullName
{
 get { return this._fullName; }
 set
 {
  if (this._fullName != value)
  {
   this._fullName = value;
   if (this.PropertyChanged != null)
     this.PropertyChanged(this, new PropertyChangedEventArgs("FullName"));
  }
 }
}

As you can see, the INotifyPropertyChanged support adds a lot of code to each property. Even worse… It is prone to errors, because you need to specify the property name as a string. These kind of properties are often copy/pasted, so beware of mistakes (it might raise issues when you are refactoring the code). I created a Visual Studio macro that generates this plumbing for me to avoid these mistakes. But still… It’s a lot of work and a good programmer avoids repeating tasks.

It would be nice if MS added a special .NET attribute or keyword to C# to generate the plumbing automatically at compile time. Unfortunately, there is no such keyword and it hasn’t been announced for the upcoming C# 4.0 release.

Update Controls
Recently, I listened to a podcast from Polymorphic Podcasts that had the appealing title “Update Controls may render INotifyPropertyChanged obsolete”. More information can be found on CodePlex. Although sceptical, I listened to the podcast and decided to take a deeper look into this technology.

The update controls is a library written by Michael L. Perry and it uses a completely different approach to track property changes. It keeps track of the inter property dependencies, so it knows which properties have to be updated when a property is changed. It uses a quite sophisticated algorithm to determine the dependencies, but this is all completely transparent to the programmer.

Unfortunately, this library also needs additional plumbing in your properties, which is even more then when implementing IPropertyChanged:

private string _fullName;
private Dependent _indFullName = new Dependent();

public string FullName
{
 get { this._indFullName.OnGet(); return this._fullName; }
 set { this._indFullName.OnSet(); this._fullName = value; }
}

The dependencies are tracked during the execution of the OnGet and OnSet methods. There is also a XAML markup extension that also contains the required plumbing for dependency tracking. The major advantage of this approach above the INotifyPropertyChanged and regular WPF depenency properties is that dependant properties are automatically updated.

Suppose the following example:

class Person
{
 public string FirstName { get; set; }
 public string LastName { get; set; }
 public string FullName { get { return this.FirstName + " " + this.LastName; } }
}

When you are using dependency properties or implement INotifyPropertyChanged then you need some kind of logic that also updates the fullname, when you change the first and/or lastname. When using the update controls, this is done automatically. The update controls know about it, because it sees that you call the FirstName and LastName properties inside the FullName getter. Quite clever…

As always, there is a catch… You need to go through the special XAML markup extension to make sure the value is updated. Normal WPF applications would use the following binding:

<TextBox Text="{Binding FirstName}"/>
<TextBox Text="{Binding LastName}"/>
<Label Text="{Binding FullName}"/>

When using updatecontrols, this doesn’t work. Although everything compiles and runs just fine, the FullName is never updated. To WPF it is an ordinary property, so it expects a PropertyChanged event and it will never receive it. When using the update controls you need to use the special XAML markup extension, like this:

<TextBox Text="{uc:Update FirstName}"/>
<TextBox Text="{uc:Update LastName}"/>
<Label Text="{uc:Update FullName}"/>

It seems like a minor change, but you need to know in your XAML code if your underlying object uses update controls or if it uses the old-style dependency properties or INotifyPropertyChanged. I consider this a major disadvantage and I have decided to stick with the WPF dependency properties and the INotifyPropertyChange interface.

The update controls might be very valuable, when you’re using a lot of properties that depend on eachother. In those special cases it might be convenient to have the automatic update functionality.

Using PostSharp to implement INotifyPropertyChanged
PostSharp is a tool that can be used to inject code. It is extensible and when you combine PostSharp with PropFu, then you can generate the PropertyChanged automatically.

[NotifyPropertyChanged]
public class Person : INotifyPropertyChanged
{
 public string Name { get; set; }
 public event PropertyChangedEventHandler PropertyChanged;

 [OnPropertyChanged]
 private void OnPropertyChanged(string propertyName)
 {
  if (this.PropertyChanged != null)
   this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
 }
}

When you mark the class with the NotifyPropertyChanged attribute, then it will raise the PropertyChanged event for all public properties. You need to implement a method that will actually raise the property and mark it with the OnPropertyChanged attribute. After running PostSharp the class will be emitted like this:

[NotifyPropertyChanged]
public class Person : INotifyPropertyChanged
{
 private string _name;
 public string Name
 {
  get { return this._name; }
  set
  {
   if (this._name != value)
   {
    this._name = value;
    this.OnPropertyChanged("Name");
  }
 }
 public event PropertyChangedEventHandler PropertyChanged;

 [OnPropertyChanged]
 private void OnPropertyChanged(string propertyName)
 {
  if (this.PropertyChanged != null)
   this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
 }
}

When you install PostSharp it will update your Visual Studio configuration, so it will automatically invoke PostSharp at the end of each build. Of course, you can disable this and call PostSharp yourself.

I think this is a fairly elegant solution. PostSharp has a lot of additional functionality, so I suggest to take a further look at it.

Memory leak with WPF resources (in rare cases)

It seems there is bug in the WPF resource architecture that can cause memory not to be released and causing performance and stability problems. The document describes when the problem occurs and why.

During the development of a WPF application, we noticed that a huge amount of memory was used after a while. After profiling with the excellent .NET Memory Profiler v3.1 we found out that a lot of objects were not collected by the garbage collector. After some heavy investigation I found out that a DropShadowEffect was holding references to objects that should have been garbage collected already.

When does the problem occur?
The problem occurs in the following situation:

  1. A style is defined in the application’s ResourceDictionary.
  2. The style uses a control template that uses media effects (i.e. DropShadowEffect).
  3. The media effect should be referenced using a StaticResource.

Why does the problem occur?
When a media effect is attached to a UIElement, then the UIElement.VisualEffectInternal property setter is invoked by the WPF framework. This method registers to the Effect.Changed event when the media effect is not frozen. This is correct, because a non-frozen effect might change its properties and the UI element should invalidate. A frozen effect cannot be changed, so there is no need to register to the Effect.Changed event.

This behavior is correct and also documented in the MSDN library in the chapter Optimizing Performance: Object Behavior:

Changed Handlers on Unfrozen Freezables may Keep Objects Alive
The delegate that an object passes to a Freezable object’s Changed event is effectively a reference to that object. Therefore, Changed event handlers can keep objects alive longer than expected. When performing clean up of an object that has registered to listen to a Freezable object’s Changed event, it is essential to remove that delegate before releasing the object.

WPF also hooks up Changed events internally. For example, all dependency properties which take Freezable as a value will listen to Changed events automatically.

Unfortunately, there seems to be no code in the WPF framework that removes the delegate again. The only way to unsubscribe is to set the Effect to NULL. Using media effects that are not frozen seems to be dangerous.

As long as the media effect is frozen, then the framework doesn’t subscribe to the Changed event and everything is fine. But that’s the problem… When using a merged resource dictionary at the application level, then the resources inside of it are not always frozen (see the example application). If the resource is marked explicitly to be frozen, then it will be frozen and everything works fine.

What kind of puzzled me, is the fact that when the effect and style are defined directly at the “Application.Resources” level (without a resource dictionary), then the effect is frozen by default. If they are defined in a resource dictionary that is merged inside the “Application.Resources” then the effect is not frozen! You can force the effect to be frozen by specifying “PresentationOptions:Freeze=True”, but this is not common behavior. So, putting resources into resource dictionaries has side effects that are not foreseen and are not (clearly) documented in the WPF documentation.

Because the media effect is shared and the delegates are not removed when the UI element is not needed anymore it keeps the UI elements alive. WPF holds a strong hierarchy in objects, so a lot of other objects stay in memory too, resulting in a lot of wasted memory that can never be regained anymore.

The actual problem is that effects that are defined in a resource dictionary that is loaded from the Application object are not frozen in some cases. I haven’t done a thorough investigation why this doesn’t happen, but I think this is a bug in the WPF framework. The framework has a lot of places where it checks if a resource is of a certain type and if it is, then it should be sealed or frozen. I wouldn’t be surprised that the framework misses such a check for media effects, because they have been added later (in v3.5 SP1). Of course, this statement is pure speculation.

The workaround is simple… Just add the Freeze attribute to all the effects that you don’t plan to modify at runtime.

Sample application
I have created a sample application that illustrates the problem. When the application is started, then you can see a checkbox that has a dropshadow effect applied to it. The window also shows whether or not this effect is frozen.

As you can see the effect is not frozen, when the effect and style are defined in the resource dictionary. If you comment out the resource dictionaries from App.xaml and uncomment the effect and style resources in App.xaml, then the application looks just the same, but you’ll see that the effect is now frozen.

The application doesn’t illustrate the actual memory leak, but in a real-life application (like ours) it actually has memory leaks that can become quite large.

Conclusion
I guess Microsoft introduced a problem when using effect and styles in resource dictionaries. Because I didn’t investigate why it wasn’t frozen, I cannot determine whether this is a real bug or just a side-effect of some design choice.

I also understand that it might be a bad idea to define resources at the application level this way. Using themes is probably a better solution and might solve this issue. But, there are just people out there using this approach and they don’t expect this behavior. The fix is simple… Just freeze all media effects that you don’t plan to change at runtime, which helps us to fix this for our next release.

Sample application
Download the sample application.

Memory leak with WPF animations

It seems there is bug in the WPF animation architecture that can cause memory not to be released and causing performance and stability problems. The document describes when the problem occurs and why.

During the development of a WPF application, we noticed that a huge amount of memory was used after a while. After profiling with the excellent .NET Memory Profiler v3.1 we found out that a lot of objects were not collected by the garbage collector.

After some heavy investigation I found out that a FrameworkTemplate was holding references to a lot of my textboxes that should have been collected already. These textboxes held references to other objects, so the result was that a lot of memory was wasted. When the event triggers with actions were removed from the textbox’s control template, then the memory was released properly.

When does the problem occur?
The problem occurs in the following situation:

  1. The control has a control template defined.
  2. The control template uses triggers with enter/leave actions.
  3. The control has the ‘Collapsed’ visibility.

Why does the problem occur?
Triggers are active during the initialization of a control. If a property triggers an enter/leave action, then the action is started. The action is started via the internal “StyleHelper.InvokeActions” method. This method checks if the visual tree of the object is already constructed and if it is, then the actions are invoked immediately.

If the visual tree hasn’t been created yet, then the action is queued by the “StyleHelper.DeferActions”, so it can be processed later. To my surprise, the deferred actions are stored with the template and not the control itself (IMHO: This is a fundamental design flaw). If the visual tree is created, then the deferred actions will be invoked.

The visual tree is created during the public “FrameworkElement.ApplyTemplate” method. The framework only calls this method when measuring the control. So if the control is never measured, then the visual tree is not created and the deferred actions will remain queued forever.

The MSDN documentation for “FrameworkElement.MeasureOverride” clearly states:

Important Note:
Elements should call Measure on each child during this process, otherwise the child elements will not be correctly sized or arranged.

You might expect that all elements call “FrameworkElement.Measure” on their children, so during this process the “FrameworkElement.ApplyTemplate” is called. However, WPF doesn’t respect this rule, because “UIElement.Measure” doesn’t start measuring when the element has a collapsed visibility.

Collapsed UI elements are never measured and if they never will get the Visible or Hidden visibility, then the object is never measured and the template isn’t applied. This seems very logical, but it also causes the deferred actions never to be removed from the template.

Because the template keeps the reference to the control, the control is never garbage collected.  WPF controls know their parents and children. They also reference other objects via databinding. The result is that a lot of objects are referenced by the control (direct or indirect) and are not garbage collected.

Sample application
I have created a sample application that illustrates the problem. When the application is started, then you can see the deferred actions for the control template of the MySampleControl.

When you press the “Create Collapsed Expander”, the application creates a window that contains a collapsed expander that contains a MySampleControl. Because the MySampleControl is collapsed it isn’t constructed and you can see the control is added to the deferred actions list (after refreshing the list).

When you expand the expander on the window and refresh the deferred actions, then you can see that the control is removed from the list. If you close the window, without expanding the expander, then the control will stay on the list forever. Because the item is still on the list it is never collected.

You can force a garbage collection and in the debug window (when running in Visual Studio) you can see which objects are finalized. Windows with expanders that have never been opened will never be finalized.

Conclusion
I guess Microsoft made a design error by storing the deferred actions with the template. They should have been stored in the control itself. The deferred actions are only used when applying the template, which is an instance method of the control. So it would have had full access. Now a dictionary is used that connects objects that shouldn’t be connected.

Response from Microsoft
I have received a response from Microsoft and they confirm it is a bug. It is on the list to be fixed, but there is no releasedate for .NET 3.5 SP2 or .NET 4. So I guess we’ll have to wait.

Microsoft .NET Framework 4
Beta 1 of the Microsoft .NET Framework 4 has been released and it now uses a ConditionalWeakTable instead of a HybridDictionary. The keys are kept as weak references and when the key is collected by the GC, then the associated value is also released. This also solves this issue in our situation, because the dictionary doesn’t keep the objects alive anymore.

Sample application
Download the sample application.