gcnew需要删除吗? [英] gcnew requires Delete?

查看:390
本文介绍了gcnew需要删除吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我从Google那里得到不同答案时,这个问题始终会铭记在心.

还有,
如果我的变量是返回类型,该如何删除?

例如,假设我有一个C ++/Cli函数,该函数返回字符串,并且还具有引用类型字符串参数.我必须从C#调用此函数

This question always remains in mind as I got mixed answers from Google.

And also,
How would I delete if my variable is a return type?

For example, say I have a C++/Cli function which returns string and also has reference type string parameter. I have to call this function from C#

String^ GetStrings( [Out] String^ %outputstring)
{
    String^ result = gcnew(/*  C++ function that returns char*  */);
    outputstring = gcnew(/*  C++ function that returns char*  */);
    return result;
}


这会导致内存泄漏吗?
在C#中返回这些字符串对象后,如何删除用gcnew创建的字符串对象?

在此先感谢


Will this cause a memory leak?
How would I go deleting these string object that I created with gcnew after I return these in C#?

Thanks in advance

推荐答案

不,永远不要.这是.NET托管的内存分配,对象由Garbage Collector(GC)销毁并回收.

内存泄漏很可能发生,但不是因为您忘记删除托管空间中的任何内容. (但是,很可能会发生非托管内存和资源的泄漏;通常在Dispose方法中进行清理(但是可以在其他任何位置);如果忘记处理,则会发生泄漏.) >
应用程序永远不会回收或处置托管内存.它是这样工作的:当Application Domain松开对对象的引用时,该对象被标记为要破坏的对象.但是,它不会立即发生.确切的时间由内部GC算法确定.因此,任何依赖于调用析构函数的时间和顺序的代码都是无效的.出于同样的原因,析构函数很少使用. CG机制足够智能.例如,它处理循环关联:如果对象A引用对象B,对象B引用对象C,而对象C引用对象A,则如果当前运行的代码无法访问所有三个对象,则所有此结构都将被销毁.
—SA
No, never. This is .NET managed memory allocation, the object are destructed and memory reclaimed by the Garbage Collector (GC).

A memory leak is quite possible but not because you forgot to delete anything in a managed space. (However, a leak of unmanaged memory and resources is quite possible; clean-up is is usually placed in Dispose methods (but can be anywhere else); if you forget to dispose, there will be a leak.)

Managed memory is never reclaimed or disposed by the application. Here is how it works: when you Application Domain looses a reference to an object, the object is marked as a subject to destruction. However, it does not happen immediately; exact moment of time is determined by internal GC algorithm. By that reason, any code depending on the time and order of calling destructors is invalid. By the same reason, destructors are used quite rarely. CG mechanism is intellectual enough. For example, it handles cyclic associations: if object A references object B, object B references object C and object C references object A, all this structure is destructed anyway if currently running code cannot access all three objects.
—SA


正如 SAKryukov 正确解释的那样,您不得明确删除使用gcnew分配的内容.但是,返回char *C++函数"可能要求删除其返回值所指向的内存.例如,考虑:
As SAKryukov correctly explained you must NOT explicitely delete what you allocated with gcnew. However the ''C++ function that returns char *'' MIGHT require the deletion of the memory pointed by its return value. For instance, consider:
char * str;
str = new char[3];
sprintf(str, "hi");
String ^ result = gcnew (p);
delete [] str;


在上面的代码中,delete [] str;语句是必需的(否则...内存泄漏!),当然,您绝对不能删除result.


in the above code the delete [] str; statement is required (otherwise... memory leaks!), while you MUST NOT delete result, of course.


这篇关于gcnew需要删除吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