我有一个非 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/