Skip to content

Commit 08b4ee9

Browse files
committed
Fixed missed Dispose on exception
1 parent 7c784fb commit 08b4ee9

File tree

1 file changed

+26
-10
lines changed

1 file changed

+26
-10
lines changed

Library/DiscUtils.Vhdx/DiskImageFile.cs

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -529,26 +529,42 @@ internal DiskImageFile CreateDifferencing(FileLocator fileLocator, string path)
529529
{
530530
var stream = fileLocator.Open(path, FileMode.Create, FileAccess.ReadWrite, FileShare.None);
531531

532-
var fullPath = _fileLocator.GetFullPath(_fileName);
533-
var relativePath = fileLocator.MakeRelativePath(_fileLocator, _fileName);
534-
var lastWriteTime = _fileLocator.GetLastWriteTimeUtc(_fileName);
532+
try
533+
{
534+
var fullPath = _fileLocator.GetFullPath(_fileName);
535+
var relativePath = fileLocator.MakeRelativePath(_fileLocator, _fileName);
536+
var lastWriteTime = _fileLocator.GetLastWriteTimeUtc(_fileName);
535537

536-
InitializeDifferencingInternal(stream, this, fullPath, relativePath, lastWriteTime);
538+
InitializeDifferencingInternal(stream, this, fullPath, relativePath, lastWriteTime);
537539

538-
return new DiskImageFile(fileLocator, path, stream, Ownership.Dispose);
540+
return new DiskImageFile(fileLocator, path, stream, Ownership.Dispose);
541+
}
542+
catch
543+
{
544+
stream.Dispose();
545+
throw;
546+
}
539547
}
540548

541549
internal async ValueTask<DiskImageFile> CreateDifferencingAsync(FileLocator fileLocator, string path, CancellationToken cancellationToken)
542550
{
543551
var stream = fileLocator.Open(path, FileMode.Create, FileAccess.ReadWrite, FileShare.None);
544552

545-
var fullPath = _fileLocator.GetFullPath(_fileName);
546-
var relativePath = fileLocator.MakeRelativePath(_fileLocator, _fileName);
547-
var lastWriteTime = _fileLocator.GetLastWriteTimeUtc(_fileName);
553+
try
554+
{
555+
var fullPath = _fileLocator.GetFullPath(_fileName);
556+
var relativePath = fileLocator.MakeRelativePath(_fileLocator, _fileName);
557+
var lastWriteTime = _fileLocator.GetLastWriteTimeUtc(_fileName);
548558

549-
await InitializeDifferencingInternalAsync(stream, this, fullPath, relativePath, lastWriteTime, cancellationToken).ConfigureAwait(false);
559+
await InitializeDifferencingInternalAsync(stream, this, fullPath, relativePath, lastWriteTime, cancellationToken).ConfigureAwait(false);
550560

551-
return new DiskImageFile(fileLocator, path, stream, Ownership.Dispose);
561+
return new DiskImageFile(fileLocator, path, stream, Ownership.Dispose);
562+
}
563+
catch
564+
{
565+
stream.Dispose();
566+
throw;
567+
}
552568
}
553569

554570
internal MappedStream DoOpenContent(SparseStream parent, Ownership ownsParent)

0 commit comments

Comments
 (0)