Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dependency Property With Default Value Throwing StackOverflowException

I'm using the WPF SQL Connection User Control. I am having an issue with it throwing a StackOverflowException whenever I have it on a tab (AvalonDock DocumentTab) which has been opened, closed, and then opened a second time.

I've messed around with Jake's base implementation to better suit my application, but it essentially is the same. I've added a property which disables the selection of a database.

I've placed the control into my application like this:

<controls:SqlConnectionStringBuilder
       Grid.Row="2"
       Margin="0,10,0,0"
       ConnectionString="{Binding ElementName=listBoxClients,
                                  Path=SelectedItem.ConnectionString,
                                  UpdateSourceTrigger=PropertyChanged}"
       Header="Connection String"
       RequireDatabase="True" />

I've done some refactoring of the code-behind of the the SqlConnectionStringBuilder in order to troubleshoot this issue, but this appears to be the offending code:

public static readonly DependencyProperty ConnectionStringProperty =
    DependencyProperty.Register(
        "ConnectionString", 
        typeof(SqlConnectionString),
        typeof(SqlConnectionStringBuilder),
        new FrameworkPropertyMetadata(
            new SqlConnectionString { IntegratedSecurity = true, Pooling = false },
            FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));

public SqlConnectionString ConnectionString
{
    get { return (SqlConnectionString)GetValue(ConnectionStringProperty); }
    set { SetValue(ConnectionStringProperty, value); }
}

On the second open of the tab the SqlConnectionString object gets into an infinite loop with its OnPropertyChanged method and the IntegratedSecurity property. When I make the ConnectionString property not a DependencyProperty I do not get this issue. To me this says that the issue is with the default value of the dependency property.

I poked around online to see if anyone else has had this issue, but it seems like I may have gotten myself into a bit of a pickle. The only thing close that I can think that this issue may have come from is in regards to this SO question about dependency properties which was answered as thread safety. I'm not sure how dependency properties treat their default values, but I could see that if the same object was wired up twice the issue with OnPropertyChanged event. However this also leads me to believe that if this were the case this issue would have been noted somewhere!

Any thoughts?

Additional Information:
I removed the default value from the registration of the dependency property (set it to null). This prevents the issue from occurring. The only drawback to this solution is that the UI is in a null state, no default selections. I'd like to prevent that from being the case by solving the issue, though.

like image 444
Mike G Avatar asked Feb 24 '12 14:02

Mike G


2 Answers

Are you registering new dependency properties every time they are disposed of by the tab closing? I'm fairly sure that you cannot reuse the same dependency property registration if what it's referencing has been disposed. Once you close the tab the garbage collector will try to eat your "ConnectionString" object. It will dispose of all child variables when the tab loses scope, even if they're static readonly.

like image 189
Jeremiah Avatar answered Oct 26 '22 13:10

Jeremiah


In that infinite loop, who is making assignment to the IntegratedSecurity property in response to OnPropertyChanged? If you find out who that is, this is the key to your answer.

like image 20
Ivan Krivyakov Avatar answered Oct 26 '22 13:10

Ivan Krivyakov