Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EXC_BAD_ACCESS crash detection

Tags:

arrays

ios

swift

I am getting this crash. I know how to detect these kind of crashes and dubugging with ThreadSanitiser enabled.

enter image description here

This is the information i am getting from ThreadSanitiser:

WARNING: ThreadSanitizer: race on NSMutableArray (pid=16866)
  Read-only access of NSMutableArray at 0x7b0c00065d60 by thread T11:
    #0 -[__NSArrayM count] <null>:2 (CoreFoundation:x86_64+0xeabf)
    #1 -[GMSx_GPBMessage mergeFromCodedInputStream:extensionRegistry:] <null>:2 (DummyProject:x86_64+0x10072c83a)
    #2 _dispatch_client_callout <null>:2 (libdispatch.dylib:x86_64+0x4a5a)

  Previous modifying access of NSMutableArray at 0x7b0c00065d60 by thread T1:
    #0 -[__NSArrayM addObject:] <null>:2 (CoreFoundation:x86_64+0xf5b9)
    #1 +[GMSx_GPBDescriptor allocDescriptorForClass:rootClass:file:fields:fieldCount:storageSize:flags:] <null>:2 (DummyProject:x86_64+0x1007029e3)
    #2 _dispatch_client_callout <null>:2 (libdispatch.dylib:x86_64+0x4a5a)

  Location is heap block of size 48 at 0x7b0c00065d60 allocated by thread T1:
    #0 calloc <null>:2 (libclang_rt.tsan_iossim_dynamic.dylib:x86_64+0x53564)
    #1 class_createInstance <null>:2 (libobjc.A.dylib:x86_64+0x17925)
    #2 _dispatch_client_callout <null>:2 (libdispatch.dylib:x86_64+0x4a5a)

  Thread T11 (tid=163564, running) is a GCD worker thread

  Thread T1 (tid=163545, running) is a GCD worker thread

SUMMARY: ThreadSanitizer: race on NSMutableArray (CoreFoundation:x86_64+0xeabf) in -[__NSArrayM count]+0x3c

I know it is a race condition but my concern is the line number where this is happening, I need to know exactly which array is causing this crash so that i can make it thread safe. Any help would be appreciable. Thanks

like image 770
Vikas saini Avatar asked Dec 21 '25 17:12

Vikas saini


1 Answers

You can swizzle this -[__NSArrayM addObject:] to get info about which kind of objects are those being added while threads collide, this was tested in a small project

extension NSMutableArray {
    static let swizzleAddObject: Void = {
        DispatchQueue.once(token: "NSMutableArray.initialize.swizzle") {
            let mutableArrayClass: AnyClass? = classFromString("__NSArrayM")
            let originalSelector = Selector(("addObject:"))
            let swizzledSelector = #selector(swizzledAddObject(_:))
            guard let originalMethod = class_getInstanceMethod(mutableArrayClass.self, originalSelector),
                  let swizzledMethod = class_getInstanceMethod(mutableArrayClass.self, swizzledSelector)
            else {
                debugPrint("Error while swizzling")
                return
            }
            method_exchangeImplementations(originalMethod, swizzledMethod)
        }
    }()

    @objc
    private func swizzledAddObject(_ object: Any) {
        debugPrint("\(object)")
        swizzledAddObject(object)
    }
    
    static func classFromString(_ className: String) -> AnyClass! {
        /// get 'anyClass' with classname and namespace
        let cls: AnyClass = NSClassFromString("\(className)")!

        // return AnyClass!
        return cls
    }
}

implementation in DispatchQueue.once gist

then in AppDelegate

import UIKit

@main
class AppDelegate: UIResponder, UIApplicationDelegate {

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        NSMutableArray.swizzleAddObject
        // Override point for customization after application launch.
        return true
    }

hope this helps you, btw I have an article about this medium article maybe can be helpful for you.

As you can see we have all objects added in any NSMutableArray, be careful using this, and should be used under your own responsability ;) enter image description here

like image 62
Reinier Melian Avatar answered Dec 23 '25 06:12

Reinier Melian



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!