1

我的 C# 应用程序每 3 分钟拍摄一次图像,并且每次都按预期从 EDSDK 获取图像。我的问题是应用程序每次拍摄都会泄漏大约 5 mb,我非常确定问题出在 EDSDK 上。

代码:

    private uint CameraObjectEvent(uint inEvent, IntPtr inRef, IntPtr inContext)
    {
        switch (inEvent)
        {
            case EDSDK.ObjectEvent_DirItemRequestTransfer:
                GetCapturedItem(inRef);
                break;
        }

        return EDSDKErrorCodes.EDS_ERR_OK;
    }

    private void GetCapturedItem(IntPtr directoryItem)
    {
        uint error = EDSDKErrorCodes.EDS_ERR_OK;
        IntPtr stream = IntPtr.Zero;

        //Get information of the directory item.
        EDSDK.EdsDirectoryItemInfo dirItemInfo;
        error = EDSDK.EdsGetDirectoryItemInfo(directoryItem, out dirItemInfo);
        if (error != EDSDKErrorCodes.EDS_ERR_OK)
        {
            OnCameraErrorRaised(error, "EDSDK.EdsGetDirectoryItemInfo failed.");
            return;
        }

        //Create a file stream for receiving image.
        error = EDSDK.EdsCreateMemoryStream(dirItemInfo.Size, out stream);
        if (error != EDSDKErrorCodes.EDS_ERR_OK)
        {
            OnCameraErrorRaised(error, "EDSDK.EdsCreateMemoryStream failed");
            return;
        }

        //Fill the stream with the resulting image
        error = EDSDK.EdsDownload(directoryItem, dirItemInfo.Size, stream);
        if (error != EDSDKErrorCodes.EDS_ERR_OK)
        {
            OnCameraErrorRaised(error, "EDSDK.EdsDownload failed.");
            return;
        }

        error = EDSDK.EdsDownloadComplete(directoryItem);
        if (error != EDSDKErrorCodes.EDS_ERR_OK)
        {
            OnCameraErrorRaised(error, "EDSDK.EdsDownloadComplete failed.");
            return;
        }

        //Copy the stream to a byte[]
        IntPtr pointerToBytes = IntPtr.Zero;
        EDSDK.EdsGetPointer(stream, out pointerToBytes);

        MemoryStream imageStream = null;
        Image image = null;

        try
        {
            byte[] buffer = new byte[dirItemInfo.Size];
            GCHandle gcHandle = GCHandle.Alloc(buffer, GCHandleType.Pinned);
            Marshal.Copy(pointerToBytes, buffer, 0, (int)dirItemInfo.Size);

            //Create a MemoryStream and return the image
            imageStream = new MemoryStream(buffer);
            image = Image.FromStream(imageStream);
        }
        catch (Exception ex)
        {
            OnCameraErrorRaised(999999, string.Format("Failed while retrieving image from camera. Exception: {0}.", ex.Message));
        }
        finally
        {
            if (imageStream != null)
                imageStream.Dispose();
        }

        //If image was captured then send ImageCaptured event
        if (image != null)
            OnImageCaptured(image);

        //Clean up
        EDSDK.EdsRelease(pointerToBytes);
        pointerToBytes = IntPtr.Zero;

        EDSDK.EdsRelease(stream);
        stream = IntPtr.Zero;

        EDSDK.EdsRelease(directoryItem);
        directoryItem = IntPtr.Zero;
    }

OnImageCaptured(image) 行只是将图像发送到控制器,该控制器将来自相机的图像与另一个图像合并,然后在保存最后合并的图像后处理这两个图像:

    private void ImageCaptured(Image originalImage)
    {
        Image watermark = null;

        //Merge images
        try
        {
            watermark = Image.FromFile(Settings.Default.ImageWatermarkFilename);
            _imageController.Merge(originalImage, watermark);
            _imageController.SaveImage(originalImage);
        }
        catch (Exception ex)
        {
            LogManager.Instance.UpdateLog(string.Format("Error - Failed to merge and save images. Exception: {0}.", ex.Message));

            //HACK:
            System.Windows.Forms.Application.Restart();
            App.Current.Shutdown(); 
        }
        finally
        {
            originalImage.Dispose();
            if (watermark != null)
                watermark.Dispose();
        }
    }

那么为什么应用程序内存泄漏 - 有什么想法吗?

/干杯

4

1 回答 1

0

释放你的GCHandle. 每次拍照都是吃记忆的罪魁祸首

gcHandle.Free()

于 2012-05-30T13:23:18.790 回答