I have a strange bug in my own program. I'm currently working on Video Editing app. I have a SongPicker view controller, which displays all the songs from the user's music app. When the user selects a song, a new object (MediaAsset) representing that song is created. It worked perfectly fine, when SongPicker was written in Swift, and MediaAsset in Objective-C. However I rewrited MediaAsset completely on Swift and now every time I'm trying to create new MediaAsset from SongPicker, Xcode throwes EXC_BREAKPOINT (code=1, subcode=...) and my app crushes.
Here is the code, that is called when user selects song:
private let mediaItems = MPMediaQuery.songsQuery().items as [MPMediaItem]
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath){
let item = filteredMediaItems[indexPath.row]
println("didSelectRowAtIndexPath")
println("item: \(item), url: \(item.assetURL.absoluteString)")
delegate?.songPickerViewController(self, didPickedAsset: MediaAsset(url: item.assetURL, type: .Audio))
}
It actually generates output to console:
didSelectRowAtIndexPath
item: <MPConcreteMediaItem: 0x174648340> 2369259457983598523, url: Optional("ipod-library://item/item.mp3?id=2369259457983598523")
then it goes to MediaAsset constructor, which looks like this:
init(url: NSURL, type: MediaAssetType){
println("new MediaAsset with url \(url.absoluteString)")
self.url = url
self.asset = AVURLAsset(URL: url, options: [AVURLAssetPreferPreciseDurationAndTimingKey: true])
self.timeRange = CMTimeRangeMake(kCMTimeZero, self.asset.duration)
self.initialRate = CGFloat(max(self.asset.videoTrack!.nominalFrameRate / 30.0, 1.0))
self.rate = self.initialRate
self.type = type
}
it prints to console:
new MediaAsset with url Optional("ipod-library://item/item.mp3?id=2369259457983598523")
And on this line it crushes:
The code that creates asset from AVURLAsset is exactly the same as it was in my old ObjectiveC class (where it worked perfectly well), so the problem shouldn't belong to AVFoundation. Does anybody knows what can be reason of that crush? And as more general question, in which cases "EXC_BREAKPOINT (code=1, ..." arises?
EDIT After deleting this println statement, my app still crushes but now shows this assembler code:
This exception happens when the value being unwrapped by the '!' operator is nil. Fix the nil value and the code should work.
I found an answer. Error was in self.asset.videoTrack!.nominalFrameRate
, because in case of an audio asset.videoTrack
will be nil, that's why app crashes.
Just don't know why it behaves so strangely, pointing an error at println()
line. Must be one of tons of Xcode's swift
related bugs
This error might also be caused if you dispatch something on the main queue synchronously inside an asynchronous block, that happens to run on the main queue as well:
dispatch_async(dispatch_get_main_queue(), ^{ // This might happen unintentionally.
dispatch_sync(dispatch_get_main_queue(), ^{
// Do stuff.
});
});
In this case your code will run into a deadlock caused by the fact that the asynchronous block won't complete until the synchronous block will complete, and it won't start until the asynchronous block complete.
I faced the same problem, i tried Shift+Cmd+K to clean the project and it fixed the problem, Although i don't know why that happened !
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With