Sign in to follow this  
segt

Unity [.net] Advice on AppDomain Cleanup

Recommended Posts

segt    122
I'm integrating an AppDomain solution in my game engine for C# scripting. I had it setup and working, but then I found this thread, the post with vizone in particular. It is a neat example, but how would the AppDomain ever get removed from memory (aside from program termination)? The system I am creating allows the user to switch from game to game from within the main menu, so the AppDomain must be unloaded before the next one gets created. I guess this just wouldn't fit my needs, but I do like its simplicity. Any ideas?

Share this post


Link to post
Share on other sites
Arild Fines    968
Quote:
Original post by segt
how would the AppDomain ever get removed from memory (aside from program termination)?

The AppDomain class has an Unload method. Does this not suffice for your needs?

Share this post


Link to post
Share on other sites
segt    122
Is it safe to call the unload method from within the AppDomain itself? Once all code paths have terminated will it then be garbage collected? That is my question. Normally you would unload an AppDomain from within a second AppDomain.

Share this post


Link to post
Share on other sites
gwihlidal    1004
You need to create a MarshalByRefObject proxy that wraps interfaces\types in your plugins\scripts but never returns any of those objects (aside from native types like string, int, etc..) back to the primary AppDomain. You create a second AppDomain, and load the proxy within it. When you are done with the second AppDomain you can unload it. You have to be careful though, if you mistakingly returned any types from the second AppDomain then the assembly will "leak" into the primary AppDomain and the unload won't do much.

This is a handy snippet to help debug assembly leaks:

StringBuilder buffer = new StringBuilder();

foreach (Assembly LoadedAssembly in AppDomain.CurrentDomain.GetAssemblies())
{
buffer.AppendLine(LoadedAssembly.GetName().Name);
}

MessageBox.Show(this, buffer.ToString(), "Loaded Assemblies in CurrentDomain");




<shamless_plug>My upcoming book covers this topic in great detail</shameless_plug>

Here is a fairly robust example of how to implement the MarshalByRefObject proxy and an object that creates the temporary AppDomain and uses the proxy.

Proxy wrapper:

using System;
using System.IO;
using System.Reflection;
using System.Collections.Generic;

namespace Plugin.Manager
{
using Plugin.API;

public class PluginProxy : MarshalByRefObject
{
List<Type> pluginTypes = new List<Type>();
List<PluginInfo> pluginInfo = new List<PluginInfo>();
List<IPlugin> pluginInstances = new List<IPlugin>();

public bool LoadAssembly(AppDomain appDomain, byte[] data)
{
try
{
Assembly assembly = appDomain.Load(data);

foreach (Type type in assembly.GetTypes())
{
if (!type.IsAbstract)
{
foreach (Type interfaceType in type.GetInterfaces())
{
if (interfaceType == typeof(IPlugin) & type.IsDefined(typeof(PluginAttribute), false))
{
pluginTypes.Add(type);

PluginAttribute pluginAttrib = type.GetCustomAttributes(typeof(PluginAttribute), false)[0] as PluginAttribute;

PluginInfo info = new PluginInfo(pluginAttrib.Component, pluginAttrib.Description);
pluginInfo.Add(info);
}
}
}
}

return true;
}
catch (Exception)
{
return false;
}
}

public bool CompileAssembly(AppDomain appDomain, string fileName)
{
try
{
PluginFactory factory = new PluginFactory();

Assembly assembly = factory.CompilePluginSource(new List<string>(new string[] { fileName }));

foreach (Type type in assembly.GetTypes())
{
if (!type.IsAbstract)
{
foreach (Type interfaceType in type.GetInterfaces())
{
if (interfaceType == typeof(IPlugin) & type.IsDefined(typeof(PluginAttribute), false))
{
pluginTypes.Add(type);

PluginAttribute pluginAttrib = type.GetCustomAttributes(typeof(PluginAttribute), false)[0] as PluginAttribute;

PluginInfo info = new PluginInfo(pluginAttrib.Component, pluginAttrib.Description);
pluginInfo.Add(info);
}
}
}
}

return true;
}
catch (Exception ex)
{
return false;
}
}

public bool ImplementsInterface(string interfaceName)
{
foreach (Type type in pluginTypes)
{
foreach (Type interfaceType in type.GetInterfaces())
{
if (interfaceType.Name.Equals(interfaceName))
return true;
}
}

return false;
}

public void Initialize()
{
bool exists = false;

foreach (Type type in pluginTypes)
{
foreach (IPlugin plugin in pluginInstances)
{
if (plugin.GetType().Equals(type))
{
exists = true;
break;
}
}

if (!exists)
{
IPlugin plugin = Activator.CreateInstance(type) as IPlugin;

ExecuteInitializeMethod(plugin);

pluginInstances.Add(plugin);
}

exists = false;
}
}

public void Release()
{
foreach (IPlugin plugin in pluginInstances)
{
ExecuteReleaseMethod(plugin);
}
}

public void ExecuteMethodNoReturn(string interfaceName, string method, object[] parameters)
{
foreach (IPlugin plugin in pluginInstances)
{
foreach (Type interfaceType in plugin.GetType().GetInterfaces())
{
if (interfaceType.Name.Equals(interfaceName))
{
ExecuteMethodNoReturn(plugin, method, parameters);
}
}
}
}

public object[] ExecuteMethodWithReturn(string interfaceName, string method, object[] parameters)
{
List<object> results = new List<object>();

foreach (IPlugin plugin in pluginInstances)
{
foreach (Type interfaceType in plugin.GetType().GetInterfaces())
{
if (interfaceType.Name.Equals(interfaceName))
{
results.Add(ExecuteMethodWithReturn(plugin, method, parameters));
}
}
}

return results.ToArray();
}

public PluginInfo[] QueryPluginInformation()
{
return pluginInfo.ToArray();
}

#region Plugin Method Invocation

/// <summary></summary>
/// <param name="plugin"></param>
/// <param name="proxy"></param>
private void ExecuteInitializeMethod(IPlugin plugin)
{
ExecuteMethodNoReturn(plugin, "Initialize", null);
}

/// <summary></summary>
/// <param name="plugin"></param>
/// <param name="proxy"></param>
private void ExecuteReleaseMethod(IPlugin plugin)
{
ExecuteMethodNoReturn(plugin, "Release", null);
}

/// <summary></summary>
/// <param name="plugin"></param>
/// <param name="methodName"></param>
/// <param name="parameters"></param>
private void ExecuteMethodNoReturn(IPlugin plugin, string methodName, object[] parameters)
{
MethodInfo method = plugin.GetType().GetMethod(methodName);

if (method != null)
method.Invoke(plugin, parameters);
}

private object ExecuteMethodWithReturn(IPlugin plugin, string methodName, object[] parameters)
{
MethodInfo method = plugin.GetType().GetMethod(methodName);

if (method != null)
return method.Invoke(plugin, parameters);

return null;
}

#endregion
}
}




