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>