nickdu088 / SharpExt4

A .Net library to provide full access (read/write) to Linux ext2/ext3/ext4 filesystem
https://www.nickdu.com/?p=890
42 stars 9 forks source link

Error: Attempted to read or write protected memory. This is often an indication that other memory is corrupt. #13

Closed thuongthinh closed 1 year ago

thuongthinh commented 1 year ago

Hello! My project has 2 winform, Form A used the command: fs.OpenFile(); it's ok, but from Form A call Form B, get error "Attempted to read or write protected memory. This is often an indication that other memory is corrupt" in the command: fs.GetDirectories(); Help me! Thanks!

nickdu088 commented 1 year ago

You'd better copy your code to explain. SharpExt4 is just a library to provide the ext4 fs read/write.

thuongthinh commented 1 year ago

Here my code: Form A: private void frmLogin_Load(object sender, EventArgs e) {
disk = ExtDisk.Open(3); fs = ExtFileSystem.Open(disk, disk.Partitions[0]); }

private void btnLogin_Click(object sender, EventArgs e) { var file = fs.OpenFile("/USB/Test.txt", FileMode.Open, FileAccess.Read); var filelen = file.Length; var buf = new byte[filelen]; var count = file.Read(buf, 0, (int)filelen); file.Close(); var content = Encoding.Default.GetString(buf); if(content == "123") { // Call Form B frmMain frm = new frmMain(DeviceID); frm.Show(); this.Hide(); }
}

Form B: private void frmMain_Load(object sender, EventArgs e) { disk = ExtDisk.Open(3); fs = ExtFileSystem.Open(disk, disk.Partitions[0]); listFolderFile.DataSource = ListAllFiles(fs); // Error here! }

static List ListAllFiles(ExtFileSystem fs) { var result = new List(); foreach (var file in fs.GetDirectories("/", "*", SearchOption.AllDirectories)) // Error hẻ { result.Add(new FolderFiles { Name = file }); }
return result; }

nickdu088 commented 1 year ago

If you remove function, ListAllFiles(ExtFileSystem), and move the logic into formload, what do you get?

thuongthinh commented 1 year ago

i remove function, ListAllFiles(ExtFileSystem), then not error. Rrror "Attempted to read or write protected memory. This is often an indication that other memory is corrupt" in the command: de = ext4_dir_entry_next(&d); in function ListAllFiles(ExtFileSystem)

nickdu088 commented 1 year ago

fs.GetDirectories("/", "*", SearchOption.AllDirectories)

This will list all the files and directories under the root directory recursively!!! It will be massive amount of strings. This will cause out of memory!!!! Are you sure to use this way?

Try not to do this.

thuongthinh commented 1 year ago

Thank!