-
- realesmedia
- Posts: 85
- Joined:
Cyclic reference when using child user control with DataContext binding on the Parent Control
Hi,
Noesis C++ 2.1.0f1 - 2.2.0b3
if use DataContext = "{Binding ElementName = window}" then when you destroy a window, mView.Reset () does not call WindowCaption () destructor and you cannot destroy Window () because of NumRefCounts > 1
Noesis C++ 2.1.0f1 - 2.2.0b3
Code: Select all
simplified XAML for Window:
<Window x:Class="TrackEditor"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Background="#303033" Height="300" Width="900"
Title="Track editor" x:Name="window" IsEnableWindowButtons="True">
<Grid>
<!--UserControl.-->
<WindowCaption Height="30" VerticalAlignment="Top" Background="#01000000" Margin="1,1,30,1" DataContext="{Binding ElementName=window}">
</WindowCaption>
</Grid>
</Window>
-
-
sfernandez
Site Admin
- Posts: 3268
- Joined:
Re: Cyclic reference when using child user control with DataContext binding on the Parent Control
Our implementation has that limitation. Storing a reference to an ancestor in the tree in a DependencyProperty will produce a leak.
There are ways to avoid this by adding some indirection, instead of setting the parent as datacontext, use a viewmodel which can provide the desired information to the children classes. You will have something like this:
Where WindowInfo is a property of the ViewModel set by your Window as DataContext.
Could that work for you?
There are ways to avoid this by adding some indirection, instead of setting the parent as datacontext, use a viewmodel which can provide the desired information to the children classes. You will have something like this:
Code: Select all
<Window x:Class="TrackEditor" ...>
...
<WindowCaption DataContext="{Binding WindowInfo}"
...
</Window>
Could that work for you?
-
- realesmedia
- Posts: 85
- Joined:
Re: Cyclic reference when using child user control with DataContext binding on the Parent Control
Hi
memory leak, in this situation, is not obvious.
Is it possible to emit a warning in this case?
thank
memory leak, in this situation, is not obvious.
Is it possible to emit a warning in this case?
thank
Re: Cyclic reference when using child user control with DataContext binding on the Parent Control
We are working in a solution that tries to break circular references when elements are disconnected from the view. I think this will cover 99% of the scenarios. Anyway, having the DataContext in the view model, as proposed by sfernandez, is probably a better solution.
Yes, in 2.2.0 you should be getting a log warning. Are you setting up that callback?
Yes, in 2.2.0 you should be getting a log warning. Are you setting up that callback?
-
- realesmedia
- Posts: 85
- Joined:
Re: Cyclic reference when using child user control with DataContext binding on the Parent Control
Yes, LogHandler is connected
Re: Cyclic reference when using child user control with DataContext binding on the Parent Control
Code: Select all
if (GetAllocatedMemory() > 0)
{
NS_LOG_WARNING("Memory leaks detected: %d", GetAllocatedMemory() );
}
Who is online
Users browsing this forum: Semrush [Bot] and 6 guests