• Advertisement
Sign in to follow this  

Unity [.net] Advice on AppDomain Cleanup

This topic is 4500 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
  • Advertisement
  • Popular Tags

  • Advertisement
  • Popular Now

  • Similar Content

    • By Vu Chi Thien
      Hi fellow game devs,
      First, I would like to apologize for the wall of text.
      As you may notice I have been digging in vehicle simulation for some times now through my clutch question posts. And thanks to the generous help of you guys, especially @CombatWombat I have finished my clutch model (Really CombatWombat you deserve much more than a post upvote, I would buy you a drink if I could ha ha). 
      Now the final piece in my vehicle physic model is the differential. For now I have an open-differential model working quite well by just outputting torque 50-50 to left and right wheel. Now I would like to implement a Limited Slip Differential. I have very limited knowledge about LSD, and what I know about LSD is through readings on racer.nl documentation, watching Youtube videos, and playing around with games like Assetto Corsa and Project Cars. So this is what I understand so far:
      - The LSD acts like an open-diff when there is no torque from engine applied to the input shaft of the diff. However, in clutch-type LSD there is still an amount of binding between the left and right wheel due to preload spring.
      - When there is torque to the input shaft (on power and off power in 2 ways LSD), in ramp LSD, the ramp will push the clutch patch together, creating binding force. The amount of binding force depends on the amount of clutch patch and ramp angle, so the diff will not completely locked up and there is still difference in wheel speed between left and right wheel, but when the locking force is enough the diff will lock.
      - There also something I'm not sure is the amount of torque ratio based on road resistance torque (rolling resistance I guess)., but since I cannot extract rolling resistance from the tire model I'm using (Unity wheelCollider), I think I would not use this approach. Instead I'm going to use the speed difference in left and right wheel, similar to torsen diff. Below is my rough model with the clutch type LSD:
      speedDiff = leftWheelSpeed - rightWheelSpeed; //torque to differential input shaft. //first treat the diff as an open diff with equal torque to both wheels inputTorque = gearBoxTorque * 0.5f; //then modify torque to each wheel based on wheel speed difference //the difference in torque depends on speed difference, throttleInput (on/off power) //amount of locking force wanted at different amount of speed difference, //and preload force //torque to left wheel leftWheelTorque = inputTorque - (speedDiff * preLoadForce + lockingForce * throttleInput); //torque to right wheel rightWheelTorque = inputTorque + (speedDiff * preLoadForce + lockingForce * throttleInput); I'm putting throttle input in because from what I've read the amount of locking also depends on the amount of throttle input (harder throttle -> higher  torque input -> stronger locking). The model is nowhere near good, so please jump in and correct me.
      Also I have a few questions:
      - In torsen/geared LSD, is it correct that the diff actually never lock but only split torque based on bias ratio, which also based on speed difference between wheels? And does the bias only happen when the speed difference reaches the ratio (say 2:1 or 3:1) and below that it will act like an open diff, which basically like an open diff with an if statement to switch state?
      - Is it correct that the amount of locking force in clutch LSD depends on amount of input torque? If so, what is the threshold of the input torque to "activate" the diff (start splitting torque)? How can I get the amount of torque bias ratio (in wheelTorque = inputTorque * biasRatio) based on the speed difference or rolling resistance at wheel?
      - Is the speed at the input shaft of the diff always equals to the average speed of 2 wheels ie (left + right) / 2?
      Please help me out with this. I haven't found any topic about this yet on gamedev, and this is my final piece of the puzzle. Thank you guys very very much.
    • By Estra
      Memory Trees is a PC game and Life+Farming simulation game. Harvest Moon and Rune Factory , the game will be quite big. I believe that this will take a long time to finish
      Looking for
      Programmer
      1 experience using Unity/C++
      2 have a portfolio of Programmer
      3 like RPG game ( Rune rune factory / zelda series / FF series )
      4 Have responsibility + Time Management
      and friendly easy working with others Programmer willing to use Skype for communication with team please E-mail me if you're interested
      Split %: Revenue share. We can discuss. Fully Funded servers and contents
      and friendly easy working with others willing to use Skype for communication with team please E-mail me if you're interested
      we can talk more detail in Estherfanworld@gmail.com Don't comment here
      Thank you so much for reading
      More about our game
      Memory Trees : forget me not

      Thank you so much for reading
      Ps.Please make sure that you have unity skill and Have responsibility + Time Management,
      because If not it will waste time not one but both of us
       

    • By RoKabium Games
      We've now started desinging the 3rd level of "Something Ate My Alien".
      This world is a gas planet, and all sorts of mayhem will be getting in our aliens way!
      #screenshotsaturday
    • By Pacoquinha Studios
      Kepuh's Island is Multiplayer 3D Survival Game where you survive on the Kepuh's Islands, confronting challenges that are not only other players but also bosses, and even the environment itself.
      We have a lowpoly faster battle-royale idea, where about 12 players on the map fighting for survival! Also adding some more things into that style such as bosses around the map giving you abilities and much more such as vehicles, weapons, skins, etc...
      Now we are on cartase which is a crowdfunding online which purpose is to raise funds for the development of the game. Come and be part of this development.
      Link for Cartase: https://www.catarse.me/kepuhsisland?ref=project_link
      We post updates and trailers on
      Twitter: https://twitter.com/pcqnhastudios
      Facebook: https://www.facebook.com/pacoquinhastudios/
      Site: http://pacoquinhastudios.com.br
      If you could check out it would be great
      Thnks
      Some images:





  • Advertisement