objective-c - RetainCount 可以在这种情况下使用吗?

标签 objective-c ios nsthread retaincount

保留计数 == 坏

retainCount 是禁忌的、不可靠的、不可预测的,一般不应该使用。我没有在我的代码中的任何地方使用它,但我在一个类中以一种有趣的方式使用过它。

我有一个运行线程的类,该线程无限期运行直到线程被取消。问题是线程增加了所有者的保留计数,在我的例子中是实例化它的类。因此,即使我不再使用该类,该实例仍将继续存在,除非管理我的类的人也有智慧知道关闭线程。这是一种解决方案,但这是我在代码中找到的。

- (oneway void)release
{
    // This override allows allows this object to be dealloced 
    // by shutting down the thread when the thread holds the last reference.
    // Otherwise, the object will never be dealloc'd
    if (self.retainCount == 2)
    {
        [self quitDispatchThread];
    }

    [super release];
}

这是一个聪明的解决方案,但我不确定该怎么想。它覆盖类上的释放并检查保留计数是否为 2。换句话说,它检查线程是否是唯一让我的对象保持事件状态的东西(因为保留计数大约是从 2 递减到 1),如果是,则终止线程(quitDispatchThread 将阻塞,直到线程终止)。

所以...

能不能靠retainCount看是不是一个?

通常人们说要远离 retainCount,因为您不知道那里是否有一些自动释放。但是,如果 retainCount 是一个,那么我知道只有线程保持事件状态并且我不必担心 retainCount 可能由于某些自动释放等而关闭......

这段代码有什么问题?

我正要去掉它,但它实际上似乎是有道理的。其他对象不必知道我的类正在运行线程。其他对象可以安全地 retainrelease 甚至 autorelease 拥有线程的对象,而不必担心关闭线程,因为它负责本身。

这段代码实际上感觉很干净,这让我很惊讶。

编辑::NSThread 正在保留我的对象

由于我使用了 NSThread,我的对象的保留计数增加了。我的对象是 targetselector 是线程运行的方法。

initWithTarget:selector:object:

Returns an NSThread object initialized with the given arguments.

  • (id)initWithTarget:(id)target selector:(SEL)selector object:(id)argument

Parameters

target

The object to which the message specified by selector is sent.

selector

The selector for the message to send to target. This selector must take only one argument and must not have a return value.

argument

The single argument passed to the target. May be nil.

Return Value

An NSThread object initialized with the given arguments.

Discussion

For non garbage-collected applications, the method selector is responsible for setting up an autorelease pool for the newly detached thread and freeing that pool before it exits. Garbage-collected applications do not need to create an autorelease pool.

The objects target and argument are retained during the execution of the detached thread. They are released when the thread finally exits.

最佳答案

retainCount is taboo, unreliable, unpredictable, and in general shouldn't be used.

您可以依赖 retainCount 的值 IFF 您的对象不会通过任何对您来说不透明的代码,例如任何 Cocoa 框架。实际上,这几乎是不可能实现的,因此发出警告。 Cocoa 的内部可能会出于多种原因多次传递您的对象、保留、释放并将其放入自动释放池中,并且您不能在任何给定点依赖它的绝对值。

The catch is that the thread increases the retain count of the owner, in my case the class that instantiated it.

这是一个保留周期。这里的答案是想办法打破那个循环,而不是颠覆引用计数机制。当您的线程或拥有对象知道线程正在执行的工作已完成(或需要提前停止)时,在释放之前必须有某个点

听起来拥有对象是客户端代码与线程正在执行的工作的接口(interface)。此拥有对象需要一个“立即关闭”方法,该方法需要在 所有者释放它之前调用(并记录为“必须调用”)。在该关闭方法中,您可以通过释放线程来打破循环。

我不确定线程​​保留其创建者的原因是什么(循环非常清楚地表明您的所有权模型某事有问题)——我猜你正在使用 NSThreadinitWithTarget:...,目标是创建/拥有对象。这有点混淆了标准 MVC 模式——线程的所有者是一个“ Controller ”,线程本身(以及它运行的代码)更像是一个“模型”。

换句话说, Controller 不应该包含线程的代码。我建议您将线程的代码提取到另一个对象中以用作目标。然后 Controller 对象同时拥有线程本身和“工作”目标对象,它们都不拥有 Controller 。瞧,没有循环!

关于objective-c - RetainCount 可以在这种情况下使用吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10032014/

相关文章:

ios - 如何根据声明顺序对 UITableView header 进行排序

iOS swift 3.0本地JSON解析崩溃

ios - 将文本永久附加到 UITextView

iphone - 在后台调用函数和在线程中调用函数的区别

objective-c - 从 UITextView 获取准确的光标位置

ios - 将 UIImage 圆角化但使圆的外部透明

iphone - "Redirect URI doesnot match registered redirect URI"在 IOS 应用程序中的 instagram 集成

IOS:在后台单击时删除 View

iphone - iPhone SDK中如何实现NSThread在后台不断执行功能

iphone - 如何退出 NSThread