Sign in to follow this  
segt

Unity [.net] Advice on AppDomain Cleanup

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
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

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  

  • Announcements

  • Forum Statistics

    • Total Topics
      628337
    • Total Posts
      2982164
  • Similar Content

    • By Afambus
      Hello,
      I have a full experienced team that looking for a composer and another 3d designer, im expecting as much from you there are other people in this server that can help you we have a determined team, we have done many things in our game there is more information in the discord about everyone and there skills there region and about the game, if you join the discord pm asap
      https://discord.gg/b2teN3m
    • By BAG Labs
      Mobile SoS

      Platform: Android
      Genre: Board
      Link: Google Play
       
      This games sharpen memory and test your strategies to place S-O-S pattern within time limit and serve 3 difficulties as Easy, Normal, and Hard.

      Goals of the game is to put S-O-S words in patterns (Horizontal, Vertical, and Diagonal) alternately with enemy.
       
      Features:
      Single Player Multiplayer Achievements Leaderboards  
       
      Screenshot:

       

       

       
      Link: Google Play
       
      Please help us improve this game with review
    • By abarnes
      Hello!
      I am a game development student in my second year of a three year program and I would like to start building my portfolio. I was thinking of creating some games to show what I can do to potential employers since I wont have any work related experience when I graduate. But as I'm sure you all know there are tons of ways to approach developing/designing a game and I'm curious if anyone had any insight as to any "standards" that come with this? Is it okay to use game engines like Unity, Unreal, Game Maker etc? Or would it be better to make a game from scratch to better show case your skills? Any and all advice will be greatly appreciated!
    • By Hilster
      Hello 2D Artists,
      I've started making a 2D Puzzle Adventure game for mobile and I'm looking for someone who would want in on creating assets for the game. The core of the programming is pretty much complete, you can walk within the grid laid out and push boxes, when there is an object on top of a pressure pad it will activate the linked objects or if there is one object with multiple linked pressure pads it requires you to activate all points for the object to become active. 

      The level iteration for the game is quick and simple, a Photoshop file that is made of individual pixels that represents objects is put into the game and it creates the level out of those pixels with the assigned objects.
      The objects that need sprites created so far is the character, box, pressure pad, door, trap door, the walls, the stairs and the tiled background.
      I intend to add more objects so the amount I'd like to add will be extended.
      My motivations for posting here is to have something that looks nice to be able to display on my portfolio, so if you're looking for a working game that you can place your art into and improve the look of your portfolio then we're in business.
      Please reply with a few past examples of your art below and I'll be in touch!
    • By thefollower
      Hi
      I have set up my TcpClient to connect to my server and that works fine. But i am a bit confused how i read messages from the network stream with it continuously running via async, without ever closing the connection ?
      My TcpClient Manager class has:
       
      public async Task<bool> Connect(string address, int port) { try { await _tcpClient.ConnectAsync(address, port); IsConnected = true; return true; } catch(Exception e) { Debug.Log(e); return false; } } public async Task<int> Read(byte[] readBuffer) { if (!IsConnected) return -1; using (var networkStream = _tcpClient.GetStream()) { try { var bytesRead = await networkStream.ReadAsync(readBuffer, 0, readBuffer.Length); return bytesRead; } catch (Exception e) { Debug.Log(e); IsConnected = false; return -1; } } }  
      So i thought to just run a co-routine and call Read constantly to get the most recent message, but that doesn't make much sense to me since a co-routine would be blocked with the await. How is this actually done? The MS Docs don't have very good Async examples with the TcpClient class so i don't know fully get how to keep calling Read correctly.
  • Popular Now