Proxy usage class:

using System;
using System.IO;
using System.Security;
using System.Security.Permissions;
using System.Security.Policy;
using System.Collections;

namespace Plugin.Manager
{
using Plugin.API;

public sealed class PluginLibrary
{
private AppDomain appDomain;

private PluginProxy proxy;

private string name = string.Empty;

public string Name
{
get { return name; }
}

public bool Load(DirectoryInfo pluginDirectory, FileInfo plugin)
{
try
{
if (plugin.Exists)
{
using (FileStream stream = plugin.OpenRead())
{
byte[] assemblyData = new byte[stream.Length];

if (stream.Read(assemblyData, 0, (int)stream.Length) < 1)
{
return false;
}

AppDomainSetup setup = new AppDomainSetup();
setup.ApplicationName = "Plugins";
setup.ApplicationBase = AppDomain.CurrentDomain.BaseDirectory;
setup.ShadowCopyFiles = "true";
setup.ShadowCopyDirectories = pluginDirectory.FullName;

appDomain = AppDomain.CreateDomain("PluginDomain" + plugin.Name.Replace(".dll", "").Replace(".", ""), null, setup);

EnforceSecurityPolicy();

proxy = (PluginProxy)appDomain.CreateInstanceAndUnwrap("Plugin.Manager", "Plugin.Manager.PluginProxy");

if (plugin.Extension.EndsWith("cs") || plugin.Extension.EndsWith("js") || plugin.Extension.EndsWith("vb"))
{
if (!proxy.CompileAssembly(appDomain, plugin.FullName))
{
return false;
}
}
else if (!proxy.LoadAssembly(appDomain, assemblyData))
{
return false;
}

name = plugin.Name;

return true;
}
}
else
{
return false;
}
}
catch (IOException)
{
return false;
}
}

public void Unload()
{
if (appDomain == null)
return;

Release();

AppDomain.Unload(appDomain);

appDomain = null;
}

public PluginInfo[] QueryPluginInformation()
{
return proxy.QueryPluginInformation();
}

public void Initialize()
{
proxy.Initialize();
}

public void Release()
{
proxy.Release();
}

public bool ImplementsInterface(string interfaceName)
{
return proxy.ImplementsInterface(interfaceName);
}

public bool ImplementsInterface(Type interfaceType)
{
return proxy.ImplementsInterface(interfaceType.Name);
}

public void ExecuteMethodNoReturn(string interfaceName, string methodName, object[] parameters)
{
proxy.ExecuteMethodNoReturn(interfaceName, methodName, parameters);
}

public void ExecuteMethodNoReturn(Type interfaceType, string methodName, object[] parameters)
{
proxy.ExecuteMethodNoReturn(interfaceType.Name, methodName, parameters);
}

public object[] ExecuteMethodWithReturn(string interfaceName, string methodName, object[] parameters)
{
return proxy.ExecuteMethodWithReturn(interfaceName, methodName, parameters);
}

public object[] ExecuteMethodWithReturn(Type interfaceType, string methodName, object[] parameters)
{
return proxy.ExecuteMethodWithReturn(interfaceType.Name, methodName, parameters);
}

private void EnforceSecurityPolicy()
{
IMembershipCondition condition;
PolicyStatement statement;

PolicyLevel policyLevel = PolicyLevel.CreateAppDomainLevel();

PermissionSet permissionSet = new PermissionSet(PermissionState.None);
permissionSet.AddPermission(new SecurityPermission(SecurityPermissionFlag.Execution));

condition = new AllMembershipCondition();
statement = new PolicyStatement(permissionSet, PolicyStatementAttribute.Nothing);

// The root code group of the policy level combines all
// permissions of its children.
UnionCodeGroup codeGroup = new UnionCodeGroup(condition, statement);

NamedPermissionSet localIntranet = FindNamedPermissionSet("LocalIntranet");

condition = new ZoneMembershipCondition(SecurityZone.MyComputer);
statement = new PolicyStatement(localIntranet, PolicyStatementAttribute.Nothing);



// The following code limits all code on this machine to local intranet permissions
// when running in this application domain.
UnionCodeGroup virtualIntranet = new UnionCodeGroup(condition, statement);
virtualIntranet.Name = "Virtual Intranet";

// Add the code groups to the policy level.
codeGroup.AddChild(virtualIntranet);
policyLevel.RootCodeGroup = codeGroup;

appDomain.SetAppDomainPolicy(policyLevel);
}

private NamedPermissionSet FindNamedPermissionSet(string name)
{
IEnumerator policyEnumerator = SecurityManager.PolicyHierarchy();

while (policyEnumerator.MoveNext())
{
PolicyLevel currentLevel = (PolicyLevel)policyEnumerator.Current;

if (currentLevel.Label == "Machine")
{
IList namedPermissions = currentLevel.NamedPermissionSets;
IEnumerator namedPermission = namedPermissions.GetEnumerator();

while (namedPermission.MoveNext())
{
if (((NamedPermissionSet)namedPermission.Current).Name == name)
{
return ((NamedPermissionSet)namedPermission.Current);
}
}
}
}

return null;
}
}
}




