Page 1 of 1

Unexpected exception while downloading from Canon

Posted: Thu Apr 26, 2018 6:13 pm
by bvanderwkc
I am having an issue that is totally kicking my butt. While capturing photos every few seconds from a Canon camera, less than 5% of the time we are seeing the following exception:

Code: Select all

Unexpected exception while downloading.
Object reference not set to an instance of an object.
   at Canon.Eos.Framework.Internal.EosImageTransporter.Download(IntPtr directoryItem, UInt64 size, IntPtr stream) in C:\Users\bvanderw\Dropbox\Documents\Visual Studio 2017\Projects\kOS\kos-win\DigiCam\Canon.Eos.Framework\Internal\EosImageTransporter.cs:line 62
   at Canon.Eos.Framework.Internal.EosImageTransporter.Transport(IntPtr directoryItem, UInt64 size, IntPtr stream, Boolean destroyStream) in C:\Users\bvanderw\Dropbox\Documents\Visual Studio 2017\Projects\kOS\kos-win\DigiCam\Canon.Eos.Framework\Internal\EosImageTransporter.cs:line 74
   at Canon.Eos.Framework.Internal.EosImageTransporter.TransportAsFileName(IntPtr directoryItem, String imagePath, IntPtr context) in C:\Users\bvanderw\Dropbox\Documents\Visual Studio 2017\Projects\kOS\kos-win\DigiCam\Canon.Eos.Framework\Internal\EosImageTransporter.cs:line 101
   at CameraControl.Devices.Canon.CanonSDKBase.TransferFile(Object o, String filename)
   at Canon.Eos.Framework.Internal.SDK.Edsdk.EdsDownload(IntPtr inDirItemRef, UInt64 inReadSize, IntPtr outStream)
   at Canon.Eos.Framework.Internal.EosImageTransporter.Download(IntPtr directoryItem, UInt64 size, IntPtr stream) in C:\Users\bvanderw\Dropbox\Documents\Visual Studio 2017\Projects\kOS\kos-win\DigiCam\Canon.Eos.Framework\Internal\EosImageTransporter.cs:line 53
As you can see, it is failing at a call to the Canon SDK, so I am at a loss how to address this. We have seen this on two computers using different Canon cameras.

I have tried to duplicate this using the example program, but can't make it fail. Anyone else see anything like this? Any suggestions on what I might try or an alternative way of doing this?

Bruce

Re: Unexpected exception while downloading from Canon

Posted: Mon Apr 30, 2018 1:10 am
by bvanderwkc
This does appear to be an issue with memory allocation. If I surround the call to CanonSDKBase.TransferFile with GC.TryStartNoGCRegion and GC.EndNoGCRegion, the problem completely disappears.

Re: Unexpected exception while downloading from Canon

Posted: Thu May 03, 2018 9:41 pm
by admin
Thank for the feedback. I meet this issue time to time too, but i don't find any good solution for it, until now :)

Re: Unexpected exception while downloading from Canon

Posted: Thu May 03, 2018 11:56 pm
by Mosanec
hello
How do you set those properties and where in the code please?
regards

Re: Unexpected exception while downloading from Canon

Posted: Fri May 04, 2018 12:38 pm
by bvanderwkc
Call SuspendGarbageCollection before and ResumeGarbageCollection after the call to CanonSDKBase.TransferFile.

(Replace the calls to LogInfo and LogInternalException with your own logging.)

Code: Select all

    private void SuspendGarbageCollection()
    {
      try
      {
        var oneMB = 1024 * 1024;
        LogInfo(GC.TryStartNoGCRegion(5 * oneMB) ? "GC suspended" : "Unable to suspend GC");
      }
      catch (Exception ex)
      {
        LogInternalException("TryStartNoGCRegion", ex);
      }
    }

    private void ResumeGarbageCollection()
    {
      try
      {
        GC.EndNoGCRegion();
      }
      catch (Exception ex)
      {
        LogInternalException("EndNoGCRegion", ex);
      }
    }