C+中的内存泄漏+/C#应用 我有一个应用程序,它使用C++ DLL与佳能相机通信,C++中的方法从C++应用程序调用。我在应用程序中看到的是,拍照时,内存当然会增加。在我关闭“图像捕获窗口”后,应用程序仍保留与捕获所有图像时相同的内存量

C+中的内存泄漏+/C#应用 我有一个应用程序,它使用C++ DLL与佳能相机通信,C++中的方法从C++应用程序调用。我在应用程序中看到的是,拍照时,内存当然会增加。在我关闭“图像捕获窗口”后,应用程序仍保留与捕获所有图像时相同的内存量,c#,c++,wpf,ant,memory-leaks,C#,C++,Wpf,Ant,Memory Leaks,由于我的应用程序存在多个WPF UserControl层,我认为“Image Preview UserControl”无法获得垃圾回收,因为其他控件订阅了从该控件触发的事件。在谷歌搜索之后,我决定在事件上实现弱引用模式 //Source code found here: http://paulstovell.com/blog/weakevents public sealed class WeakEventHandler<TEventArgs> where TEventArgs

由于我的应用程序存在多个WPF UserControl层,我认为“Image Preview UserControl”无法获得垃圾回收,因为其他控件订阅了从该控件触发的事件。在谷歌搜索之后,我决定在事件上实现
弱引用模式

 //Source code found here: http://paulstovell.com/blog/weakevents

 public sealed class WeakEventHandler<TEventArgs> where TEventArgs : EventArgs
    {
        private readonly WeakReference _targetReference;
        private readonly MethodInfo _method;


        public WeakEventHandler(EventHandler<TEventArgs> callback)
        {
            _method = callback.Method;
            _targetReference = new WeakReference(callback.Target, true);
        }

        public void Handler(object sender, TEventArgs eventArgs)
        {
            var target = _targetReference.Target;
            if (target != null)
            {
                var callback =
                    (Action<object, TEventArgs>)
                        Delegate.CreateDelegate(typeof (Action<object, TEventArgs>), target, _method, true);
                if (callback != null)
                {
                    callback(sender, eventArgs);
                }
            }
        }
    }
以及在C#侧接收图像的方法:

<>我还有一个方法来清除C++中分配的内存,它占用了这样的字节指针:

_resultcallback(img->GetImageInfo().Data, img->GetImageInfo().Width, img->GetImageInfo().Height, img->GetImageInfo().BPP);
BOOL CanonEDSDKWnd::ClearImageBuffer(BYTE* img) {
    _debug->Write(_T("CanonEDSDKWnd::ClearImageBuffer"));
    delete[] img;
    return TRUE;
}
它是从C#code通过回调中的
IntPtr
调用的


_deleteAllocatedImageFunction(imagePtr)

我认为您的回调函数应该如下所示:

C++方面:

_resultcallback(
   img                    // extend the signature
   img->GetImageInfo().Data,
   img->GetImageInfo().Width,
   img->GetImageInfo().Height,
   img->GetImageInfo().BPP
);
C侧:


图像是如何从C++库中到达C代码的?非托管方法的调用是如何完成的?如何在托管端存储图像?等等,等等。。。如果没有代码方面的知识,很难说出任何有用的东西。请参阅我的编辑。我认为它应该是最基本的部分。我非常确定回调中
img
指向的对象必须以某种方式被释放。您应该有一个C++函数,它可以在代码复制到托管存储中之后调用它。请看我的编辑。这种方法错了吗?让我们来看看。我认为这行不通,因为
img
是一个
unique\u ptr
_resultcallback(
   img                    // extend the signature
   img->GetImageInfo().Data,
   img->GetImageInfo().Width,
   img->GetImageInfo().Height,
   img->GetImageInfo().BPP
);
private void OnResultImageCallback(IntPtr img, IntPtr imagePtr, int width, int height, int bitsPerPixel)
    {
        _state = CameraState.InitializedStandby;
        _cbResultData.Width = width;
        _cbResultData.Height = height;
        _cbResultData.BitsPerPixel = bitsPerPixel;

        int memSize = bitsPerPixel * width * height / 8;
        _cbResultData.data = new byte[memSize];
        Marshal.Copy(imagePtr, _cbResultData.data, 0, memSize);
        _deleteAllocatedImageFunction(img);

        if (ImageCaptured != null)
            ImageCaptured(_cbResultData.data, _cbResultData.Width, _cbResultData.Height, _cbResultData.BitsPerPixel);

        _cbResultData.data = null; 
    }