我有一個DLL,它是用C++設計的,包含在一個C#項目中,我有奇怪的AccessViolationExceptions發生非理性。我懷疑我的垃圾收集不正確。我有一個非託管方法apiGetSettings(來自DLL),它應該將數據複製到Settings對象(實際上是原始代碼中的一個結構,但.NET InterOp只允許將數據作爲類對象導入,我使用System.Runtime.InteropServices。 Marshal方法來分配和釋放內存,但它可能會留下垃圾,導致所有內容崩潰c#interop marshalling and disposing
現在,我應該在Settings類中實現IDisposable方法嗎(如果是非託管的?)。如果是,我該如何處理字符串作爲編組和UnmanagedType.ByValTStr我要如何處置設置的對象?
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);
}
謝謝你,我的救世主!否則我永遠不會想到這一點。這解決了我過去三個月以來的所有非理性問題。 :) – entro 2009-10-22 13:35:47