Files icon indicating copy to clipboard operation
Files copied to clipboard

Fix: Fixed crash that would sometimes occur when resizing the window

Open workbysaran opened this issue 1 month ago • 4 comments

Resolved / Related Issues Fixed a crash (DivideByZeroException) that occurred when resizing the window with multiple panes split vertically.

Closes #17739

Steps used to test these changes

  1. Open Files with dual pane (vertical split)
  2. Resize the window to smaller sizes
  3. Resize window back to larger size and verify right pane restores with correct folder
  4. Test multiple resize cycles and layouts to ensure stability

workbysaran avatar Nov 21 '25 06:11 workbysaran

Thank you for the PR. Can you please provide some details on why you went with this approach? Ideally the minimum size wouldn't have to change, and we would instead fix the underlying cause.

yaira2 avatar Nov 23 '25 22:11 yaira2

@yaira2 The application was crashing with a System.DivideByZeroException in Microsoft's WinUI framework when the window was resized too small with multiple panes displayed vertically. The crash occurs in WinUI's internal layout engine(FrameworkElement.MeasureOverride) when the available window space becomes insufficient to handle the grid's minimum height and width constraints.

In fact, the same issue happens even when using a single pane but the crash threshold is much lower (nearly 0px) compared to the original default size set by the code (316×416). With dual panes, the crash occurs around 700-850px width. So setting MinWidth = 850 and MinHeight = 646 on the main window itself prevents users from resizing below the threshold before WinUI's layout engine runs, avoiding the crash condition entirely.

If you don't want to set the size explicitly, we need to spend more time on research to find out why the internal Microsoft component fails, find out if there is a way to handle the validation ourselves, and confirm whether it's a Microsoft bug and report it accordingly.

Furthermore, we can enhance my previous fix to set the minimum size dynamically based on whether multiple panes are enabled.

Please let me know how you’d like to proceed.

workbysaran avatar Nov 24 '25 07:11 workbysaran

With further investigation, the DivideByZeroException occurs because the restored pane has no navigation parameters, leaving it uninitialized. When WinUI's layout engine attempts to measure and arrange the empty pane, it encounters invalid state, which can lead to a division-by-zero error during its dimension calculations.

I fixed the issue by saving the navigation parameters and restoring them when the pane is shown again.

workbysaran avatar Dec 05 '25 08:12 workbysaran

@workbysaran I don't recall the original behavior (maybe @winston-de or @gave92 remembers) but the other option is to use 'home' when adding back the second pane. Does anyone have a preference?

yaira2 avatar Dec 08 '25 21:12 yaira2