带有 .NET : are memory leaks determinate? 的 C++ dll

标签 c++ dll memory-leaks

我有 Windows 窗体 (C#) 应用程序,它使用 C++ dll(非托管代码)。图片加载完毕,调用dll函数进行处理。 问题是: exception: std::bad_alloc 当我连续多次从 dll 调用函数时抛出。 可能问题是因为没有删除 dll 中的内存,可能是因为 Windows 窗体中的垃圾收集器。 如何判断应用程序中的哪个项目导致内存泄漏?

最佳答案

我最近遇到了类似的问题,我设计了一个有用的解决方案来解决这个问题。您可以编写一个名为“freeObject”的解构函数,只是与 ~* 函数不同,您应该显式调用“freeObject”函数。这是一个例子。

void VideoAbstraction::freeObject(){
    videoCapture.~VideoCapture();
    videoWriter.~VideoWriter();
    backgroundImage.release();              
    currentStartIndex.release();
    currentEndIndex.release();
    mog.~BackgroundSubtractorMOG2();
    gFrame.release();           
    gForegroundMask.release();  
    gBackgroundImg.release();   
    currentMask.release();  
    vector<ObjectCube>().swap(partToCompound);  
    vector<Mat>().swap(compoundResult);
    vector<Mat>().swap(indexs);
    vector<Mat>().swap(indexe);
    vector<int>().swap(frame_start);
    vector<int>().swap(frame_end); 
}

外呼部分就是这样。

void UserVideoAbstraction::UserfreeObject(){
    userVB->freeObject();
}

关于带有 .NET : are memory leaks determinate? 的 C++ dll,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18398843/

相关文章:

c++ - 如何将从模板参数派生的编译时信息添加到模板?

C++ 插件 : pass object across boundary (emulating it)

android - 尝试将带有图像的表单发送到 PHP 服务器时 Android 中的内存泄漏

c - 共享内存有时不会空终止

c++ - 返回值的完美转发,未定义的行为?

c++ - 在有向循环图中找到所有可能的路径

java - 从 Java 调用 C++ DLL

c++ - 是否合理使用返回匿名结构的函数?

c++ - 如何查找lua堆栈中有多少项(值)

c# - 在 Visual Studio 2015 中调试 native 应用程序加载的托管 DLL