相关文章推荐
气势凌人的可乐  ·  vue.js - Nuxt and ...·  1 年前    · 
考研的苦瓜  ·  vue ...·  1 年前    · 
hi all
i have one application which calls one function from dll.
that dll invokes one delegate for capturing fingerprint.
but after 4-5 capture i m getting error..
A callback was made on a garbage collected delegate of type ' SOSComp!CG4Essentials.APIWrapper+CG4_CallbackResultImage::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.
SSO is my dll and CallbackResultImage is my delegate.
I have goggled this problem and got some solutions
i made my delegate declaration as static and also initializing it at every call of function. But still getting same error.
please help me...
god bless u all..
thank you..:)
Normally, it should not happen; if a delegate instance is accessible, it cannot be garbage-collected. It may mean that you screw up the delegate mechanism in some really tricky way which I cannot picture right now. So, can you make a complete but very small code sample which could be used to reproduce the problem? It should not use any special hardware. If this is a problem, please show relevant fragments of your code: how you declare delegate type, delegate instance, invoke it, add a handler to the invocation list of the delegate instance.
--SA
yes i m using fingerprint scanner.
i m declaring delegate as this..
public static APIWrapper.CG4_CallbackResultImage _CallbackResultImageDelegate=null;

then assigning function to delegate in class constructor:
public WrapperClass()
{

_CallbackResultImageDelegate = new APIWrapper.CG4_CallbackResultImage(UserResultFuc);

GC.KeepAlive(_CallbackResultImageDelegate);
}


then calling delegate like this:
nRc = APIWrapper.CG4_M_Capture_RegisterCallbackResultImage(
nHandle,
_CallbackResultImageDelegate,
this
);

then calling the function which will call this delegate from native code
From the code you posted you are declaring the delegate reference as a static, so you can only reference one delegate, but you create a delegate instance for every WrapperClass instance.

If you create more then one WrapperClass then one of the delegate instances will go out of scope and cause this error.

Either remove the static in the first line to have a delegate reference for each WrapperClass or create a single delegate instance in the WapperClass static constructor (the callback function -UserResultFuc- should be passed the WrapperClass instance as a parameter)

One other thing to remember is you will need to remove the delegate when you are finished with the WrapperClass instance. A dispose method would probably be the best way to do this.
hi i changed my code according to ur instruction
declared delegate as this:
public APIWrapper.CG4_CallbackResultImage _CallbackResultImageDelegate = null;


then assigning function to delegate in class constructor:
public WrapperClass()
{

_CallbackResultImageDelegate = new APIWrapper.CG4_CallbackResultImage(UserResultFuc);


}


then calling delegate like this:

WrapperClass wrp = new WrapperClass();
nRc = APIWrapper.CG4_M_Capture_RegisterCallbackResultImage(
nHandle,
_CallbackResultImageDelegate,
wrp
);
and after processing i m setting delegate as null
it run for first time then it is giving error...
  • Read the question carefully.
  • Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  • If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  • Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question. Let's work to help developers, not make them feel stupid.
  •