Jump to content
  • Advertisement
Sign in to follow this  
segt

Unity [.net] Advice on AppDomain Cleanup

This topic is 4627 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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
Advertisement
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
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
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
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
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!