我正在使用ctypes將一些C函數從DLL公開到Python腳本。其中一個函數返回一個動態大小的字符數組,我希望能夠在Python中讀取這個數組的內容,但是當我完成它時,屬性句柄也會釋放數組的內存。Python ctypes.string_at釋放行爲
的C語言代碼:
...
#ifdef __cplusplus
extern "C"
{
#endif
__declspec(dllexport) char * WINAPI get_str()
{
int str_len = ... // figure out how long it is gonna be and set it here
char *ary = (char *)malloc(sizeof(char) * str_len);
// populate the array
...
ary[str_len - 1] = '\0';
return ary;
}
#ifdef __cplusplus
}
#endif
我建立我的DLL,它複製到它會被發現的位置,然後有這樣的Python代碼:
import ctypes
my_dll = ctypes.WinDLL("MyDLLName.dll")
some_str = ctypes.string_at(my_dll.get_str())
print some_str
此代碼的所有工作正常正如你所期望的那樣。我的問題是:因爲ctypes.string_at在指定的內存位置創建一個字符串,當some_str超出Python解釋器的範圍時,該內存是否會被垃圾收集,或者我是否需要手動釋放它?
我可以有回答了我自己的問題......我寫了一個無限循環,讓它在重複調用my_dll.get_str()的同時運行,並觀察我在Windows中的內存使用情況,並且似乎繼續攀升。話雖如此,我仍然希望爲了理智的緣故具體回答。 – KSletmoe 2013-02-26 03:19:31