0

I've got an application that loads an assembly dynamically:

 Assembly asm = Assembly.Load("MyClass.DLL");
 Type type = asm.GetType("MyClass");

 MyClass runningAssembly = (MyClass)Activator.CreateInstance(type);
 runningAssembly.start();

Once loaded and the start() method is called, this line of code is executed:

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

And the following exception is thrown:

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

I've been stumbling on the cause of this and haven't been able to get a grasp on it. Does anyone have any tips? I also cannot seem to trap this error... it blows right through the try/catch.

By the way, the error doesn't always happen. Sometimes in debug mode it works fine, but it seems like once it starts, it'll always happen even after restarting Visual Studio. A reboot clears it up and allows it to work at least once. It also happens when running from the compiled EXE.

EDIT

I tried the same thing but without loading the assembly dynamically. I called it as a class directly, i.e:

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

And the same problem persists, so it does NOT appear to be related to being loaded dynamically.

1 Answer 1

1

It is hard to now what causes the problems without knowing anything about MyClass. Whats in the constructor and especially what is in the start() method? Does the code have any unsafe code? If you are addressing unsafe memory you could very likely experience the described behavior.

If your start() method is starting a new thread and an exception is thrown on the new thread you will not catch it in a try/catch around the start method.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.