Implement IDispose properly
parent
d391a6973c
commit
6ce6af5fa0
|
@ -99,6 +99,7 @@ namespace SharpPhysFS
|
||||||
}
|
}
|
||||||
|
|
||||||
class Interop
|
class Interop
|
||||||
|
: IDisposable
|
||||||
{
|
{
|
||||||
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
|
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
|
||||||
public delegate void FnGetLinkedVersion(ref Version v);
|
public delegate void FnGetLinkedVersion(ref Version v);
|
||||||
|
@ -200,6 +201,11 @@ namespace SharpPhysFS
|
||||||
: this($"{libname}.dll", $"{libname}.dylib", $"{libname}.so")
|
: this($"{libname}.dll", $"{libname}.dylib", $"{libname}.so")
|
||||||
{ }
|
{ }
|
||||||
|
|
||||||
|
Func<string, IntPtr> loadLibrary;
|
||||||
|
Func<IntPtr, string, IntPtr> loadSymbol;
|
||||||
|
Func<IntPtr, bool> freeLibrary;
|
||||||
|
IntPtr library;
|
||||||
|
|
||||||
public Interop(string winlib, string maclib, string unixlib)
|
public Interop(string winlib, string maclib, string unixlib)
|
||||||
{
|
{
|
||||||
/* This method is used to dynamically load the physfs
|
/* This method is used to dynamically load the physfs
|
||||||
|
@ -211,28 +217,27 @@ namespace SharpPhysFS
|
||||||
* pointers from the loaded library
|
* pointers from the loaded library
|
||||||
*/
|
*/
|
||||||
|
|
||||||
Func<string, IntPtr> loadLibrary;
|
|
||||||
Func<IntPtr, string, IntPtr> loadSymbol;
|
|
||||||
string libraryName;
|
string libraryName;
|
||||||
|
|
||||||
IntPtr library;
|
|
||||||
|
|
||||||
if (Environment.OSVersion.Platform == PlatformID.Win32NT)
|
if (Environment.OSVersion.Platform == PlatformID.Win32NT)
|
||||||
{
|
{
|
||||||
loadLibrary = DynamicLoader.LoadLibrary;
|
loadLibrary = DynamicLoader.LoadLibrary;
|
||||||
loadSymbol = DynamicLoader.GetProcAddress;
|
loadSymbol = DynamicLoader.GetProcAddress;
|
||||||
|
freeLibrary = DynamicLoader.FreeLibrary;
|
||||||
libraryName = winlib;
|
libraryName = winlib;
|
||||||
}
|
}
|
||||||
else if(Environment.OSVersion.Platform == PlatformID.MacOSX)
|
else if(Environment.OSVersion.Platform == PlatformID.MacOSX)
|
||||||
{
|
{
|
||||||
loadLibrary = n => DynamicLoader.osx_dlopen(n, 1);
|
loadLibrary = n => DynamicLoader.osx_dlopen(n, 1);
|
||||||
loadSymbol = DynamicLoader.osx_dlsym;
|
loadSymbol = DynamicLoader.osx_dlsym;
|
||||||
|
freeLibrary = DynamicLoader.osx_dlclose;
|
||||||
libraryName = maclib;
|
libraryName = maclib;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
loadLibrary = n => DynamicLoader.unix_dlopen(n, 1);
|
loadLibrary = n => DynamicLoader.unix_dlopen(n, 1);
|
||||||
loadSymbol = DynamicLoader.unix_dlsym;
|
loadSymbol = DynamicLoader.unix_dlsym;
|
||||||
|
freeLibrary = DynamicLoader.unix_dlclose;
|
||||||
libraryName = unixlib;
|
libraryName = unixlib;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -252,5 +257,29 @@ namespace SharpPhysFS
|
||||||
field.SetValue(this, del);
|
field.SetValue(this, del);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#region IDisposable Support
|
||||||
|
private bool disposedValue = false; // To detect redundant calls
|
||||||
|
|
||||||
|
protected virtual void Dispose(bool disposing)
|
||||||
|
{
|
||||||
|
if (!disposedValue)
|
||||||
|
{
|
||||||
|
/*if (disposing)
|
||||||
|
{
|
||||||
|
// TODO: dispose managed state (managed objects).
|
||||||
|
}*/
|
||||||
|
|
||||||
|
freeLibrary(library);
|
||||||
|
|
||||||
|
disposedValue = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Dispose()
|
||||||
|
{
|
||||||
|
Dispose(true);
|
||||||
|
}
|
||||||
|
#endregion
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -693,9 +693,25 @@ namespace SharpPhysFS
|
||||||
return new PhysFSStream(this, handle, false);
|
return new PhysFSStream(this, handle, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool disposed = false;
|
||||||
|
|
||||||
|
protected virtual void Dispose(bool disposing)
|
||||||
|
{
|
||||||
|
if (!disposed)
|
||||||
|
{
|
||||||
|
if (disposing)
|
||||||
|
{
|
||||||
|
Deinit();
|
||||||
|
interop.Dispose();
|
||||||
|
}
|
||||||
|
|
||||||
|
disposed = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public void Dispose()
|
public void Dispose()
|
||||||
{
|
{
|
||||||
Deinit();
|
Dispose(true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue