Search code examples
c#winformsexceptionmemorysavefiledialog

SaveFileDialog() Attempted to read or write protected memory


I'm trying to use the SaveFileDialog() and I get the exception on saveFile.ShowDialog();:

System.AccessViolationException: 'Attempted to read or write protected memory. This is often an indication that other memory is corrupt.'

Here's my code:

private void saveFile(ACCObject acc) {

            IFormatter formatter = new BinaryFormatter();

            SaveFileDialog saveFile = new SaveFileDialog();

            // set a default file name
            saveFile.InitialDirectory = Environment.ExpandEnvironmentVariables(@"C:\Users\%USERNAME%\Desktop\");
            saveFile.Title = "Save File";
            saveFile.FileName = "unknown.txt";
            saveFile.DefaultExt = "txt";
            saveFile.FilterIndex = 1;

            // set filters - this can be done in properties as well
            saveFile.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*";


            DialogResult result = saveFile.ShowDialog();

            if (result == DialogResult.OK) {

                Stream stream = new FileStream(saveFile.FileName, FileMode.Create, FileAccess.Write);

                formatter.Serialize(stream, acc);
                stream.Close();
            }
        }

I have no issues using FolderBrowserDialog() or OpenFileDialog().

Does anyone know what could be wrong? I'm just trying to let the user save the object to deserialize it later on.

I'm using Visual Studio 2019 Professional on Windows 10.

Here's the stack trace:

StackTrace  "   at System.Windows.Forms.FileDialogNative.IFileDialog.Show(IntPtr parent)\r\n   at System.Windows.Forms.FileDialog.RunDialogVista(IntPtr hWndOwner)\r\n   at System.Windows.Forms.FileDialog.RunDialog(IntPtr hWndOwner)\r\n   at System.Windows.Forms.CommonDialog.ShowDialog(IWin32Window owner)\r\n   at System.Windows.Forms.CommonDialog.ShowDialog()\r\n   at com.aerocc.aerocc.saveFile(ACCObject acc) in C:\\Users\\user\\Source\\Repos\\user\\com-engineering-app\\Calculations\\aerocc\\aerocc.cs:line 613\r\n   at com.aerocc.aerocc.calculate() in C:\\Users\\user\\Source\\Repos\\user\\com-engineering-app\\Calculations\\aerocc\\aerocc.cs:line 425\r\n   at com.aerocc.aerocc.Preview_Click(Object sender, EventArgs e) in C:\\Users\\user\\Source\\Repos\\user\\com-engineering-app\\Calculations\\aerocc\\aerocc.cs:line 341\r\n   at System.Windows.Forms.Control.OnClick(EventArgs e)\r\n   at System.Windows.Forms.Button.OnClick(EventArgs e)\r\n   at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)\r\n   at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)\r\n   at System.Windows.Forms.Control.WndProc(Message& m)\r\n   at System.Windows.Forms.ButtonBase.WndProc(Message& m)\r\n   at System.Windows.Forms.Button.WndProc(Message& m)\r\n   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)\r\n   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)\r\n   at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)\r\n   at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)\r\n   at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)\r\n   at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)\r\n   at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)\r\n   at System.Windows.Forms.Application.Run(Form mainForm)\r\n   at com.Program.Main() in C:\\Users\\user\\Source\\Repos\\user\\com-engineering-app\\Program.cs:line 13"    string

Solution

  • Look at the below link to check how to serialize the file. Also, not clear about the ACCObject what it has.

    https://learn.microsoft.com/en-us/dotnet/api/system.runtime.serialization.formatters.binary.binaryformatter?view=netframework-4.8