removeFilePresenter is never called on ARC

Originator:graeter
Number:rdar://16301054 Date Originated:12-Mar-2014
Status:Open Resolved:
Product:OS X Product Version:10.9.2
Classification:Other Bug Reproducible:Always
 
- Register a file presenter using [NSFileCoordinator addFilePresenter: self]
- the documentation states that on ARC and Garbage Collection, -removeFilePresenter must not be called
- it states that -removeFilePresenter is automatically performed if the presenter is deallocated

However, this seems not to be true, as the following example shows:

- The presenter is created inside an @autorelease pool
- A weak reference of the presenter is kept outside the autorelease
- The code waits until the weak reference is set to nil
- This never happens...

Please fix the documentation or the implementation.

-----
@interface MyPresenter : NSObject <NSFilePresenter>
{
	NSURL *_url;
	NSOperationQueue *_queue;
}

- (id)initWithURL:(NSURL *)url;

@end

@implementation MyPresenter

- (id)initWithURL:(NSURL *)url
{
	self = [self init];
	
	if (self) {
		_url = url;
		_queue = [NSOperationQueue new];
		_queue.maxConcurrentOperationCount = 1;
		
		// According to documentation, -addFilePresenter should not require a remove. This is done automatically for ARC...
		[NSFileCoordinator addFilePresenter: self];
	}
	
	return self;
}

- (NSURL *)presentedItemURL
{
	return _url;
}

- (NSOperationQueue *)presentedItemOperationQueue
{
	return _queue;
}

@end



int main(int argc, const char * argv[])
{
	@autoreleasepool {
		__weak MyPresenter *weakPresenter;
		
		@autoreleasepool {
			NSURL *url =  [NSURL fileURLWithPath: @"test.txt"];
			[@"abc" writeToURL:url atomically:YES encoding:NSUTF8StringEncoding error:NULL];

			// Create and register a presenter.
			MyPresenter *presenter = [[MyPresenter alloc] initWithURL: url];
			weakPresenter = presenter;
			
			// The presenter should be released when leaving the autoreleasepool. Seems not to happen.
		}
	    
		while(1) {
			// The presenter should be released eventually. However, this never happens
			if (weakPresenter == nil)
				break;
			
			[[NSRunLoop mainRunLoop] runUntilDate: [NSDate dateWithTimeIntervalSinceNow: 0.001]];
		}
		
	}
    return 0;
}

Comments


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!