3

I did a quick testing and am trying to pass a closure as function argument, but notice some kind of "exception". The app is not crashing but the offending line generating a crash report.

ViewController

import UIKit

class MapViewController: UIViewController {

    typealias MapTouchHandler = (String) -> Void

    var mapTouchHandlers = Array<MapTouchHandler>()

    @IBAction func tapGestureAction(_ sender: UITapGestureRecognizer) {
            for handler in mapTouchHandlers {

                // This line produces this: "0x000000010c8f8a00 Mediator`partial apply forwarder for reabstraction thunk helper from @callee_owned (@in Swift.String) -> (@out ()) to @callee_owned (@owned Swift.String) -> () at MapViewController.swift"

                handler("tap recognized")
            }

    }

    override func viewDidLoad() {
        super.viewDidLoad()
    }



     func subscribeMapTouchEvent(mapTouchHandler: @escaping MapTouchHandler) {

           // This line produce this: "0x000000010c8ff910 Mediator`partial apply forwarder with unmangled suffix ".16" at Mediator.swift"

            mapTouchHandlers.append(mapTouchHandler)
    }
}

Mediator class

import UIKit

class Mediator {

    var mapViewController: MapViewController? {
        didSet {
            mapViewController?.subscribeMapTouchEvent(mapTouchHandler: self.handleTouchEvent(str:))
        }
    }

    private func handleTouchEvent(str: String) {
        print(str)
    }
}

Appreciate any insight to solve this issue.

Andi Setiyadi
  • 197
  • 1
  • 1
  • 11
  • Does the app crash or not? – Martin R Nov 09 '17 at 19:12
  • The app does not crash – Andi Setiyadi Nov 09 '17 at 19:22
  • Then what does "offending line generating a crash report" mean? Is it a compiler error/warning, a runtime error/warning, or what? – Martin R Nov 09 '17 at 19:23
  • I am a bit lost as well, app is not crashing but in hockey app it is producing a crash report: `Thread 0 Crashed: partial apply forwarder for reabstraction thunk helper from @callee_owned (@unowned __C.CGPoint, @owned [__ObjC.MGLFeature]) -> () to @callee_owned (@in (__C.CGPoint, [__ObjC.MGLFeature])) -> (@out ()) (MapViewController.swift:0)` – Andi Setiyadi Nov 09 '17 at 19:33
  • If I put breakpoint on `mapTouchHandlers.append(mapTouchHandler)`, in debugger I can see `mapTouchHandler = (() -> () 0x000000010ee7a910 Mediator partial apply forwarder with unmangled suffix ".16" at Mediator.swift` . And breakpoint on `handler("tap recognized")` shows `handler = (() -> ()) 0x000000010ee73a00 Mediator partial apply forwarder for reabstraction thunk helper from @callee_owned (@in Swift.String) -> (@out ()) to @callee_owned (@owned Swift.String) -> () at MapViewController.swift` – Andi Setiyadi Nov 09 '17 at 19:40

2 Answers2

3

If you are referring "self" in a block i recommend you to use weak self, thus you can avoid retain cycle, memory leak and crash.

Example,

getAsynchronousData { [weak self] data in
    guard let self = self else { return }

    self.yourProperty = data 
} 
Jeba Moses
  • 809
  • 8
  • 25
  • 1
    According to this post https://stackoverflow.com/a/41992442/2610888 and this https://medium.com/@abhimuralidharan/functional-programming-closure-reference-cycle-and-fix-f42cc53c6213 you do not need to add the capture list with weak self when using DispatchQueue.main.async as no retain cycles are created – Andrea Gorrieri Mar 12 '19 at 10:37
  • 1
    this example is misleading about capturing self within a block because you shouldn't be cautious about `self` in dispatch queues. – gokhanakkurt Apr 14 '20 at 15:25
3

Checkout this Q&A from Apple Developer Forum. Tips for you: find the variable that maybe nil and have something to do with Objective-C code. I fixed the bug in mine. Good luck!

wzso
  • 3,482
  • 5
  • 27
  • 48