public override void Unload() { // 注释这两条代码之后可能导致内存泄漏,但会避免潜在的崩溃 //var env = new Native.Legacy.Environment(); //_destroy?.Invoke(ref env); base.Unload(); _create = null; _strategy = null; _destroy = null; }
public override bool Load(string dllPath, bool reverse, out Exception exception) { if (IsLoaded) { exception = new Exception("DLL has loaded."); return(false); } ReverseCoordinate = reverse; _lastDllPath = dllPath; var hModule = LoadLibrary(dllPath); var errorCode = Marshal.GetLastWin32Error(); if (hModule == IntPtr.Zero) { exception = new Exception($"Error load {dllPath}, code {errorCode}"); return(false); } CurrentModule = hModule; try { //BEGIN UNMANAGED FUNCTIONS _create = LoadFunction <LegacyStrategyDelegate>("Create"); _strategy = LoadFunction <LegacyStrategyDelegate>("Strategy"); _destroy = LoadFunction <LegacyStrategyDelegate>("Destroy"); //END UNMANAGED FUNCTIONS } catch (ArgumentNullException e) { Unload(); exception = new Exception("Missing function", e); return(false); } var env = new Native.Legacy.Environment() { CurrentBall = new Native.Legacy.Ball { Position = new Native.Legacy.Vector3 { x = 50, y = 41.5 } }, SelfRobots = new[] { new Native.Legacy.Robot { Position = new Native.Legacy.Vector3 { x = 90.5, y = 42 } }, new Native.Legacy.Robot { Position = new Native.Legacy.Vector3 { x = 81, y = 23 } }, new Native.Legacy.Robot { Position = new Native.Legacy.Vector3 { x = 81, y = 61 } }, new Native.Legacy.Robot { Position = new Native.Legacy.Vector3 { x = 62, y = 23 } }, new Native.Legacy.Robot { Position = new Native.Legacy.Vector3 { x = 62, y = 61 } }, } }; _create?.Invoke(ref env); userData = env.UserData; exception = null; return(true); }