将成员函数用于 C 样式回调时,是否需要固定委托以使其不被 GC 收集或移动?我已阅读有关该主题的几篇文章,但无法确定是否有必要或正确的实施方式。
public delegate void FeedCallbackDelegate(int type,void* data,void *param);
public ref class CStyleApiWrapper
{
public:
void StartFeed();
private:
FeedCallbackDelegate^ managedFeedCallback;
IntPtr unmanagedFeedCallbackPtr;
void FeedCallback(int code,int type,void* data,void *param);
};
void CStyleApiWrapper::StartFeed()
{
managedFeedCallback = gcnew managedFeedCallback(this, &CStyleApiWrapper::FeedCallback);
unmanagedFeedCallbackPtr = Marshal::GetFunctionPointerForDelegate(managedFeedCallback);
//Start Feed
StartFeed((NOTIFY_FUNC)(void*)unmanagedFeedCallbackPtr,0,NULL);
}
void CStyleApiWrapper::FeedCallback(int type,void* data,void *param)
{
//Process Feed
...
}