0
我從NSThread
派生的類:NSThread對象保留兩次?
@interface FSEventMonitorThread : NSThread {
FSEventStreamRef m_fseStreamRef;
CFRunLoopRef m_runLoop;
}
- (id) initWithStream:
(FSEventStreamRef)fseStreamRef;
- (void) dealloc;
- (void) main;
@end
@implementation FSEventMonitorThread
- (id) initWithStream:
(FSEventStreamRef)fseStreamRef
{
if (self = [super init])
m_fseStreamRef = fseStreamRef;
return self;
}
- (void) dealloc
{
CFRunLoopStop(m_runLoop);
FSEventStreamStop(m_fseStreamRef);
[super dealloc];
}
- (void) main
{
m_runLoop = CFRunLoopGetCurrent();
FSEventStreamScheduleWithRunLoop(
m_fseStreamRef, m_runLoop, kCFRunLoopDefaultMode
);
FSEventStreamStart(m_fseStreamRef);
CFRunLoopRun();
}
@end
在其他地方(一個C++函數內),我創建一個實例:
m_thread = [[FSEventMonitorThread alloc] initWithStream:m_fseStreamRef];
我的理解是,保留數現在應該是1。 在另一個C++函數,我想停下來,解除分配線程:
[m_thread release];
然而dealloc
方法不被調用。如果我不是這樣做:
[m_thread release];
[m_thread release];
然後dealloc
被稱爲這意味着該保留數是2,但它是怎麼得到是2?
請注意,當使用detachNewThreadSelector:toTarget:withObject:
時,NSThread
的文檔僅提及保留。
CFRunLoopRun()之前,不會返回CFRunLoopStop()被調用;因此,我的main()無法檢查isCancelled。我覆蓋了取消方法來調用CFRunLoopStop()。所以現在停止線程,我正在做[m_thread取消],然後是[m_thread發佈],並且工作。問題:重寫取消方法是一件好事嗎?文件沒有說不應該。 – 2009-12-08 19:04:30
這取決於你在做什麼,但總的來說,我會說不,不這樣做。最簡單的方法是添加一個像finishThread這樣的方法;我會在我的答案中添加一個快速示例。在CFRunLoopRun()函數返回後,您還應該執行所需的清理。 – 2009-12-08 19:36:48