Skip to content

Commit ada10e5

Browse files
committed
Avoid exception when closing a repository window
It seems to be caused by PBGitHistoryList holding a weak reference to PBGitRepository but also observing it via KVO which may lead to the PBGitRepository object to be deallocated before the KVO-observer is removed. Attempt to solve this by implementing explicit setters in PBGitHistory which also call the update actions that were previously triggered by KVO. If the atomicity of those fields was crucial before, this may require a bit more work.
1 parent 9162b30 commit ada10e5

File tree

3 files changed

+19
-21
lines changed

3 files changed

+19
-21
lines changed

Classes/git/PBGitHistoryList.m

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,6 @@ - (id) initWithRepository:(PBGitRepository *)repo
5151
commits = [NSMutableArray array];
5252
repository = repo;
5353
lastBranchFilter = -1;
54-
[repository addObserver:self forKeyPath:@"currentBranch" options:0 context:@"currentBranch"];
55-
[repository addObserver:self forKeyPath:@"currentBranchFilter" options:0 context:@"currentBranch"];
56-
[repository addObserver:self forKeyPath:@"hasChanged" options:0 context:@"repositoryHasChanged"];
5754

5855
shouldReloadProjectHistory = YES;
5956
projectRevList = [[PBGitRevList alloc] initWithRepository:repository rev:[PBGitRevSpecifier allBranchesRevSpec] shouldGraph:NO];
@@ -96,9 +93,6 @@ - (void)cleanup
9693
}
9794
[graphQueue cancelAllOperations];
9895

99-
[repository removeObserver:self forKeyPath:@"currentBranch"];
100-
[repository removeObserver:self forKeyPath:@"currentBranchFilter"];
101-
[repository removeObserver:self forKeyPath:@"hasChanged"];
10296
}
10397

10498

@@ -350,19 +344,8 @@ - (void) updateHistoryForRev:(PBGitRevSpecifier *)rev
350344

351345
#pragma mark -
352346
#pragma mark Key Value Observing
353-
354347
- (void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
355348
{
356-
if ([@"currentBranch" isEqualToString:(__bridge NSString*)context]) {
357-
[self updateHistory];
358-
return;
359-
}
360-
361-
if ([@"repositoryHasChanged" isEqualToString:(__bridge NSString*)context]) {
362-
[self forceUpdate];
363-
return;
364-
}
365-
366349
if ([@"commitsUpdated" isEqualToString:(__bridge NSString*)context]) {
367350
NSInteger changeKind = [(NSNumber *)[change objectForKey:NSKeyValueChangeKindKey] intValue];
368351
if (changeKind == NSKeyValueChangeInsertion) {

Classes/git/PBGitRepository.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ static NSString * PBStringFromBranchFilterType(PBGitXBranchFilterType type) {
5050

5151
@interface PBGitRepository : NSDocument
5252

53-
@property (assign) BOOL hasChanged;
54-
@property (assign) NSInteger currentBranchFilter;
53+
@property (nonatomic, assign) BOOL hasChanged;
54+
@property (nonatomic, assign) NSInteger currentBranchFilter;
5555

5656
@property (readonly, strong) PBGitWindowController *windowController;
5757
@property (readonly, getter = getIndexURL) NSURL* indexURL;

Classes/git/PBGitRepository.m

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,7 @@ @interface PBGitRepository () {
4040

4141
@implementation PBGitRepository
4242

43-
@synthesize revisionList, branchesSet, currentBranch, refs, hasChanged;
44-
@synthesize currentBranchFilter;
43+
@synthesize revisionList, branchesSet, currentBranch, currentBranchFilter, hasChanged, refs;
4544

4645
#pragma mark -
4746
#pragma mark Memory management
@@ -478,6 +477,22 @@ - (void) readCurrentBranch
478477
self.currentBranch = [self addBranch: [self headRef]];
479478
}
480479

480+
- (void) setCurrentBranch:(PBGitRevSpecifier *)newCurrentBranch {
481+
currentBranch = newCurrentBranch;
482+
[revisionList updateHistory];
483+
}
484+
485+
- (void) setCurrentBranchFilter:(NSInteger)newCurrentBranchFilter {
486+
currentBranchFilter = newCurrentBranchFilter;
487+
[revisionList updateHistory];
488+
}
489+
490+
- (void) setHasChanged:(BOOL)newHasChanged {
491+
hasChanged = newHasChanged;
492+
[revisionList forceUpdate];
493+
}
494+
495+
481496
#pragma mark Stashes
482497

483498
- (NSArray *)stashes

0 commit comments

Comments
 (0)