{"action":"created","issue":{"url":"https://api.github.com/repos/realm/realm-cocoa/issues/1734","labels_url":"https://api.github.com/repos/realm/realm-cocoa/issues/1734/labels{/name}","comments_url":"https://api.github.com/repos/realm/realm-cocoa/issues/1734/comments","events_url":"https://api.github.com/repos/realm/realm-cocoa/issues/1734/events","html_url":"https://github.com/realm/realm-cocoa/issues/1734","id":66703495,"number":1734,"title":"EXC_BAD_ACCESS crash when using NSOutlineView","user":{"login":"enricenrich","id":357265,"avatar_url":"https://avatars.githubusercontent.com/u/357265?v=3","gravatar_id":"","url":"https://api.github.com/users/enricenrich","html_url":"https://github.com/enricenrich","followers_url":"https://api.github.com/users/enricenrich/followers","following_url":"https://api.github.com/users/enricenrich/following{/other_user}","gists_url":"https://api.github.com/users/enricenrich/gists{/gist_id}","starred_url":"https://api.github.com/users/enricenrich/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/enricenrich/subscriptions","organizations_url":"https://api.github.com/users/enricenrich/orgs","repos_url":"https://api.github.com/users/enricenrich/repos","events_url":"https://api.github.com/users/enricenrich/events{/privacy}","received_events_url":"https://api.github.com/users/enricenrich/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":4,"created_at":"2015-04-06T20:55:49Z","updated_at":"2015-11-23T19:56:43Z","closed_at":"2015-05-01T20:02:29Z","body":"I'm creating an NSOutlineView populated by objects saved on Realm. The object is called `Person`. And this is how it looks:\r\n\r\n```objc\r\n@interface Person : RLMObject\r\n\r\n@property NSString *name;\r\n@property NSInteger indent;\r\n@property NSInteger order;\r\n\r\n// Ignored properties\r\n@property NSArray *contacts;\r\n```\r\n\r\nThe `contacts ` array returns a query. Those `contacts` are child of the `person`, but they also have the same object class, `Person`.\r\n\r\nAnd this is the view controller:\r\n\r\n```objc\r\n#import \"MainWindowController.h\"\r\n#import \"Person.h\"\r\n\r\n@interface MainWindowController () <NSOutlineViewDataSource, NSOutlineViewDelegate>\r\n\r\n@property (strong) IBOutlet NSOutlineView *outlineView;\r\n\r\n@property (nonatomic, strong) RLMResults *array;\r\n@property (nonatomic, strong) RLMNotificationToken *notification;\r\n\r\n@end\r\n\r\n@implementation MainWindowController\r\n\r\n- (id)init\r\n{\r\n    self = [super initWithWindowNibName:@\"MainWindow\"];\r\n    \r\n    if (self) {\r\n        __weak typeof(self) weakSelf = self;\r\n        \r\n        self.notification = [RLMRealm.defaultRealm addNotificationBlock:^(NSString *note, RLMRealm *realm) {\r\n            [weakSelf.outlineView reloadData];\r\n        }];\r\n        \r\n        self.array = [[Person objectsWhere:@\"indent == 1\"] sortedResultsUsingProperty:@\"order\" ascending:YES];\r\n    }\r\n    \r\n    return self;\r\n}\r\n\r\n- (void)windowDidLoad\r\n{\r\n    [super windowDidLoad];\r\n    \r\n    [self.outlineView setRowHeight:50];\r\n}\r\n\r\n#pragma mark - NSOutlineView\r\n\r\n- (id)outlineView:(NSOutlineView *)outlineView child:(NSInteger)index ofItem:(id)item\r\n{\r\n    if (item == nil) {\r\n        return self.array[index];\r\n    }\r\n    else if ([item isKindOfClass:[Person class]]) {\r\n        return [item contacts][index];\r\n    }\r\n    \r\n    return nil;\r\n}\r\n\r\n- (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item\r\n{\r\n    if (item == nil) {\r\n        return NO;\r\n    }\r\n    else if ([item isKindOfClass:[Person class]]) {\r\n        return [item contacts].count > 0;\r\n    }\r\n    \r\n    return NO;\r\n}\r\n\r\n- (NSInteger)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item\r\n{\r\n    if (item == nil) {\r\n        return self.array.count;\r\n    }\r\n    else if ([item isKindOfClass:[Person class]]) {\r\n        return [item contacts].count;\r\n    }\r\n    \r\n    return 0;\r\n}\r\n\r\n- (NSView *)outlineView:(NSOutlineView *)outlineView viewForTableColumn:(NSTableColumn *)tableColumn item:(id)item\r\n{\r\n    NSView *cellView = [[NSView alloc] initWithFrame:NSMakeRect(0, 0, 250, 0)];\r\n    \r\n    NSTextField *titleTextField = [[NSTextField alloc] initWithFrame:NSMakeRect(0, 15, 250, 18)];\r\n    [titleTextField setEditable:NO];\r\n    [titleTextField setBordered:NO];\r\n    titleTextField.backgroundColor = [NSColor clearColor];\r\n    titleTextField.textColor = [NSColor blackColor];\r\n    titleTextField.focusRingType = NSFocusRingTypeNone;\r\n    titleTextField.stringValue = [item name];\r\n    [cellView addSubview:titleTextField];\r\n    \r\n    return cellView;\r\n}\r\n```\r\n\r\nThe \"root\" cells are displayed correctly, but then, when I click the arrow to expand one of those root cells, the app crashes with an EXC_BAD_ACCESS error. I used NSZombie to know more about this crash, and that's what it tells me:\r\n\r\n`-[RLMAccessor_v0_Person retain]: message sent to deallocated instance 0x60800016ba00`\r\n\r\nI debugged all the data source methods of the NSOutlineView when tapping the arrow to expand the row, but no method is called.\r\n\r\nAny idea of what's happening?"},"comment":{"url":"https://api.github.com/repos/realm/realm-cocoa/issues/comments/159045547","html_url":"https://github.com/realm/realm-cocoa/issues/1734#issuecomment-159045547","issue_url":"https://api.github.com/repos/realm/realm-cocoa/issues/1734","id":159045547,"user":{"login":"lm2s","id":221489,"avatar_url":"https://avatars.githubusercontent.com/u/221489?v=3","gravatar_id":"","url":"https://api.github.com/users/lm2s","html_url":"https://github.com/lm2s","followers_url":"https://api.github.com/users/lm2s/followers","following_url":"https://api.github.com/users/lm2s/following{/other_user}","gists_url":"https://api.github.com/users/lm2s/gists{/gist_id}","starred_url":"https://api.github.com/users/lm2s/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/lm2s/subscriptions","organizations_url":"https://api.github.com/users/lm2s/orgs","repos_url":"https://api.github.com/users/lm2s/repos","events_url":"https://api.github.com/users/lm2s/events{/privacy}","received_events_url":"https://api.github.com/users/lm2s/received_events","type":"User","site_admin":false},"created_at":"2015-11-23T19:56:43Z","updated_at":"2015-11-23T19:56:43Z","body":"For future reference of anyone having this problem, like I did.\r\n\r\nInstead of using the RLMResults array directly in \"- (id)outlineView:(NSOutlineView *)outlineView child:(NSInteger)index ofItem:(id)item\".\r\n\r\nCopy the objects into a NSArray to make sure that they are not released and then use the NSArray."}}