Skip to content

Commit

Permalink
fix crash calling nonexistent delegate (also memory leak)
Browse files Browse the repository at this point in the history
  • Loading branch information
gobbledegook committed Dec 25, 2023
1 parent a0bb2b6 commit e05fb38
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
2 changes: 2 additions & 0 deletions VDKQueue.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
//
// VDKQueue is also simplified. The option to use it as a singleton is removed. You simply alloc/init an instance and add paths you want to
// watch. Your objects can be alerted to changes either by notifications or by a delegate method (or both). See below.
// When you're done with the object, call stopWatching (to release the background thread), then release.
//
// It also fixes several bugs. For one, it won't crash if it can't create a file descriptor to a file you ask it to watch. (By default, an OS X process can only
// have about 3,000 file descriptors open at once. If you hit that limit, UKKQueue will crash. VDKQueue will not.)
Expand Down Expand Up @@ -139,6 +140,7 @@ extern NSString * VDKQueueAccessRevocationNotification;
- (void) removePath:(NSString *)aPath;
- (void) removeAllPaths;

- (void) stopWatching; // You must call this when you release the VDKQueue object

- (NSUInteger) numberOfWatchedPaths; // Returns the number of paths that this VDKQueue instance is actively watching.

Expand Down
22 changes: 16 additions & 6 deletions VDKQueue.m
Original file line number Diff line number Diff line change
Expand Up @@ -141,12 +141,6 @@ - (id) init

- (void) dealloc
{
// Shut down the thread that's scanning for kQueue events
_keepWatcherThreadRunning = NO;

// Do this to close all the open file descriptors for files we're watching
[self removeAllPaths];

[_watchedPathEntries release];
_watchedPathEntries = nil;

Expand Down Expand Up @@ -422,6 +416,22 @@ - (void) removeAllPaths
}


- (void) stopWatching
{
// This method must be called if we want this object to ever be dealloc'd. This is because detachNewThreadSelector:toTarget:withObject:
// retains the target (in this case, self), setting the retainCount to 2. Aside from the memory leak issue, if the thread is never terminated,
// the watcher thread might still try to send messages to a nonexistent delegate, causing your app to crash.
@synchronized(self)
{
// Shut down the thread that's scanning for kQueue events
_keepWatcherThreadRunning = NO;

// Do this to close all the open file descriptors for files we're watching
[_watchedPathEntries removeAllObjects];
}
}


- (NSUInteger) numberOfWatchedPaths
{
NSUInteger count;
Expand Down

0 comments on commit e05fb38

Please sign in to comment.