Skip to content

Commit

Permalink
Fix circular reference between CBLUnsavedRevision and CBLAttachment
Browse files Browse the repository at this point in the history
- As CBLUnsavedRevision keeps CBLAttachment objects in an array so setting the revision (self) object to the CBLAttachment objects will cause circular reference memory leak.

- Instead of setting the revision (self) object when adding a CBLAttachment to the CBLUnsavedRevision object, overiding the attachmentNamed: method and creating a new CBLAttachment object and adding a revision object there.
  • Loading branch information
pasin committed Apr 13, 2015
1 parent bbd3b72 commit a3f6388
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Source/API/CBLAttachment.m
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ - (instancetype) _initWithContentType: (NSString*)contentType
}


@synthesize revision=_rev, name=_name, metadata=_metadata;
@synthesize revision=_rev, name=_name, body = _body, metadata=_metadata;


- (NSString *)description
Expand Down
18 changes: 17 additions & 1 deletion Source/API/CBLRevision.m
Original file line number Diff line number Diff line change
Expand Up @@ -367,12 +367,28 @@ - (void) _addAttachment: (CBLAttachment*)attachment named: (NSString*)name {
[atts setValue: attachment forKey: name];
_properties[@"_attachments"] = atts;
attachment.name = name;
attachment.revision = self;

// NOTE: Not setting the revision to the attachment object (attachment.revision = self) as
// [1] The UnsavedRevision object is not used during save operation
// [2] Setting the UnsavedRevision object here will cause the circular reference memory leak.
// The revision object will be set in the overridden attachmentName: method.
}

- (void) removeAttachmentNamed: (NSString*)name {
[self _addAttachment: nil named: name];
}

- (CBLAttachment*) attachmentNamed: (NSString*)name {
CBLAttachment* base = self.attachmentMetadata[name];
if (base) {
CBLAttachment* attachment = [[CBLAttachment alloc] _initWithContentType: base.contentType
body: base.body];
attachment.name = name;
attachment.revision = self;
return attachment;
}
return nil;
}


@end
1 change: 1 addition & 0 deletions Source/API/CouchbaseLitePrivate.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@
intoDatabase: (CBLDatabase*)database __attribute__((nonnull(2)));
@property (readwrite, copy) NSString* name;
@property (readwrite, retain) CBLRevision* revision;
@property (readonly) id body;
@end


Expand Down

0 comments on commit a3f6388

Please sign in to comment.