6

I am writing a chess game which allows two programs compete, the player needs to write a DLL and expose a function to tell the main application where his player will move next, suppose the function looks like this

public static void MoveNext(out int x, out int y, out int discKind);

The player's DLL can be written using C# or C++.

In the chess game application, I start a new thread to call the function that the player's DLL exposed to get where he will move in a turn, and I start a timer to prevent the player timeouts, if a player timesout i will kill the corresponding thread by following APIs

thread.Abort();
thread.Join();

I have the following issues as described below:

  1. The thread cannot be killed with 100% assurance (it depends on the player's code)

  2. During test I found that, if the player uses a deep recursions (and if there is memory leak in the player's program), the memory usage of the host application will increase and then the host application will be terminated without any exceptions.

Are there any techniques, ideas or methods that can handle the above issues?

From this CodeInChaos suggested to load player's DLL into separate domain and then unload it when necessary, I am not sure if it still works for the unmanaged DLL (C++) and if it will cause a low efficiency?

2 Answers 2

8

An unhandled exception in their AppDomain will still cause your program to terminate in .Net 2.0. You get a chance to respond to the exception through an event handler but not the ability to handle it.

Your best bet is to use processes for the kind of isolation you're looking for.

5
  • Would you please give me more detailed information about how to use processes to handle my problems?
    – Carlos Liu
    Commented Mar 28, 2011 at 0:42
  • You would need to create a helper hosting process that starts up the environment for the add in, which is dependent on what your add in model needs. Commented Mar 31, 2011 at 20:22
  • You would need to create a helper hosting process that starts up the environment for the add in, which is dependent on what your add in model needs. Then you would need to use an IPC mechanism to pass data to and from the hosting process. Which method you choose would depend on how much data you need to pass back and forth and how often among other reasons. .Net Remoting and WCF are two mechanisms specific to .Net that are supposed to make this easier. Shared memory regions, named pipes and COM are more language agnostic mechanisms that you can use but are more complicated and need more work. Commented Mar 31, 2011 at 20:28
  • Is there efficiency issue by using any of the IPC mechanism to communication between the game hosting application and player's plug-in hosting application because they will communicate with each other each step (there will be about 100 steps each round of game and there will be lots of rounds for the competition)
    – Carlos Liu
    Commented Apr 1, 2011 at 7:31
  • Any isolation mechanism will have an associated cost. Mechanisms based on marshalling and copying will have the greatest impact on communication, methods based on shared memory will have the least overhead but aren't as isolating/protective. Commented Apr 9, 2011 at 17:31
4
+25

If you can ensure your plugin DLL's are always managed code, then you have the option of createing a new application domain in your main application logic and loading the assembly containing the plugin into that domain.

This then gives you the option of trapping unhandled excpetions in that specific app domain and you then have the option of Unloading that whole app domain. That way you can cope with other peoples application plugins misbehaving and throwing exceptions. you also gain the option of specifying partial trust to further restrict what a plugin can do.

However this will not help if you cannot enforce the use of managed code plugins, and the earlier option of a set of seperate processes would be more apropriate.

Reading your post agin it seems you have some quality issues with the plugins you have to use. If you must cope with such buggy plugins I would take the previous advice and go with seperate processes.

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