区别 SAFE_RELEASE(), SAFE_DELETE()

发布于 2024-11-17 11:21:22 字数 116 浏览 0 评论 0原文

我是编程新手。

我想知道释放和删除功能。

当我用new分配内存时,我应该用delete终止它。

但是什么时候应该使用释放呢?

释放和删除有什么区别...?

I'm newbie in programming.

I'm wondering release and delete function.

When I allocate memory with new, I should terminate it with delete.

But when I should use release?

What's the difference release and delete...?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

扶醉桌前 2024-11-24 11:21:22

如果您正在寻找定义,您会发现

#define SAFE_RELEASE(p) { if ( (p) ) { (p)->Release(); (p) = 0; } }
#define SAFE_DELETE(a) if( (a) != NULL ) delete (a); (a) = NULL;

SAFE_DELETE 应该用于用 new

SAFE_RELEASE 分配的内存,应该为 com 对象(如 directx 对象)调用 SAFE_RELEASE ,并且“幕后”正在执行类似的操作,

if (--ref_cnt==0)
{
   delete this;
}

它会减少引用计数器并释放对象如果没有更多的参考资料。

If you are looking for definition you will find

#define SAFE_RELEASE(p) { if ( (p) ) { (p)->Release(); (p) = 0; } }
#define SAFE_DELETE(a) if( (a) != NULL ) delete (a); (a) = NULL;

SAFE_DELETE should be used for memory allocated with new

SAFE_RELEASE should be called for com objects (like directx objects) and "under the hood" is doind something like this

if (--ref_cnt==0)
{
   delete this;
}

it decrements a reference counter and releases the object if there are no more references to it.

风流物 2024-11-24 11:21:22

c++ 没有发布版本,也许您在涉及 COM+ 的教程中看到过?喜欢 DirectX 吗?

当然,SAFE_RELEASESAFE_DELETE 实际上并不是 C++ 的一部分,很可能是在某些头文件中定义的宏。

无论如何,规则如下:

  • delete[]new[]
  • deletenew
  • free () 你的malloc()/calloc()/realloc()

c++ has no release, perhaps you saw that in a tutorial involving COM+? Like DirectX?

Certainly, SAFE_RELEASE and SAFE_DELETE are not actually part of c++ and are likely to be macros defined in some header file.

Anyway, here's the rules:

  • delete[] what you new[]
  • delete what you new
  • free() what you malloc()/calloc()/realloc()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文