How to debug iOS EXC_BAD_ACCESS KERN_INVALID_ADDRESS on iOS

9.6k views Asked by At

I'm getting iOS EXC_BAD_ACCESS errors while using SocketRocket, and I wonder what I can do to further debug the issue to determine if the problem is on my side, or on SocketRocket's side.

The stacktrace I get is :

Crashed: com.apple.main-thread
EXC_BAD_ACCESS KERN_INVALID_ADDRESS at 0x2000000c
raw
0    libobjc.A.dylib     objc_msgSend + 5
1    OMlearnings         SRWebSocket.m line 692    __30-[SRWebSocket _failWithError:]_block_invoke_2
2    libdispatch.dylib   _dispatch_call_block_and_release + 10
10   UIKit               UIApplicationMain + 1136
11   OMlearnings         main.m line 16    main

or sometimes

Crashed: NSOperationQueue Serial Queue
EXC_BAD_ACCESS KERN_INVALID_ADDRESS at 0xc
raw
0    libobjc.A.dylib     objc_msgSend + 5
1    OMlearnings         SRWebSocket.m line 613    -[SRWebSocket scheduleInRunLoop:forMode:]
2    OMlearnings         SRWebSocket.m line 600    -[SRWebSocket _connect]
3    OMlearnings         OMSRealTimeTeamDashboard.m line 157    -[OMSRealTimeTeamDashboard sendMessage:]
4    OMlearnings         OMSRealTimeTeamDashboard.m line 171    -[OMSRealTimeTeamDashboard eventReceived:]
5    CoreFoundation      __invoking___ + 68
6    CoreFoundation      -[NSInvocation invoke] + 282
7    Foundation          -[NSInvocationOperation main] + 112
8    Foundation          -[__NSOperationInternal _start:] + 770
14   libsystem_pthread.dylib _pthread_wqthread + 298

My codebase is quite simple and basically subscribes to events, and executes socketrocket sendMessage in a queue (to deal with concurrency)

[signalServices subscribe:my-event toBlock:^(NSNotification * notification) {
    [this.queue addOperation:[[NSInvocationOperation alloc] initWithTarget:self selector:@selector(eventReceived:) object:notification]];
}];

- (void)eventReceived: (NSNotification *)notification {
    // ...
    [socket send:[NSString stringWithFormat:@"%i,1,%@", currentUserId.intValue, [NSNumber numberWithInt: rate.value]]];
}

I've read people using NSZombies to debug the issue, but my problem only happens rarely, so I would likely run out of memory before the problem becomes visible. It works fine 99% of the time.

Is there anything to know about iOS that could randomly crash apps that use sockets, etc ? For instance, we have background fetch enabled, could that be the cause of some of the random crashes ?

Thanks !

1

There are 1 answers

1
Kazuki Sakamoto On

In the stacktrace,

0    libobjc.A.dylib     objc_msgSend + 5
1    OMlearnings         SRWebSocket.m line 692    __30-[SRWebSocket _failWithError:]_block_invoke_2

EXC_BAD_ACCESS occurred in this line, https://github.com/square/SocketRocket/blob/master/SocketRocket/SRWebSocket.m#L692

[self.delegate webSocket:self didFailWithError:error];

According to the document, https://github.com/square/SocketRocket

SRWebSocket
(unlike NSURLConnection, SRWebSocket won't retain the delegate)

@property (nonatomic, assign) id <SRWebSocketDelegate> delegate;

the delegate property wasn't retained by SRWebSocket object, thus, if you don't retain the delegate object, it will crash with the dangling pointer. How do you have strong reference for the delegate object?