The IPlugin interface is used to find plugins within an external assembly and work with them using a common interface. My plugin manager also supports domain security (a bunch of it shown here), and also runtime compilation of source code for plugins (factory not shown here).

Hope this helps!

~Graham

Share this post


Link to post
Share on other sites
segt    122
gwihlidal, that is a very useful post! I am thinking of implementing something very similar. I have a couple more question however. Every example I have looked at including yours uses the MarshalByRefObject interface for the main proxy object. Should all other scripted objects also inherit off this interface? For instance, I plan on having a base object type in my engine which users can inherit from in scripted objects, then placing those objects in my engine scene taking advantage of polymorphism. I have gotten the impression that any assembly which needs to cross the AppDomain barrier should use MarshalByRefObject for performance benefits. Is the best solution to make my base engine object inherit from that?

My next question is about your code. You create an appdomain, and then use that new domain to create an instance of and unwrap a pluginproxy. This should create the object inside of the new appdomain. Yet you pass in the created appdomain with the method LoadAssembly. This seems redundant to me, as you could use the static appdomain property of currentdomain to load any assembly. Is there reason behind your decision?

[Edited by - segt on October 24, 2005 12:18:50 AM]

Share this post


Link to post
Share on other sites
gwihlidal    1004
Quote:
Original post by segt
gwihlidal, that is a very useful post! I am thinking of implementing something very similar. I have a couple more question however. Every example I have looked at including yours uses the MarshalByRefObject interface for the main proxy object. Should all other scripted objects also inherit off this interface? For instance, I plan on having a base object type in my engine which users can inherit from in scripted objects, then placing those objects in my engine scene taking advantage of polymorphism. I have gotten the impression that any assembly which needs to cross the AppDomain barrier should use MarshalByRefObject for performance benefits. Is the best solution to make my base engine object inherit from that?

My next question is about your code. You create an appdomain, and then use that new domain to create an instance of and unwrap a pluginproxy. This should create the object inside of the new appdomain. Yet you pass in the created appdomain with the method LoadAssembly. This seems redundant to me, as you could use the static appdomain property of currentdomain to load any assembly. Is there reason behind your decision?


