Cocoanetics / DTFoundation

Standard toolset classes and categories
BSD 2-Clause "Simplified" License
802 stars 237 forks source link

App crashes when repeatedly starting/stopping DTFolderMonitor #56

Closed sebromero closed 10 years ago

sebromero commented 10 years ago

When i use start/stop the folder monitor more then once the app will crash because the file descriptor is closed when stopMonitoring is called. So when calling startMonitoring and stopMonitoring again the filedescriptor is invalid which will lead to a crash inside the cancel handler.

sebromero commented 10 years ago

I suggest to only open the filedescriptor in the startMonitoring method:

- (void)startMonitoring
{
    @synchronized(self)
    {
        if (_source)
        {
            return;
        }

        _fileDescriptor = open([_URL.path fileSystemRepresentation], O_EVTONLY);

        if (!_fileDescriptor) {
            return;
        }

        // watch the file descriptor for writes
        _source = dispatch_source_create(DISPATCH_SOURCE_TYPE_VNODE, _fileDescriptor, DISPATCH_VNODE_WRITE, _queue);

        // call the passed block if the source is modified
        dispatch_source_set_event_handler(_source, _block);

        // close the file descriptor when the dispatch source is cancelled
        dispatch_source_set_cancel_handler(_source, ^{

            close(_fileDescriptor);
        });

        // at this point the dispatch source is paused, so start watching
        dispatch_resume(_source);
    }
}
odrobnik commented 10 years ago

Please send a pull request

Von meinem iPhone gesendet

Am 01.05.2014 um 13:59 schrieb Sebastian notifications@github.com:

I suggest to only open the filedescriptor in the startMonitoring method:

  • (void)startMonitoring { @synchronized(self) { if (_source) { return; }

    _fileDescriptor = open([_URL.path fileSystemRepresentation], O_EVTONLY);
    
    if (!_fileDescriptor) {
      return;
    }
    
    // watch the file descriptor for writes
    _source = dispatch_source_create(DISPATCH_SOURCE_TYPE_VNODE, _fileDescriptor, DISPATCH_VNODE_WRITE, _queue);
    
    // call the passed block if the source is modified
    dispatch_source_set_event_handler(_source, _block);
    
    // close the file descriptor when the dispatch source is cancelled
    dispatch_source_set_cancel_handler(_source, ^{
    
      close(_fileDescriptor);
    });
    
    // at this point the dispatch source is paused, so start watching
    dispatch_resume(_source);

    } } — Reply to this email directly or view it on GitHub.

odrobnik commented 10 years ago

Merged the fix into develop, will release soon.