I have a class derived from 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
Elsewhere (inside a C++ function), I create an instance:
m_thread = [[FSEventMonitorThread alloc] initWithStream:m_fseStreamRef];
My understanding is that the retain-count should now be 1. In another C++ function, I want to stop and deallocate the thread:
[m_thread release];
Yet the dealloc
method is not called. If I instead do:
[m_thread release];
[m_thread release];
then dealloc
is called which implies the retain-count was 2. But how did it get to be 2?
Note that the documentation for NSThread
only mentions retaining when using detachNewThreadSelector:toTarget:withObject:
.