我希望有人可以告诉我可能导致此错误的原因:
尝试读取或写入受保护的内存.这通常表明其他内存已损坏.
我不能真正发布代码,因为这个错误似乎被抛出应用程序的任何随机区域.应用程序将在抛出错误之前的12-48小时内运行.有时它会在一个看似随机的位置停止并抛出上述错误,其他时候整个应用程序停止并且我得到一个错误的屏幕,上面写着"有一个致命的错误...这可能是一个CLR中的错误或......"关于PInvoke或其他非相关信息的内容.发生这种情况时,所有线程都显示终止,并且没有可用的调试信息.
简而言之,这就是应用程序的作用:
它是一个完全用C#编写的多线程服务器应用程序.客户端通过套接字连接到服务器.服务器为客户端运行虚拟"环境",以便他们可以相互交互并与环境进行交互.它消耗了相当多的内存,但我没有看到它泄漏.它通常消耗大约1.5GB.我认为它没有泄漏,因为内存使用在应用程序运行的整个时间内保持相对稳定.即使客户端没有做任何事情,它也会不断运行代码来维护环境.它不使用第三方软件或其他API.此应用程序使用的唯一外部资源是套接字连接和SQL数据库连接.它运行在64位服务器上.我尝试使用.net 2.0,3.5和4在VS2008和VS2010中进行调试.
我试过关闭编译器优化和几个微软热修复.似乎没有什么能让这个问题消失.如果有人知道任何可能的原因,或某种方式来确定导致问题的原因,将不胜感激.
我们正在编写一个代码,使用Windows Defender API对C#中的文件进行按需扫描.
[DllImport(@"C:\Program Files\Windows Defender\MpClient.dll")]
public static extern int WDStatus(out bool pfEnabled);
[DllImport(@"C:\Program Files\Windows Defender\MpClient.dll")]
public static extern int MpManagerOpen(uint dwReserved, out IntPtr phMpHandle);
[DllImport(@"C:\Program Files\Windows Defender\MpClient.dll")]
public static extern int MpScanStart(IntPtr hMpHandle, uint ScanType, uint dwScanOptions, IntPtr pScanResources, IntPtr pCallbackInfo, out IntPtr phScanHandle);
[DllImport(@"C:\Program Files\Windows Defender\MpClient.dll")]
public static extern int MpHandleClose(IntPtr hMpHandle);
private void DoDefenderScan_Click(object sender, EventArgs e)
{
try
{
bool pfEnabled;
int result = WDStatus(out pfEnabled); //Returns the defender status - It's working properly.
ErrorHandler.ThrowOnFailure(result, VSConstants.S_OK); …Run Code Online (Sandbox Code Playgroud) 我真的不明白这个错误是如何发生的.请自行检查代码
void dispatcherTimer_Tick(object sender, EventArgs e)
{
string srUrl = lstLocalIndex[irLocalIndex] + lstMainIndex[irMainIndex].Replace("0;","");
Task.Factory.StartNew(() =>
{
startNewWindow(srUrl);
});
}
void startNewWindow(string srUrl)
{
NewWindowThread<TitleWindow, string>(c => new TitleWindow(c), srUrl);
}
Run Code Online (Sandbox Code Playgroud)
现在这段代码就是发生错误的地方.我还会附上截图
private void NewWindowThread<T, P>(Func<P, T> constructor, P param) where T : Window
{
Thread thread = new Thread(() =>
{
T w = constructor(param);
w.Show();
w.Closed += (sender, e) => w.Dispatcher.InvokeShutdown();
try
{
System.Windows.Threading.Dispatcher.Run();
}
catch
{
}
});
thread.SetApartmentState(ApartmentState.STA);
try
{
thread.Start();
}
catch
{
}
}
Run Code Online (Sandbox Code Playgroud)
此错误导致整个软件抛出错误并停止工作,即使我在新线程中调用它们:( …
c# wpf unhandled-exception memory-corruption access-violation
申请/代码说明:
我的应用程序基于c#并使用SQL Server CE,并且在同一代码位置只有两次获得此异常.直到此版本才引入此例外的崩溃.此版本中唯一的变化是将.net框架更改为4.5.2.
我在处理a时遇到访问冲突异常SqlCeConnection,出现以下错误:
尝试读取或写入受保护的内存.这通常表明其他内存已损坏.
此异常不会被.net的try catch子句拦截 - 它会导致崩溃.
在我的代码中,我使用以下命令运行
try
{
var connectionString = string.Format("{0}{1}{2}", "Data Source=", _localDB, ";File Mode=Read Write;Max Database Size=4000;Persist Security Info=False;");
using (var sqlCeConnection = new SqlCeConnection(connectionString))
{
using (var sqlCeCommand = new SqlCeCommand())
{
sqlCeCommand.Connection = sqlCeConnection;
sqlCeCommand.CommandText = "SELECT * FROM Application";
sqlCeConnection.Open();
var result = (string)sqlCeCommand.ExecuteScalar();
isValid = !IsValid(result);
}
}
}
catch (Exception ex)
{
_log.Error("exception", ex);
}
Run Code Online (Sandbox Code Playgroud)
第一次崩溃的调用堆栈:
ntdll!ZwWaitForMultipleObjects+a
KERNELBASE!WaitForMultipleObjectsEx+e8
kernel32!WaitForMultipleObjectsExImplementation+b3
kernel32!WerpReportFaultInternal+215
kernel32!WerpReportFault+77
kernel32!BasepReportFault+1f
kernel32!UnhandledExceptionFilter+1fc
ntdll! ?? …Run Code Online (Sandbox Code Playgroud)