When using a member function for a C-Style callback, is it necessary to pin the delegate so that it is not collected or moved by the GC? I have read several posts on the subject but have not been able to determine if it's necessary or the correct way of implementing.
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
...
}