Getting EXC_BAD_ACCESS (SIGBUS) on iOS

We are facing iOS crash issue in our one SDK library written in objective-c. This objective-c library is being invoked from swift app developed by our customer.



Following is the code snippet from our sdk, where we are creating multiple threads:-



//Create a queue to collect attributes in parallel

dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0);

//Create group to wait on the task scheduled in global queue

dispatch_group_t group = dispatch_group_create();

// Collect all information1

dispatch_group_async(group, queue, ^{

info1 = [self getInformation1];

});

// Collect all information2

dispatch_group_async(group, queue, ^{

info2 = [self getInformation2];

});

// Collect all information3

dispatch_group_async(group, queue, ^{

info3 = [self getInformation3];

});

// Collect all information4

dispatch_group_async(group, queue, ^{

info4 = [self getInformation4];

});

// Collect all information5

dispatch_group_async(group, queue, ^{

info5 = [self getInformation5];

});



// wait on the group to block the current thread.

int64_t time_delay = 3.0;

dispatch_time_t timeout = dispatch_time(DISPATCH_TIME_NOW, time_delay * NSEC_PER_SEC);

long success = dispatch_group_wait(group,timeout);

if(success != 0) {

//Log data to check which bucket is not complete

[Logger writeLogWithSeverity:kLogERROR message:info1];

[Logger writeLogWithSeverity:kLogERROR message:info2];

[Logger writeLogWithSeverity:kLogERROR message:info3];

[Logger writeLogWithSeverity:kLogERROR message:info4];

[Logger writeLogWithSeverity:kLogERROR message:info5];

}



Is this is ideal way or is there any better way to create concurrent threads?





Following are couple of stack traces of the crashes

===================================================



1)



Exception Type: EXC_BAD_ACCESS (SIGBUS)

Exception Codes: 0x00000000 at 0x0000000323f7bec8

Crashed Thread: 0



Thread 0 Crashed:

0 libobjc.A.dylib 0x000000018193c1a0 0x181918000 + 147872 (objc_retain + 16)

1 abcMobile 0x0000000104365cbc 0x1041c8000 + 1694908 -[DeviceInventory notifyCollectingdeviceInfo:] (DeviceInventory.m:215 + 28)

2 abcMobile 0x0000000104365b78 0x1041c8000 + 1694584 -[DeviceInventory startCollectingdeviceInfo] (DeviceInventory.m:206 + 0)

3 libdispatch.dylib 0x0000000182058b24 0x182057000 + 6948 (<redacted> + 24)

4 libdispatch.dylib 0x0000000182058ae4 0x182057000 + 6884 (<redacted> + 16)

5 libdispatch.dylib 0x00000001820656e0 0x182057000 + 59104 (<redacted> + 1012)

6 CoreFoundation 0x000000018270f070 0x182621000 + 974960 (<redacted> + 12)

7 CoreFoundation 0x000000018270cbc8 0x182621000 + 965576 (<redacted> + 2272)

8 CoreFoundation 0x000000018262cda8 0x182621000 + 48552 (CFRunLoopRunSpecific + 552)

9 GraphicsServices 0x000000018460f020 0x184604000 + 45088 (GSEventRunModal + 100)

10 UIKit 0x000000018c60d78c 0x18c2f0000 + 3266444 (UIApplicationMain + 236)

11 abcMobile 0x00000001041ccc40 0x1041c8000 + 19520 main (main.swift:14 + 20)

12 libdyld.dylib 0x00000001820bdfc0 0x1820bd000 + 4032 (<redacted> + 4)





Crashing code :- [self.delegate didCompletedCollectingInformation:deviceInfo];



didCompletedCollectingInformation is a call-back function



2)



Exception Type: EXC_BAD_ACCESS (SIGBUS)

Exception Codes: 0x00000000 at 0x00000007943dbec8

Crashed Thread: 18



Thread 18 Crashed:

0 libobjc.A.dylib 0x0000000182f3c1a0 0x182f18000 + 147872 (objc_retain + 16)

1 abcMobile 0x0000000100c0150c 0x100a64000 + 1692940 -[DeviceInventory didFailWithError:] (DeviceInventory.m:144 + 28)

2 CoreFoundation 0x0000000183cf8c3c 0x183c21000 + 883772 (<redacted> + 20)

3 CoreFoundation 0x0000000183cf81b8 0x183c21000 + 881080 (<redacted> + 428)

4 CoreFoundation 0x0000000183cf7f14 0x183c21000 + 880404 (<redacted> + 216)

5 CoreFoundation 0x0000000183d7584c 0x183c21000 + 1394764 (<redacted> + 1408)

6 CoreFoundation 0x0000000183c2ef38 0x183c21000 + 57144 (_CFXNotificationPost + 384)

7 Foundation 0x000000018469fbbc 0x184699000 + 27580 (<redacted> + 68)

8 abcMobile 0x0000000100bf7f80 0x100a64000 + 1654656 +[Errors sendErrorNotifactionForErrorCode:] (Errors.m:405 + 64)

9 abcMobile 0x0000000100bf101c 0x100a64000 + 1626140 +[NetworkInfo information1] (NetworkInfo.m:711 + 0)

10 abcMobile 0x0000000100bfb9fc 0x100a64000 + 1669628 -[Services information1] (Services.m:391 + 20)

11 abcMobile 0x0000000100c058b4 0x100a64000 + 1710260 -[DeviceInventory getInformation1] (DeviceInventory.m:635 + 56)

12 abcMobile 0x0000000100c0268c 0x100a64000 + 1697420 __39-[DeviceInventory getdeviceInfo]_block_invoke.163 (DeviceInventory.m:256 + 16)

13 libdispatch.dylib 0x0000000183658aa0 0x183657000 + 6816 (<redacted> + 24)

14 libdispatch.dylib 0x0000000183658a60 0x183657000 + 6752 (<redacted> + 16)

15 libdispatch.dylib 0x000000018369a5f0 0x183657000 + 275952 (<redacted> + 1148)

16 libdispatch.dylib 0x000000018369a110 0x183657000 + 274704 (<redacted> + 112)

17 libsystem_pthread.dylib 0x000000018398bfac 0x18398b000 + 4012 (_pthread_wqthread + 1176)





Crashing code :- if([self.delegate respondsToSelector:@selector(didFailWithError:)])



Here, didFailWithError is a call-back function





We are unable to find the root cause of this crash as it is very random issue. Does anyone have any idea how to fix this crash issue?

Any pointers will be very helpful as we are struck on this issue.

Replies

Is this is ideal way or is there any better way to create concurrent threads?

That very much depends on what these threads are doing. If they are CPU bound then the approach I’d normally recommend for spreading work across available CPU is

dispatch_apply
(see its man page for details). If they are I/O bound then this things get more complex.

As to the cause of your crash, it’s hard to offer specifics with the info you’ve posted so far, but in my experience most crashes in

objc_retain
are caused by memory management problems, and thus I’d recommend that you start with the Standard Memory Debugging Tools.

Of particular concern would by the

delegate
you reference in
-didFailWithError:
. Most delegates are either
unsafe_unretained
(aka
assign
) or
weak
. A crash calling a delegate is an indication that it might be the former, and that the object has been deallocated out from underneath the caller. Using
weak
can help with problems like that.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"