Skip to content
This repository has been archived by the owner on Oct 5, 2022. It is now read-only.

Passing nil as a dispatch block fails on newest version of Xcode #15

Closed
Danappelxx opened this issue Oct 26, 2015 · 9 comments
Closed

Comments

@Danappelxx
Copy link
Contributor

The following lines of code don't build on the newest version of Xcode:

guard let asyncData = dispatch_data_create(buffer,bufsize, queue,nil) else {
    return false
}

with the following error:

ActiveSocket.swift:311:70: Nil is not compatible with expected argument type 'dispatch_block_t' (aka '@convention(block) () -> ()')

I'll submit a pull request which changes nil to {} (an empty block), which should work.

@helje5
Copy link
Owner

helje5 commented Oct 26, 2015

This is with Xcode 7.1? I guess we need a branch for Swift 0.2.1.

@Danappelxx
Copy link
Contributor Author

Perhaps, but that would mess things up with Carthage & Cocoapods. This fix shouldn't break any old version of Swift since it's only changing nil to be an empty closure, so I don't see why creating a new branch would be necessary in this case.

@helje5
Copy link
Owner

helje5 commented Oct 27, 2015

Well, passing in NULL aka DISPATCH_DATA_DESTRUCTOR_DEFAULT is the intended thing here. That nil cannot be used is likely a bug in Swift (well, in the tagging of the API). Need to review this.

@helje5
Copy link
Owner

helje5 commented Oct 27, 2015

Passing in {} is wrong and quite likely crashes the program. I'll test it with 7.1b and file a Radar with Apple later on.

@helje5
Copy link
Owner

helje5 commented Oct 27, 2015

Works fine for me as-is in Xcode 7.1 (7B91b). What version do you use?

@helje5 helje5 closed this as completed Oct 27, 2015
@Danappelxx
Copy link
Contributor Author

I'm on Xcode 7.1 Beta (7B75). Could you explain why passing {} will cause it to crash? Could you give me an example which should crash it so that I can test it? Thanks.

@Danappelxx
Copy link
Contributor Author

I'm downloading Xcode 7.2 Beta (just released today I think) and test passing nil there. If the issue persists I think this issue should be reopened.

@helje5
Copy link
Owner

helje5 commented Oct 27, 2015

You might want to upgrade to the 7.1 release.

Passing in DISPATCH_DATA_DESTRUCTOR_DEFAULT (nil) tells GCD to copy the memory buffer being passed in (and maintain it internally).

The buffer being passed into the asyncWrite() function is temporary (conceptually on the stack) and will be gone eventually. If you pass a free-handler-block to GCD (instead of nil) you guarantee that the ptr being passed in will stay valid until that free-block is called. Which is wrong here, as the stack buffer will disappear or become corrupt.

As with all async stuff it can be hard to repro. The buffer may still be kinda valid and just have a few corruptions etc.

P.S.: This is just another reason why SwiftSockets is not really for production but more of a demo. Production software should use GCD channels and not copy any memory.

@Danappelxx
Copy link
Contributor Author

Thanks for explaining that! Issue disappeared on newest version of Xcode (7.2 beta), so I guess this should remain closed.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants