Click here to Skip to main content
15,881,600 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have one Initialize() in my class and I am calling a delegate wrapper method from this function as follows (the call back method will call around 20 times in a sec),

C#
public Initialize()
{
                CX_OUTPUT_CALLBACK cb = new CX_OUTPUT_CALLBACK(OutDelegateCb);
                AureusSDK_Wrapper_VideoCallbackFunSetup.CX_SetOuputCallBack(cb, cnt);
}

public static void OutDelegateCb(IntPtr p_aureus_video_output, int num_of_people, IntPtr pObject)
{
 //Process the video...
}


I am getting the below error, when I execute the method. Could you anyone help me to resolve this issue:

A callback was made on a garbage collected delegate of type 'FaceTracker_Abc!AAA.VideoAnalytics.Framework.Implementations.FaceTracker_Abc.CX_OUTPUT_CALLBACK::Invoke'. This may cause application crashes, corruption and data loss. When passing delegates to unmanaged code, they must be kept alive by the managed application until it is guaranteed that they will never be called.
Posted
Updated 27-Jun-13 1:19am
v2

C#
public Initialize()
{
                CX_OUTPUT_CALLBACK cb = new CX_OUTPUT_CALLBACK(OutDelegateCb);
                AureusSDK_Wrapper_VideoCallbackFunSetup.CX_SetOuputCallBack(cb, cnt);
                GC.KeepAlive(cb);
}
 
public static void OutDelegateCb(IntPtr p_aureus_video_output, int num_of_people, IntPtr pObject)
{
 //Process the video...
}


That would prevent garbage collection from collecting your object. The problem is that you don't havea ny reference (outside of COM) to the "cb" variable. Since garbage collecting is a reference counting operation, when the garbage collector runs it sees the "cb" is not referenced anywhere and garbage collects it.
 
Share this answer
 
Comments
Jeneesh K. Velayudhan 27-Jun-13 10:34am    
Hi Ron Beyer,

Thanks for your response.

I have implemented the same GC.KeepAlive(cb);

But, still I am getting the same error on the next call.

Since it is an async call, it calls min 25 times in a sec.

Please suggest me any other solution for the same.
Ron Beyer 27-Jun-13 17:46pm    
Try declaring cb outside of the Initialize routine in the class code. Just declare it there, and in the Initialize routine you can initialize it. As long as you aren't calling the Initialize routine 25/sec then it should work.
Thank you, Ron Beyer...

It worked...:)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900