gpt4 book ai didi

c# - C++/CLI 代码中的内存泄漏。我做错了什么?

转载 作者:可可西里 更新时间:2023-11-01 18:33:24 26 4
gpt4 key购买 nike

用 C++ 编写无 memleak 的代码对我来说不是问题,我只是坚持 RAII成语。

在 C# 中编写无 memleak 代码也不是很难,垃圾收集器会处理它。

不幸的是,编写 C++/CLI 代码对我来说是个问题。我以为我已经理解了它是如何工作的,但我仍然有很大的问题,我希望你能给我一些提示。

这是我的:

用 C# 编写的 Windows 服务,在内部使用 C++ 库(例如 OpenCV)。使用 C++/CLI 包装器类访问 C++ 类。例如我有一个 MatW cv::Mat 的 C++/CLI 包装类图像对象,将 System::Drawing::Bitmap 作为构造函数参数:

public ref class MatW
{
public:
MatW(System::Drawing::Bitmap ^bmpimg)
{
cv::Size imgsize(bmpimg->Width, bmpimg->Height);
nativeMat = new Mat(imgsize, CV_8UC3);

// code to copy data from Bitmap to Mat
// ...
}

~MatW()
{
delete nativeMat;
}

cv::Mat* ptr() { return nativeMat; }

private:
cv::Mat *nativeMat;
};

例如,另一个 C++ 类可能是

class PeopleDetector
{
public:
void detect(const cv::Mat &img, std::vector<std::string> &people);
}

及其包装类:

public ref class PeopleDetectorW
{
public:
PeopleDetectorW() { nativePeopleDetector = new PeopleDetector(); }
~PeopleDetectorW() { delete nativePeopleDetector; }

System::Collections::Generic::List<System::String^>^ detect(MatW^ img)
{
std::vector<std::string> people;
nativePeopleDetector->detect(*img->ptr(), people);

System::Collections::Generic::List<System::String^>^ peopleList = gcnew System::Collections::Generic::List<System::String^>();

for (std::vector<std::string>::iterator it = people.begin(); it != people.end(); ++it)
{
System::String^ p = gcnew System::String(it->c_str());
peopleList->Add(p);
}

return peopleList;
}

下面是对我的 Windows 服务 C# 类中的方法的调用:

Bitmap bmpimg = ...
using (MatW img = new MatW(bmpimg))
{
using (PeopleDetectorW peopleDetector = new PeopleDetector())
{
List<string> people = peopleDetector.detect(img);
}
}

现在,这是我的问题:

  • 我的代码有什么问题吗?
  • 我必须使用 using 吗?在我的 C# 代码中?当使用多个包装器对象时,它使代码变得难看,因为 using语句必须嵌套
  • 我可以使用Dispose()吗?而不是在使用了对象之后?
  • 我能不麻烦把它留给垃圾收集器吗?(没有using,没有Dispose())
  • 上面的代码是返回像List<string^>^这样的对象的正确方法吗?从 C++/CLI 到 C#?
  • 是否使用 gcnew这并不意味着垃圾收集器会处理这些对象,而我不必关心如何以及何时释放它们?

我知道有很多问题,但我只想摆脱内存泄漏,所以我列出了我认为可能出错的所有内容...

最佳答案

is there anything wrong with my code?

不是在您发布的内容中 - 您正在正确应用 using 语句。所以您的代码示例不是内存泄漏的原因。

do I have to use using in my C# code? It makes the code ugly, when there are multiple wrapper objects in use, because the using statements have to be nested

您不必这样做,但您不必在语法上嵌套它们。这是等价的:

Bitmap bmpimg = ...
using (MatW img = new MatW(bmpimg))
using (PeopleDetectorW peopleDetector = new PeopleDetector())
{
List<string> people = peopleDetector.detect(img);
}

could I use Dispose() instead after having used the objects?

你可以,但是你需要一个 try/finally 来确保 Dispose 总是被调用,即使在抛出异常时也是如此. using 语句封装了整个模式。

Could I just not bother and leave it to the garbage collector? (no using, no Dispose())

C++ RAII 通常应用于各种临时状态清理,包括递减在构造函数中递增的计数器等。而 GC 在后台线程中运行。它不适用于 RAII 可以处理的所有确定性清理场景。 RAII 的 CLR 等效项是 IDisposable,它的 C# 语言接口(interface)是 using。在 C++ 中,它的语言接口(interface)(自然地)是析构函数(成为 Dispose 方法)和 delete 运算符(成为对 Dispose). “在堆栈上”声明的 Ref 类对象实际上等同于 C# using 模式。

is the above code the right way to return objects like List^ from C++/CLI to C#?

差不多吧!

does using gcnew not mean that the garbage collector will take care of the objects and I don't have to care how and when to free them?

您不必释放内存。但是,如果该类实现了 IDisposable,那么这就是与内存分配完全不同的问题。在放弃对象之前,您必须手动调用 Dispose

当心终结器 - 这是一种 Hook 到 GC 的方式,让您自己的清理代码在 GC 收集您的对象时运行。但它们并不真正适合应用程序代码中的一般用途。它们从您无法控制的线程运行,在您无法控制的时间以您无法控制的顺序运行。因此,如果一个对象的终结器试图访问另一个带有终结器的对象,则第二个对象可能已经被终结。无法控制这些事件的顺序。现在 SafeHandle 涵盖了终结器的大部分原始用途。 .

关于c# - C++/CLI 代码中的内存泄漏。我做错了什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12701177/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com