代码之家  ›  专栏  ›  技术社区  ›  bugfixr

新的XmlSerializer(typeof(MyClass))导致内存损坏?

  •  0
  • bugfixr  · 技术社区  · 15 年前

    我有一个动态加载程序集的应用程序:

     Assembly asm = Assembly.Load("MyClass.DLL");
     Type type = asm.GetType("MyClass");
    
     MyClass runningAssembly = (MyClass)Activator.CreateInstance(type);
     runningAssembly.start();
    

     XmlSerializer deserializer = new XmlSerializer(typeof(MyClass));
    

    并引发以下异常:

     "Attempted to read or write protected memory. This is often an indication that other memory is corrupt."
    

    我一直在这件事的起因上磕磕绊绊,没能弄清楚。有人有什么建议吗?我似乎也无法捕捉这个错误。。。它在试/接球时就吹了。

    总是 发生了。有时在调试模式下,它工作得很好,但似乎一旦启动,即使在重新启动Visual Studio之后,它也总是会发生。重新启动会清除它,并允许它至少工作一次。从编译的EXE运行时也会发生这种情况。

    编辑

    我尝试了同样的方法,但没有动态加载程序集。我直接把它称为一个类,即:

    MyClass c = new MyClass();
    c.start();
    

    同样的问题仍然存在,因此它似乎与动态加载无关。

    1 回复  |  直到 15 年前
        1
  •  1
  •   Simon Fischer    15 年前

    现在很难不知道是什么引起了这些问题 MyClass . 构造函数中的内容,尤其是 start()

    如果你的 方法正在启动新线程,并在新线程上引发异常。在 start