5

UPDATE: This bug is confirmed by rdar://20931915 and is fixed in Xcode 7 beta 3.


I found a weird bug caused by calling a swift closure in another closure in debug build. My Xcode is version 6.3.1 with Swift version 1.2. Here's the code:

import Swift

class ClosureStackOverflow {
    private var b: Bool = false
    private func callClosure1(callback: Void -> Void) {
        println("in closure 1")
        callback()
    }

    private func callClosure2(callback: Void -> Void) {
        println("in closure 2")
        callback()
    }

    func call() {
        callClosure1 { [weak self] in
            self?.callClosure2 {
                self?.b = true
            }
        }
    }
}

let c = ClosureStackOverflow()
c.call()

The code above compiles well. However if you call its call() method, it will print "in closure 2" infinitely and eventually overflow the stack.

Could you please explain why calling one closure within another will cause this bug?

Thanks.

6
  • I've posted a gist also.
    – hankbao
    May 13, 2015 at 4:47
  • @MartinR I can reproduce the problem. I am using Apple Swift version 1.2 (swiftlang-602.0.49.6 clang-602.0.49) May 13, 2015 at 4:53
  • 1
    In Release mode, the program prints "in closure 1", "in closure 2" and then terminates. In Debug mode, the described "infinite" recursion and stack overflow occurs. – Also it occurs only with a weak reference to self, not with strong or unowned. Could be a compiler bug.
    – Martin R
    May 13, 2015 at 4:58
  • @MartinR Thanks for pointing out the difference between the release build and the debug build.
    – hankbao
    May 13, 2015 at 7:29
  • That is a cool bug. Have you raised a radar for it? Its the only way it will get fixed. :-) May 13, 2015 at 8:49

1 Answer 1

2

Change your code to this,and it will work

    class ClosureStackOverflow {
    private var b: Bool = false
    private func callClosure1(callback: Void -> Void) {
        println("in closure 1")
        callback()
    }

    private func callClosure2(callback: Void -> Void) {
        println("in closure 2")

        callback()
    }

    func call() {
        callClosure1 {
            self.callClosure2 {
                self.b = true
            }
        }
    }
    deinit{
        print("deinit")
    }
}

It seems that you declare [weak self] in in the function,and it cause the problem.

I also test this to call

 let c = ClosureStackOverflow()
    c.call()

It will output

 in closure 1
in closure 2
deinit

It seems that it does not cause circular references if you donot use weak self

Besides I also test to change the function to this

  func call() {
    callClosure1 {
        [weak self] in
        self!.callClosure2 {
            self?.b = true
        }
    }
}

It will work as well. So I think this may be some compiler bug of swift.

5
  • Did you try it? Your code terminates with E_BAD_ACCESS in my test.
    – Martin R
    May 13, 2015 at 5:05
  • Hey @WenchenHuang, thanks for your post. Actually I know how to get workaround about this issue. I'm just curious about the cause and asking for a explanation. Thank you anyway.
    – hankbao
    May 13, 2015 at 7:23
  • I think that this is an compiler bug. I watch the call tree.it just fall in a run loop and never get out. It make no sense
    – Leo
    May 13, 2015 at 7:25
  • Set breakpoints in both callClosure1 and callClosure2 then you can see the callback in callClosure2 has the same address as the callback in callClosure1. Weird.
    – hankbao
    May 13, 2015 at 7:37
  • Add println("all done") after self?.b = true in the original code and the problem vanishes also. Very odd May 13, 2015 at 8:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.