Fix file lock error when saving cropped image on network drives#157
Open
Slashxdd wants to merge 1 commit intoModuleArt:feature/new-webp-decoderfrom
Open
Fix file lock error when saving cropped image on network drives#157Slashxdd wants to merge 1 commit intoModuleArt:feature/new-webp-decoderfrom
Slashxdd wants to merge 1 commit intoModuleArt:feature/new-webp-decoderfrom
Conversation
Resolves ModuleArt#153: the viewer held the source file locked during save, causing "file is busy" errors especially on network (SMB) drives. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #153
Problem
When opening an image (especially from a network drive), cropping it, and saving with Ctrl+S, the application throws an error saying the file is busy/locked.
Root cause β two independent bugs:
1.
BitmapWrapper.csβnew Bitmap(path)holds the file lockedIn .NET GDI+, constructing a
Bitmapdirectly from a file path keeps an open file handle for the entire lifetime of the object. On local drives the OS typically allows overwriting a file you hold open, but on network (SMB) drives file locking is strict and the subsequentFileStreamwrite fails with "file is busy".Fix: read the file into a
MemoryStreamfirst (File.ReadAllBytes), create a temporaryBitmapfrom the stream, then copy it withnew Bitmap(tmp)β which produces a fully independent in-memory copy. The file handle is released immediately.2.
MainForm.csβcropBtn_Clickβ oldBitmapnever disposedAfter cropping, the old
Bitmap(which holds the file lock) was simply overwritten without being explicitly disposed. The GC would eventually collect it, but until then the file remained locked β so Ctrl+S would fail on network drives where the timing is most critical.3.
IcoWrapper.csβIcon(path, ...)not in ausingblockSame class of bug: the
Iconobject was created withoutusing, so its file handle was only released non-deterministically by the GC.Files changed
typewrappers/BitmapWrapper.csMemoryStreamto avoid holding a file locktypewrappers/IcoWrapper.csIconin ausingblock so the handle is released immediatelyforms/MainForm.csBitmapexplicitly incropBtn_Clickbefore reassigning