0

I m facing a System.AccessViolationException issues during executing an SSIS package using C#.

I have a parent windows form where i ask the user if he would like to proceed and if so, the parent form hide, another form show and the below code executed.

The package executed successfully but as far as a there is an error trying to dispose the various objects.

Code:

namespace Run_Audit_Files_Project
{
    public partial class frmStatus : Form
    {
        private Package pkg;
        private Microsoft.SqlServer.Dts.Runtime.Application app;
        public frmWelcome ParentForm { get; set; }

        public frmStatus()
        {
            InitializeComponent();

            System.Threading.Tasks.Task.Run(() =>
                {
                    Microsoft.SqlServer.Dts.Runtime.Application localApp = null;
                    Microsoft.SqlServer.Dts.Runtime.Package localPkg = null;

                    try
                    {
                        localApp = new Microsoft.SqlServer.Dts.Runtime.Application();
                        localPkg = localApp.LoadPackage(@"C:\Users\XXXX\source\repos\XXXX\Audit Files Project\Package.dtsx", null);

                        DTSExecResult result = localPkg.Execute();

                        if (result == DTSExecResult.Success)
                        {
                            Update_Status("successful", "SSIS package executed successfully.");
                        }
                        else
                        {
                            System.Text.StringBuilder errorMessages = new System.Text.StringBuilder();

                            foreach (DtsError dtsError in this.pkg.Errors)
                            {
                                errorMessages.AppendLine($"• {dtsError.Description}");
                            }
                            Update_Status("error", errorMessages.ToString());
                            //MessageBox.Show($"Failed:\n{errorMessages}");
                        }
                    }
                    catch (Exception ex)
                    {
                        //Console.WriteLine(ex.Message);
                        if (ex.InnerException != null)
                            Update_Status("error", ex.InnerException.Message);
                        //MessageBox.Show(ex.InnerException.Message);
                    }
                    finally
                    {
                        localApp = null;
                        localPkg = null;

                        GC.Collect();
                        GC.WaitForPendingFinalizers();
                        GC.Collect();
                    }
                });

        }
        private void Update_Status(string state = "", string erromessage = "")
        {
            if (this.InvokeRequired)
            {
                // Ensures thread safety if called from a background thread
                this.Invoke(new Action(() => Update_Status(state, erromessage)));
            }
            else
            {
                lblProcess.Visible = false;
                lblSuccessful.Visible = false;
                lblError.Visible = false;
                lblComments.Visible = false;
                lblCurrentStatus.Visible = false;

                pbError.Visible = false;
                pbSuccess.Visible = false;
                pbWarning.Visible = false;

                btnClose.Enabled = false;

                lbResult.Visible = false;

                // Set visibility based on state or message
                switch (state.ToLower())
                {
                    case "successful":
                        lblSuccessful.Visible = true;
                        lblComments.Visible = false;
                        lblCurrentStatus.Visible = false;

                        pbSuccess.Visible = true;
                        btnClose.Enabled = true;

                        lbResult.Visible = true;
                        lbResult.ScrollAlwaysVisible = true;
                        lbResult.HorizontalScrollbar = true;
                        lbResult.Items.Add(erromessage);

                        break;
                    case "error":
                        lblError.Visible = true;
                        lblComments.Visible = false;
                        lblCurrentStatus.Visible = false;

                        pbError.Visible = true;
                        btnClose.Enabled = true;

                        lbResult.Visible = true;
                        lbResult.ScrollAlwaysVisible = true;
                        lbResult.HorizontalScrollbar = true;
                        lbResult.Items.Add(erromessage);

                        break;
                    default:
                        // Optional: show nothing special
                        break;
                }
            }
        }
        private void frmStatus_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = true;
        }
        private void btnClose_Click(object sender, EventArgs e)
        {

            if (ParentForm != null)
                ParentForm.Close();

            // Then close Form2 itself
            this.Close();

        }
    }
}

Error:

XEventAutoEngineLoad_002E_007Bdtor_007D((XEventAutoEngineLoad*)System.Runtime.CompilerServices.Unsafe.AsPointer(ref g_XEventAutoLoader));
System.AccessViolationException
  HResult=0x80004003
  Message=Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
  Source=<Cannot evaluate the exception source>
  StackTrace:
<Cannot evaluate the exception stack trace>
2
  • stackoverflow.com/questions/76067793/… Commented Nov 7 at 17:07
  • @HansPassant thanks for your time. In my case all sources are XML files and the destinations are Excel file or in some cases record set. Noted that SSIS package finished successfully. What do you think many cause the error? Commented Nov 7 at 18:00

1 Answer 1

1

To overcome the specific issue I have:

1. Remove any code from initialization event and just keep:

InitializeComponent()

2. Created a console app which run the SSIS package.

3. Call console app to run SSIS package from the form.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.