EXC_BAD_ACCESS (code=1, address=0x18) and -[NSTaggedPointerString objectForKey:]: unrecognized selector sent to instance 0x8000000000000000

84 views Asked by At

In Xcode, Swift language. I have a class (Class3) which contains only variables, not functions and other things:

class Class3 {

var x:Int = Int()

var y:String = String()

....

}

Then another class (Class2) which contains @Published variables and functions. Here the functions are performing calculations on the background thread, and modify the class3:Class3 variables. The last function of class2 takes all the class3 variables, and update the @Published variables from class2 with the coresponding class3 ones, on the main thread to update the UI.

Then another class (Class1) from where I appeal all the Class2 functions.

In Class1:


class Cass1:ObservableObject {
    func appeallingFunctions_fromClass2(class2:Class2//..//) {
        //..//
        var class3 = Class3()
        DispatchQueue.global().async {
            let operationQueue = OperationQueue()
            operationQueue.maxConcurrentOperationCount = 1
            
            let semaphore = DispatchSemaphore(value: 0)
            semaphore.signal()
            let operation1 = BlockOperation {
                semaphore.wait()
                class2.func1(semaphore: semaphore)
                
            }
            let operation2 ..
            ...
            let operation127..
            let operations:[BlockOperation] = [operation1,..operation127]
            for i in 1..<operations.count {
                operations[i].addDependency(operations[i-1])
            }
            
            for operation in operations {
                operationQueue.addOperation(operation)
            }
        }
    }
}

The problematic function inside Class2 (is inside operation45 inside Class1):

func func_with_error(class3: Class3,time:String, semaphore: DispatchSemaphore) { var period:Int = Int()
    
    let operationQueue = OperationQueue()
    operationQueue.maxConcurrentOperationCount = 1
    
    let operation1 = BlockOperation {
        if time == "1min" || time == "5min" || time == "15min" || time == "30min" || time == "1h" || time == "2h" || time == "4h" || time == "1day" {
            period = 450
        } else if time == "1month" || time == "1week" {
            period = 48 // 4 years for monthly and 1 year for weekly
        }
        for i in stride(from: period, to: -1, by: -1) {
            if class3.SOK[i] != nil {
                
                if class3.SO2.contains(class3.SOK[i]!) {
                    class3.SO.append(class3.SOK[i]!)
                }
                if class3.SO2.contains(class3.SOK[i]!) {
                    class3.SO.append(class3.SOK[i]!)
                }
                if class3.SO3.contains(class3.SOK[i]!) {
                    class3.SO.append(class3.SOK[i]!)
                }
                if class3.SO3.contains(class3.SOK[i]!) {
                    class3.SO.append(class3.SOK[i]!)
                }
            }
        }
    }
    
    let operation2 = BlockOperation {
        
        semaphore.signal()
        
    }
    
    operation2.addDependency(operation1) 
    
    operationQueue.addOperation(operation1)
    operationQueue.addOperation(operation2)
}

Same error it happes to another dictionary, inside a function similar to this one. Once it happends to be inside this function, other time to be in the another one. All the times the error line varies inside this function, inside "

if class3.SOK[i] != nil {

and everytime at one of the

append(class3.SOK[i]!), // highlighted at K[

The error is: Thread "X": EXC_BAD_ACCESS (code=1, address=0x18) ("X" -> the thread always differs)

and sometimes, i dont get this error but i get: Thread "X": "-[NSTaggedPointerString objectForKey:]: unrecognized selector sent to instance 0x8000000000000000" BUT not inside Class2, but at: libsystem_kernel.dylib`:
->  0x7ff83611d1f2 <+10>: jae    0x7ff83611d1fc            ; <+20>

Everytime I verify if the that I value is nil or not, so I can avoid to append a nil value. class3:Class3 is declared inside Class1 and put as parameter inside every Class2 function. I am sure that each function finishes its operations with class3 variables before another function starts using them, due to the semaphore and operations dependencies. And so I think the error is not related to accessing a deallocated object or multithreading issues.

I tried to debug, but everytime the error happens in different lines of the code inside that two functions, or with that second error.

I really have no ideea how to solve it. Any ideas? Thank you!

0

There are 0 answers