FRK2007

Hi I'm writing because I' new in C#.

I am working with an API from Canon to use my Digital Camera from my PC.

Well When I try to take a lot of pictures in a short period of time, sometimes I hava this message:CallBackOnCollectedDelegate on the method that download the picture from the camera:

here is the code where i'm setting the delegate:

err = EDSDK.EdsSetObjectEventHandler(camera, EDSDK.ObjectEvent_All, this.handleObjectEvent, IntPtr.Zero);

And here is the implementation for the handleObjectEvent

public uint handleObjectEvent(uint inEvent, IntPtr inRef, IntPtr inContext)

{

IntPtr stream = IntPtr.Zero;

IntPtr directoryItem = inRef;

IntPtr inContext2;

EDSDK.EdsDirectoryItemInfo dirItemInfo;

uint err = EDSDK.EDS_ERR_OK;

string nroFoto;

dirItemInfo.szFileName = "";

dirItemInfo.Size = 0;

GC.KeepAlive(this);

switch (inEvent)

{

case EDSDK.ObjectEvent_DirItemRequestTransfer:

{

if (err == EDSDK.EDS_ERR_OK)

{

err = EDSDK.EdsGetDirectoryItemInfo(directoryItem, out dirItemInfo);

}

if (err == EDSDK.EDS_ERR_OK)

{

err = EDSDK.EdsCreateFileStream(dirItemInfo.szFileName, EDSDK.EdsFileCreateDisposition.CreateAlways, EDSDK.EdsAccess.ReadWrite, out stream);

}

if (err == EDSDK.EDS_ERR_OK)

{

err = EDSDK.EdsSetProgressCallback(stream, progressFunc, EDSDK.EdsProgressOption.Periodically, inContext);

}

if (err == EDSDK.EDS_ERR_OK)

{

err = EDSDK.EdsDownload(directoryItem, dirItemInfo.Size, stream);

}

if (err == EDSDK.EDS_ERR_OK)

{

err = EDSDK.EdsDownloadComplete(directoryItem);

}

EDSDK.EdsRelease(directoryItem);

EDSDK.EdsRelease(stream);

frmExamen.UpdateProgressBar(0);

break;

}

}

return EDSDK.EDS_ERR_OK;

}

Please I read that I have to put GC.KeepAlive() but I don't know how to resolve my problem. If someone could help me, i would appreciatte

Thanks...




Re: Visual C# General CallBackOnCollectedDelegate C#

TaylorMichaelL

Sounds like the object that owns the delegate is being GC'ed when it shouldn't be. You don't need to use GC.KeepAlive in most cases. Whatever object contains the method that contains the line

err = EDSDK.EdsSetObjectEventHandler(camera, EDSDK.ObjectEvent_All, this.handleObjectEvent, IntPtr.Zero);

needs to remain around until the delegate is no longer needed. You can store it as a field in another class or something. The other thing you should do is be sure to unhook the delegate before your app ends so the camera will not use your delegate.

Michael Taylor - 6/12/07

http://p3net.mvps.org





Re: Visual C# General CallBackOnCollectedDelegate C#

Peter Ritchie

Is EdsSetObjectEventHandler called from within the class with the handleObjectEvent menber Is there any sort of EdsUnsetObjectEventHandler method




Re: Visual C# General CallBackOnCollectedDelegate C#

soujanya

Hi....

Is your App getting crashed when the error occurs

I also had the similar problem of " Callback on Collected Delegate" I couldn't find a permanent solution for my prob yet.

But for the time being.... go to menu item Debug -> Exceptions -> Managed Debugging Assistans. and Turn Off the CheckBox.

(it was suggested for me by an MVP, nobugs! )

This will avoid the error temporarily..... but this is not the absolute solution.

I also reported the prob at Product Feedback, but there's no solution yet. If you find a good one, pls let me know, o.k!

Thank you,






Re: Visual C# General CallBackOnCollectedDelegate C#

TaylorMichaelL

Soujanya, that is a bad idea IMHO. Ignoring a potentially fatal error gives one a false sense of security. You are simply ignoring a problem that has to be resolved anyway.

The issue shouldn't be that hard to track down assuming you are getting an exception. If you get an exception then you can look at the stack trace to determine who is making the call. The problem is self-explanatory: a callback occurred to an instance method that no longer exists. The issue is tracking down why the object was collected to begin with. However once you see what method is being invoked it should be relatively easy to identify the instance object and then do a mental use-chain on it to make sure it remains around as long as it is needed.

If you would like to post some of the code that you're using we can take a look at it.

FRK2007, looking at your code I'm wondering if it really has anything to do with the callback. I notice that inside your handler you start what looks like an async event for downloading the image. Within there you call progressFunc. Is this function doing anything with the UI If so are you using InvokeRequired/Invoke to communicate with the UI thread If not then this may potentially be causing you the problem. Mucking with the UI on the wrong thread can lead to bizarre behavior. Best case is it will crash immediately. Worse case it will crash in some obscure location down the road. If this is the case comment out the code in the function and see if the problem goes away.

Michael Taylor - 6/15/07

http://p3net.mvps.org





Re: Visual C# General CallBackOnCollectedDelegate C#

soujanya

Hi Michael....

Thanks for your response. I too understand that what I did was just escaping from the prob rather than solving it. I also reported the same bug at the product feedback, but could find no proper solution for that.

can u please go thru' my thread regarding this issue......

Pls find it here... http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=1458955&SiteID=1

I still have the prob at times if i keep the debug assistant checked. Ps let me know a perfect solution for this.

Anyways...thanks again.






Re: Visual C# General CallBackOnCollectedDelegate C#

vtrush

Hi there FRK2007,

I also develop app with Canon camera and having the same problem with CallBackOnCollectedDelegate.
I'm just wondering if you found any resolution.

Thanks a lot.

Cheers, Viktor.