SharpExt4 icon indicating copy to clipboard operation
SharpExt4 copied to clipboard

Unhandled Exception while disposing ExtFileStream

Open christopher-fabian opened this issue 3 years ago • 7 comments

Hi,

I get an unhandled exception during disposing the ExtFileStream. The target framework of my application is .NET 7.

I do simple copy operations of large files (up to 2 GB) from my NTFS-Disk to a Micro-SD (Ext4).

Source


  ExtDisk disk = ExtDisk.Open(diskNumber);
  ExtFileSystem fileSystem = ExtFileSystem.Open(disk, disk.Partitions[0]);

  byte[] buffer = File.ReadAllBytes(sourcePath);

  using ExtFileStream stream = fileSystem.OpenFile(destinationPath, FileMode.Create, FileAccess.Write);
  stream.Write(buffer, 0, buffer.Length);
  stream.Close();

The operation is successful but after the call of Dispose via the using-operator or calling Dispose I get the following exception: grafik grafik grafik

Is this the default behavior? Any idea for a workaround?

~Best Regards

christopher-fabian avatar Dec 12 '22 14:12 christopher-fabian

Compile as release mode and try again.

nickdu088 avatar Dec 12 '22 21:12 nickdu088

Same result in release mode:

grafik

christopher-fabian avatar Dec 13 '22 18:12 christopher-fabian

Then it could be heap corruption.

nickdu088 avatar Dec 13 '22 18:12 nickdu088

Do you have any idea how to check if the heap is corrupted and/or how to fix it? I‘m not familar with problems like this.

christopher-fabian avatar Dec 14 '22 18:12 christopher-fabian

Just a quick glimpse the screen shot you attached, it seems you are using x64 release, as you can see ext4_write takes uint64 as input, however extfilestream passes int32.

You said you were copying large file (2gb). This may be the reason.

Try x86 mode, or use smaller file

nickdu088 avatar Dec 14 '22 19:12 nickdu088

Thank you for your tips.

I found out the error only occurs when ExtFileStream.Close() is called shortly before disposing.

If I skip closing the stream before disposing, it works. But on the other hand I get a memory leak, the RAM is not released properly.

Is disposing not closing the stream at all? I think this would be the expected behavior.

Maybe we can improve the whole thing, Dispose should better not throw an error exception if the stream is closed.

christopher-fabian avatar Dec 16 '22 11:12 christopher-fabian

ExtFileStream inherits . Net Stream class and override close. https://github.com/nickdu088/SharpExt4/blob/main/SharpExt4/ExtFileStream.h#L37

It should be all handled by Stream base class.

nickdu088 avatar Dec 20 '22 16:12 nickdu088