Skip to content

Fix crash when deleting image while selection tool is active#160

Open
Slashxdd wants to merge 1 commit intoModuleArt:feature/new-webp-decoderfrom
Slashxdd:fix/crash-delete-while-selection-active
Open

Fix crash when deleting image while selection tool is active#160
Slashxdd wants to merge 1 commit intoModuleArt:feature/new-webp-decoderfrom
Slashxdd:fix/crash-delete-while-selection-active

Conversation

@Slashxdd
Copy link
Copy Markdown

Fixes #92

Root cause

NullReferenceException in GetSelectionRect() at originalImage.Width.

The exact crash chain:

  1. Selection tool is open (selForm != null)
  2. User presses Delete β€” deleteButton_Click fires (Delete is bound as ShortcutKeys on deleteBtn)
  3. deleteButton_Click disposes the image and sets originalImage = null
  4. Then calls NextFile() β†’ OpenFile() β†’ openImage()
  5. Inside openImage(), selectionBtn.Checked = false fires selectionBtn_CheckedChanged β†’ selForm.Dispose()
  6. During disposal, WinForms fires the SizeChanged event on selForm
  7. SelForm_SizeChanged calls GetSelectionRect()
  8. GetSelectionRect() accesses originalImage.Width β†’ originalImage is null β†’ crash

Fix

One-line null guard at the entry of SelForm_SizeChanged:

private void SelForm_SizeChanged(object sender, EventArgs e)
{
    if (originalImage == null) return;  // ← added
    Rectangle r = GetSelectionRect();
    ...
}

GetSelectionRect() is only meaningful when an image is loaded, so returning early when originalImage is null is always correct.

Resolves ModuleArt#92: pressing Delete with the selection tool open caused a
NullReferenceException in GetSelectionRect() because SelForm_SizeChanged
fired during selForm disposal while originalImage was already null.

The deletion flow sets originalImage = null, then calls NextFile() ->
openImage() -> selectionBtn.Checked = false -> selForm.Dispose().
During disposal the SizeChanged event fires, SelForm_SizeChanged calls
GetSelectionRect() which dereferences originalImage.Width -> crash.

Fix: guard SelForm_SizeChanged with an originalImage null check.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@Slashxdd Slashxdd force-pushed the fix/crash-delete-while-selection-active branch from 6fdbb37 to d4a8cf8 Compare March 30, 2026 16:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant