objective-c - 非 GC 应用程序中的内存管理 OS X __block 变量分配

标签 objective-c macos cocoa memory-management objective-c-blocks

我有一个非 gc OS X 应用程序。在此应用程序中,我尝试从 block 内将对象分配给 block 变量。然后,另一个线程会从其所在的数组中清除该对象。我假设因为它是一个 non-gc 应用程序,所以 block_byref 结构正在设置 BLOCK_NEEDS_FREE 标志,并且 dispose 辅助函数正在清理向上的对象。副本是否足够安全?

- (void)assignFromArray
{
  __block NSObject iWantToKeep = nil;

  [myArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop)
  {
    if([[obj stringVar] isEqualToString:MyStringConst])
    {
       iWantToKeep = [obj copy];
       *stop = YES;
    }
  }];

/* Assume here that the array has been cleaned up by another thread
 * and all the objects in it have been released.
 *
 * Was a copy safe enough to survive the block_byref dispose
 * and the array objects being dealloc'd so that it can be accessed here?
 */
   NSLog(@"%@", [iWantToKeep stringVar]);

  //I only need it briefly, so it can be cleaned up here
  [iWantToKeep autorelease];
}

最佳答案

是的,效果很好。您只需确保在丢弃数组之前进行复制即可。

关于objective-c - 非 GC 应用程序中的内存管理 OS X __block 变量分配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13910705/

相关文章:

ios - 将图像上传到服务器 Ios。我有 uiimageview 并想将 imageview 图像发送到服务器以实现 objective-c

python - 无法从 Docker 容器内部连接到 Redis

html - OSX - WebView youtube html5 视频出现错误

xml - 在 Cocoa 中使用 xsd 文件验证 XML 模式?

iphone - 多级 nsmutabledictionary 的设置值

ios - 自定义 UITableViewCell,重新排序时删除初始 View

java - Apache Tomcat 服务器不显示在 Intellij Idea 14 Ultimate 的 doGet() 方法中创建的 HTML

c - 在 Mac El Capitan 上安装 gcc

macos - 在商业原生 Cocoa 应用程序中使用 Google Web Fonts?

ios - UIViewController 在模式呈现后更改尺寸