[.NET Framework] C1FlexGrid with FlexGridFilter causes memory leak?

Posted by: daniel.schoenbach on 10 July 2024, 8:55 am EST

  • Posted 10 July 2024, 8:55 am EST - Updated 10 July 2024, 9:00 am EST

    Hi,

    I’m experiencing a memory leak that I believe is caused by using a C1FlexGrid with a FlexGridFilter (and/or FullTextFilter). There seems to be a static Dictionary containing a reference to the C1FlexGrid even after its containing window has been closed.

    I’ve attached a minimal reproduction here:

    FlexGridFilterLeak.zip

    Thanks

  • Posted 10 July 2024, 5:58 pm EST

    Hi Daniel,

    We could see the behavior you mentioned. We shared the observations with the development team to get their insights on it and will let you know the updates as soon as possible.

    [Internal Tracking ID: C1XAML-36607]

    Kind Regards,

    Kartik

  • Posted 15 July 2024, 5:12 pm EST

    Hi Daniel,

    The development team researched the behavior and discovered that a dictionary object remains even after closing the window. Currently, there is no way to clear this dictionary internally. To fix this behavior, the development team suggests clearing the Owner property of the C1FlexGridFilter when the window is closed.

    Please refer to the code snippet and the updated sample project below for reference:

    public partial class FlexGridWindow : Window
    {
        public FlexGridWindow()
        {
            InitializeComponent();
        }
    
        protected override void OnClosing(CancelEventArgs e)
        {
            C1FlexGridFilterService.GetFlexGridFilter(flexGrid).Owner = null;
    
            base.OnClosing(e);
        }
    }

    Attachment: FlexGridFilterLeak_updated.zip

    Best Regards,

    Kartik

  • Posted 16 July 2024, 6:07 am EST

    Thanks for acknowledging the issue and suggesting a workaround. I’ve confirmed the workaround works on the sample. I’ll see if I can apply the workaround on my real project.

  • Posted 19 July 2024, 8:49 am EST - Updated 19 July 2024, 8:55 am EST

    I’m having difficulty applying the workaround to my real project. I tried setting the filter when the FlexGrid is loaded and clearing the filter when the FlexGrid is unloaded. Unfortunately, this triggers a StackOverflowException.

            private void C1FlexGrid_Loaded(object sender, RoutedEventArgs e)
            {
                C1FlexGridFilterService.SetFlexGridFilter(c1FlexGrid, C1FlexGridFilter);
                C1FlexGridFilterService.SetFullTextFilterBehavior(c1FlexGrid, C1FullTextFilter);
            }
    
            private void C1FlexGrid_Unloaded(object sender, RoutedEventArgs e)
            {
                C1FlexGridFilterService.SetFlexGridFilter(c1FlexGrid, null);
                C1FlexGridFilterService.SetFullTextFilterBehavior(c1FlexGrid, null);
            }

    FlexGridFilterStackOverflow.zip

  • Posted 21 July 2024, 5:20 pm EST

    Hi Daniel,

    Apologies for the delay in response, and thank you for providing the sample project. We observed the exception you mentioned, but we are unclear about the need for this implementation in this sample. The earlier sample you shared demonstrated the memory leak issue, and the developers provided a workaround. However, we did not observe the memory leak issue in the current sample project, making it unclear if this workaround is necessary.

    Could you please provide a sample project that accurately reflects your real application’s implementation and reproduces the memory leak issue? This will help us update the sample project with the appropriate workaround to assist you further.

    Thank you for your patience and understanding.

    Best Regards,

    Kartik

  • Posted 22 July 2024, 2:08 am EST

    Hi Kartik,

    Thanks for the reply. The issue is that without the Loaded/Unloaded events, there is a memory leak. But with the Loaded/Unloaded events, there is a stack overflow.

    I’ve combined the sample projects into a new one that demonstrates the issue. As written, it demonstrates the memory leak when the windows containing the FlexGrid are closed. You can uncomment lines 46-47 of Tab1.xaml.cs to fix the memory leak, but doing so will trigger a Stack Overflow Exception when switching between tabs.

    // Tab1.xaml.cs:
    
            private void C1FlexGrid_Unloaded(object sender, RoutedEventArgs e)
            {
                // Uncomment to fix memory leak
                // However, switching tabs causes StackOverflow Exception.
    
                // C1FlexGridFilterService.SetFlexGridFilter(c1FlexGrid, null);
                // C1FlexGridFilterService.SetFullTextFilterBehavior(c1FlexGrid, null);
            }

    I am using the Loaded/Unloaded events because the FlexGrid is in a UserControl, not hosted in the Window directly. Hence, I do not have access to the OnClosing method you suggested as a workaround.

    However, I recently realized I might be able to use the Window.GetWindow() method from within the UserControl to register an event handler on Window.Closed. This would let me apply the workaround even from a nested UserControl. I will let you know if this approach is successful on the real application.

    FlexGridFilterLeak2.zip

  • Posted 22 July 2024, 3:05 am EST

    This approach seems to be working in my real application. Thanks for all your help and good luck with the bugs.

  • Posted 22 July 2024, 8:55 pm EST

    Hi Daniel,

    Thank you for sharing all the details and the updated sample project. After investigating, we observed that the best workaround is the one you implemented in your last response. We are glad to hear that the issue is now resolved on your end.

    Best Regards,

    Kartik

Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels