小编Din*_*Din的帖子

C#错误"尝试读取或写入受保护的内存"或"外部组件引发异常"

我有一个C#程序(目标框架.NET 4.0),它调用COM对象(非托管代码),正确管理所有对象,不再需要时销毁,等等.

我也有一些异常处理程序,没有自定义异常的try/catch块.然而有时候这个程序崩溃了,大部分时间都是随机行为.堆栈跟踪也有所不同,例如:

System.AccessViolationException was unhandled
  Message=Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
  Source=System.Windows.Forms
  StackTrace:
       at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
       at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.Run(Form mainForm)
       at IndexerTester.Program.Main() in D:\Tester\Program.cs:line 17
  InnerException:
Run Code Online (Sandbox Code Playgroud)

在Program.cs中,第17行:Application.Run(new Form1());

System.Runtime.InteropServices.SEHException was unhandled
  Message=External component has thrown an exception.
  Source=System.Windows.Forms
  ErrorCode=-2147467259
  StackTrace:
       at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
       at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 …
Run Code Online (Sandbox Code Playgroud)

c#

7
推荐指数
2
解决办法
6601
查看次数

XmlSerializer不使用sgen创建的XmlSerializers.dll

在我的Visual Studio 2010项目中,我使用以下Post-Build事件命令行来使用sgen来创建XmlSerializers.dll.

发布后事件:

"$(ProgramFiles)\Microsoft SDKs\Windows\v7.0A\Bin\NETFX 4.0 Tools\sgen.exe" /a:"$(TargetPath)" /c:/keyfile:"c:\myproject\mykey.snk" /f
Run Code Online (Sandbox Code Playgroud)

我的项目名称很强,所以使用相同的键强名称"XmlSerializers.dll".VS在输出文件夹中创建XmlSerializers.dll.

但是,我注意到使用ProcessMonitor,.NET仍然在运行时调用CSC.exe.我来到这篇帖子,用户有类似的问题,并通过使用XmlSerializer(Type)构造函数解决.

我在我的代码中使用了相同的技术,但它仍然调用csc.exe:

var fs = new FileStream(SettingsFilePath, FileMode.Open);
try
{
var serializer = new XmlSerializer(typeof(AppSettings));
settings = (AppSettings)serializer.Deserialize(fs);
}
finally
{
fs.Close();
}
Run Code Online (Sandbox Code Playgroud)

我需要使用预编译的XML序列化的原因,因为性能和我有时看到Windows关闭时csc.exe错误.我的应用程序在表单关闭时保存数据,在关闭期间,它失败,因为Windows在关闭序列期间不允许新进程启动.我已经看到了通过预编译XML序列化来解决这个问题的建议.

有关为什么XmlSerializer不使用sgen创建的XmlSerializers.dll的任何建议?

谢谢.

.net c# sgen xml-serialization

7
推荐指数
1
解决办法
4189
查看次数

标签 统计

c# ×2

.net ×1

sgen ×1

xml-serialization ×1