Click here to Skip to main content
Sign Up to vote bad
good
See more: C#delegates
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..Smile | :)
Posted 3 Sep '12 - 20:41
MAU7871.4K

Comments
Sergey Alexandrovich Kryukov - 4 Sep '12 - 3:14
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
MAU787 - 4 Sep '12 - 6:00
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
Sergey Alexandrovich Kryukov - 4 Sep '12 - 15:01
Still not enough. And your first line will not compile. Where is the definition of the delegate type? Don't mix up delegate types and instances. Why do you use GC.KeepAlive? Where is the definition of APIWrapper? --SA
MAU787 - 5 Sep '12 - 0:56
i have class file which uses device sdk and it contains all the delegates..
MAU787 - 5 Sep '12 - 1:13
i got this solution but i dont know where is the mda.config and how to enable it: http://msdn.microsoft.com/en-us/43yky316(VS.80).aspx
Member 2053006 - 6 Sep '12 - 12:42
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.
MAU787 - 7 Sep '12 - 2:57
thanx a lot...:)
MAU787 - 7 Sep '12 - 8:09
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...

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

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 OriginalGriff 286
1 Mohammed Hameed 268
2 Sergey Alexandrovich Kryukov 218
3 Mayur_Panchal 143
4 Santhosh G_ 108
0 Sergey Alexandrovich Kryukov 8,216
1 OriginalGriff 6,271
2 CPallini 3,528
3 Rohan Leuva 2,703
4 Maciej Los 2,234


Advertise | Privacy | Mobile
Web02 | 2.6.130516.1 | Last Updated 4 Sep 2012
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid