I have a DLL, which is designed in C++, included in a C# project and I have strange AccessViolationExceptions happening unrationally. I suspect that my garbage isn't collected correctly. I have an unmanaged method apiGetSettings (from the DLL) which should copy data to a Settings object (actually a struct in the original code, but .NET InterOp only allowed importing the data as class objects. I use the System.Runtime.InteropServices.Marshal methods to allocate and deallocate memory, but it might leave garbage behind that crashes everything.
Now, should I implement IDisposable methods in the Settings class (is it unmanaged?). If so, how do I dispose of the strings marshalled as UnmanagedType.ByValTStr and how do I dispose of the Settings objects?
using System.Runtime.InteropServices;
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
class Settings
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 33)]
internal string d;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 6)]
internal string t;
internal int b;
}
[DllImport(".\\foobar.dll", EntryPoint = "getSettings")]
private static extern int apiGetSettings(IntPtr pointerToSettings);
void GetSettings(ref Settings settings)
{
int debug = 0;
// Initialize a pointer for the structure and allocate memory
IntPtr pointerToSettings = Marshal.AllocHGlobal(43);
// Connect the pointer to the structure
Marshal.StructureToPtr(settings, pointerToSettings, true);
// Point the pointer
debug = apiGetSettings(pointerToSettings);
// Copy the pointed data to the structure
Marshal.PtrToStructure(pointerToSettings, settings);
// Free the allocated memory
Marshal.FreeHGlobal(pointerToSettings);
}