我打電話從C#這樣的方法:妙傳VC++到VC#一個ULONG64陣列和釋放內存
[DllImport(@"C:\Hash.dll",
CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr ph_dct_videohash(
string file,
ref int length);
static void Main(string[] args)
{
int length = 0;
ph_dct_videohash(@"C:\Users\shady\H.avi", ref length);
Console.Read();
}
這裏是我從圖書館調用方法
ulong64* ph_dct_videohash(const char *filename, int &Length){
CImgList<uint8_t> *keyframes = ph_getKeyFramesFromVideo(filename);
if (keyframes == NULL)
return NULL;
Length = keyframes->size();
ulong64 *hash = (ulong64*)malloc(sizeof(ulong64)*Length);
//some code to fill the hash array
return hash;}
問題是如何在C#中檢索未簽名的64位Long數組,並在使用它之後釋放內存。如果它是由垃圾收集器管理的話,它會更好。
我試過Marshal.copy,但它沒有工作,我恐怕會有一個mem泄漏(我不知道mem是否會自動釋放)。任何幫助,將不勝感激。謝謝。
改爲傳遞數組作爲參數。沒有內存所有權的麻煩和不需要複製。 – 2012-02-20 14:50:43
如果我使用參數傳遞指向日期的指針,我仍然必須在C#中釋放它,對嗎?我不知道這將如何解決我的問題? – Tormentor308 2012-02-20 18:12:37
不,託管內存由垃圾回收器負責。 – 2012-02-20 18:16:21