Glad to help!

No, you wouldn't want to base all your objects off of MarshalByRefObject, the first reason being that you should really only create proxies on lightweight representations of objects (check out the .NET Broker pattern in terms of Remoting).

You shouldn't need to pass your engine objects back and forth across different AppDomains, if anything you should have a single interface to do it with (lightweight). This could be IPlugin if you are making a plugin-enabled architecture, or some sort of Broker interface if you just need to communicate outside of the current AppDomain.

I wanted to keep things modular with my code, so I didn't rely on a certain AppDomain, hence the parameter passed in. Basically the code loads the external assembly containing classes that inherit from IPlugin into the temporary AppDomain. I hope I understood your question correctly. There is also a good possibility that I could refactor the code even further, since I spent a great deal of time fighting with assembly leaks, which is why the first code snippet I posted is so useful!

~Graham

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  

  • Similar Content

    • By EvaBalikova
      Main menu in Feudal Alloy. 
      twitter
    • By Jcyshadow97
      Hi guys,i m looking for someone that can work with me on a "top-down" multiplayer fps as 2d and 3d artist.I used photon server and i can take the part of programming.For now i made only the basic gameplay of the game that include shooting,switch weapon and and damage player.If someone can help me please contact me via e mail: 270514974@libero.it.
      I really appreciate your collaboration and hope you have a good day.....
      Thanks for you time to read the post
      At the bottom i attach some screenshot of the current game,i m sorry that i can't attach a video...



    • By Raptor42
      I'm looking to form a new game development team, mostly for training purposes.
      About me:
      I'm a student - Unity C# developer, who worked part-time in this industry for a couple of years already. I've been a lead developer in many "random collab groups" as well as a few companies. I specialize in creating 2D games for Android, but I'm looking forward to trying out new things - especially 3D development.
      Currently, I've got one Android game close to a release so I'd work for this team in my spare time. 
      About the project:
      I've been thinking about creating a simple tycoon-like simulation game for Android (and PC eventually), inspired by the Game Dev Story (initially released by Kairosoft in 1997) https://en.wikipedia.org/wiki/Game_Dev_Story 
      I haven't done much planning though, therefore I'm looking forward to hearing out your ideas.
      Right now, I've only created a test 3D scene using placeholder models and implemented a simple pathfinding system for me to play around with:
      https://i.imgur.com/xAd0l4o.png
      https://i.imgur.com/nHZerOT.png
      I'm looking to work with people who are:
      - willing to take a position of a: 3D modeller/2D artist/Designer
      - not necessarily very experienced, but eager to learn and improve their skills
      - active - check in at least once a day
      If you'd like to apply for a different position which I didn't list here, you are welcome to contact me as well.
      While this project is created mostly for learning purposes, if we ever get to release it and generate any revenue - you will recieve a certain percentage of it.
       
      To Apply:
      Send an email to rk.softwaredev@gmail.com
      Introduce yourself and attach an example of your work (if you have any)
    • By EvaBalikova
      Hello.

      We're a two-member team and I'd like to introduce you our game - Feudal Alloy. 
      It's a metroidvania-style action RPG with fishbowl-powered medieval robots.

      We've been working on the project for a few months. I'd be happy for any feedback.
      teaser
      homepage
      twitter
      facebook
       
      Our Steam page is public now: 
      steam link
       
       



    • By MisspeldGames
      Ballimals is a work in progress physics-based fighting game where your only source of movement is some form of a "Grappling Gun" or jump, be it a chicken's tongue or an elephant's trunk. It features a similar system to Super Smash Bros, where you have to knock other players off the screen. However there are no "percentage-health" system yet, and I probably won't add something like that either. All aspects of the game is currently made by me, and even though I might believe it is amazing I desperately need someone else to take a look at it and give me some feedback. Therefore, I would appreciate it if you could take a look at the screenshots, try the concept/movement demo and potentially check out my devlog.
      The game currently features three playable characters, three playable levels, a character select and a level select. All of the character's share the same basic moves, but have one unique special attack.

      Character select

      Level select

      Level 1 displaying the elephant performing his special attack: Elphquake.

      Level 2 displaying the pig performing his special attack: Megafart or something... Also, the egg is the chickens special attack and can eggsplode.

      Level 3 displaying the pig swinging by his snot.
      Download the concept/movement demo here.
      You can also listen to some of the music I have composed so far. I am fairly new to composing would love to hear what you think. You can find it on my devlog.
      Any and all feedback is extremely appreciated.
      Thanks for reading!
      Edit: You will need a controller to play the demo.
       
  • Popular Now