Quickblox: I am receiving EXC_BAD_ACCESS(code=1, address"mem-address") when joining/leaving chat rooms

216 views Asked by At

Hey so the title gives some information, but let me expand further. In my iOS application, I am receiving this EXC_BAD_ACCESS message when I am joining and leaving chat rooms quickly (by selecting a thread in a UITableView and view is getting pushed to a new UIViewController and then popping back to the UITableViewController). I am trying to fix this problem because it is more prevalent with slower network connections, and I want to avoid crashes. I use the Quickblox Chat service based on the ChatService.h/.m files that are in the sample-chat xcode project. Here is the only modified code:

- (void)chatRoomDidEnter:(QBChatRoom *)room{
    if (self.joinRoomCompletionBlock) {
        self.joinRoomCompletionBlock(room);
        self.joinRoomCompletionBlock = nil;
    }

    [[NSNotificationCenter defaultCenter] postNotificationName:@"kNotificationChatRoomDidEnter"
                                                    object:nil userInfo:nil];
}

The crash happens at the end of this method and when I turned on NSZombies, I got this error message:

*** -[QBChatRoom retain]: message sent to deallocated instance 0x15e6bbe0

I am actually calling these methods from that ChatService in my UIViewController's viewWillAppear/Disappear methods (written in Swift):

override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)
    self.chatRoom = self.dialog.chatRoom
    ChatService.instance().joinRoom(self.chatRoom, completionBlock: { (joinedChatRoom : QBChatRoom!) -> Void in
        //joined
    })
}

override func viewWillDisappear(animated: Bool)  {
    super.viewWillDisappear(animated)

    let notificationCenter = NSNotificationCenter.defaultCenter()
    notificationCenter.removeObserver(self);

    if (self.chatRoom != nil) {
        ChatService.instance().leaveRoom(self.chatRoom);
        self.chatRoom = nil
    }
}

Thanks in advance for any insight with this issue.

1

There are 1 answers

1
Walt Sellers On

I have no experience with Swift yet, but I would guess that

ChatService.instance().joinRoom(self.chatRoom, completionBlock:{...})

is asynchronous (thus the completion block) and is not retaining self.chatRoom.

When things are quick as you describe, viewWillDisappear is called first and self.chatRoom becomes nil. Later, the completion block is called and "room" (or self.chatRoom) attempts to retain, but it is too late.

self.chatRoom is released before the completion block is called and thus you have your zombie.

Is there an abort call of some kind in ChatService?