gpt4 book ai didi

c++ - 正确使用@autoreleasepool

转载 作者:太空宇宙 更新时间:2023-11-04 13:20:47 24 4
gpt4 key购买 nike

我在我的代码中混合了一些 ObjectiveC 和 C++。 (我的文件是 .mm 文件,而不是 .m。)什么时候才是用 @autoreleasepool 包装任何代码块的正确理由?不知道我在做什么,我正在包装任何可能包含任何非 ObjectiveC 变量的代码块,无论它是 intcharstd::string,或非 ObjectiveC 变量的任何指针。所以,我的 C++ 类中的每个类方法都有一个 @autoreleasepool 包装器在里面。

我做错了吗?

Note that on previous questions sort of related to this question, they talk about using alloc, init, and release, and these seem to be deprecated now in XCode7+. So, I need the latest advice on this, not old advice. I need advice pertaining to XCode7 or greater.

最佳答案

自动释放池用于限制其中autoreleased 的事物的生命周期。 autorelease 是 Cocoa Objective-C API,因此只有 Objective-C 代码可以autorelease 东西。因此,将自动释放池放在您确定永远不会调用到 Objective-C 代码的纯 C/C++ 代码块周围是没有任何意义的。

自动释放池通常仅适用于循环运行多次且每次迭代可能执行大量autorelease 的情况。请注意,并非所有 Objective-C 代码都会autorelease;这并不明显。一些 Cocoa API 将 autorelease 而一些则不会。如果都是你自己用ARC写的代码,很可能不会autorelease

关于c++ - 正确使用@autoreleasepool,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35373183/

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