Memory Leak in WPF ListBox with ListBoxItems in Navigation Windows
- Dominant language
- C#
- Stars
- 7.7k
- Forks
- 1.3k
- Avg merge
- 1d 11h
- Merged PRs (30d)
- 61
Description
### Description
When using a ListBox control with ListBoxItem in a WPF Navigation Window, opening and closing the window multiple times leads to memory leaks. The ListBox and its related objects are not released from memory, as confirmed using the .NET Memory Profiler.
### Reproduction Steps
1. Create a WPF Navigation Window with a ListBox containing ListBoxItems.
2. Open the navigation window multiple times and close it.
3. Use a memory profiler to observe memory allocations.
4. Notice that ListBox, ListBoxItem, and other related objects like Event from ItemContainerGenerator and ItemCollection remain in memory.
Refer the attached sample to replicate the issue.
[TextBoxExtSample.zip](https://github.com/user-attachments/files/18574460/TextBoxExtSample.zip)
### Expected behavior
When the Navigation Window is closed, the ListBox and its child elements should be released, and garbage collection should reclaim the memory.
### Actual behavior
The ListBox, ListBoxItem, and related objects like Event from ItemContainerGenerator and ItemCollection remain in memory, causing an accumulation of memory usage over time, leading to a memory leak.
### Regression?
Not sure if this issue was present in previous versions of WPF.
### Known Workarounds
_No response_
### Impact
The memory leak leads to increased memory usage in applications that frequently open and close Navigation Windows, eventually causing performance degradation.
### Configuration
_No response_
### Other information

Attached the reference image
Contributor guide
Assessment
This issue has not been assessed yet.