How can I debug this deadlock on release of AVAudioPlayer?

In my iPhone app I have an instance of AVAudioPlayer which I release (so that I can then initialize another one with a different URL in its place). Normally this works fine, but occasionally I get a deadlock on the main thread:

Thread 1, Queue : com.apple.main-thread
#0  0x357e00d8 in __psynch_mutexwait ()
#1  0x3497d67a in pthread_mutex_lock ()
#2  0x359997a6 in -[AVAudioPlayer privCommonCleanup] ()
#3  0x35999bb6 in -[AVAudioPlayer dealloc] ()
#4  0x3338a174 in _objc_rootRelease ()

The app just stalls, accepting no input. No crash or anything, it just stops.

It doesn't seem to be interacting with any of the other threads in the app, but for completeness here they are:

Thread 2, Queue : com.apple.libdispatch-manager
#0  0x357d03a8 in kevent ()
#1  0x33b03eaa in _dispatch_mgr_invoke ()
#2  0x33b03bc8 in _dispatch_mgr_thread ()
Thread 3, Queue : (null)
#0  0x357e0cd4 in __workq_kernreturn ()
#1  0x34983f3c in _pthread_wqthread ()
#2  0x34983cd0 in start_wqthread ()
Thread 4 WebThread, Queue : (null)
#0  0x357d0004 in mach_msg_trap ()
#1  0x357d0200 in mach_msg ()
#2  0x3573e3f2 in __CFRunLoopServiceMachPort ()
#3  0x3573d12a in __CFRunLoopRun ()
#4  0x356c04a4 in CFRunLoopRunSpecific ()
#5  0x356c036c in CFRunLoopRunInMode ()
#6  0x37406ca2 in _ZL12RunWebThreadPv ()
#7  0x34989734 in _pthread_start ()
#8  0x349895f0 in thread_start ()
Thread 5, Queue : (null)
#0  0x357e0cd4 in __workq_kernreturn ()
#1  0x34983f3c in _pthread_wqthread ()
#2  0x34983cd0 in start_wqthread ()
Thread 6 AQClient, Queue : (null)
#0  0x357d0004 in mach_msg_trap ()
#1  0x357d0200 in mach_msg ()
#2  0x3573e3f2 in __CFRunLoopServiceMachPort ()
#3  0x3573d12a in __CFRunLoopRun ()
#4  0x356c04a4 in CFRunLoopRunSpecific ()
#5  0x356c036c in CFRunLoopRunInMode ()
#6  0x34e2cb2e in GenericRunLoopThread::Entry(void*) ()
#7  0x34db3ca2 in CAPThread::Entry(CAPThread*) ()
#8  0x34989734 in _pthread_start ()
#9  0x349895f0 in thread_start ()
Thread 7, Queue : (null)
#0  0x357d0004 in mach_msg_trap ()
#1  0x357d0200 in mach_msg ()
#2  0x3573e3f2 in __CFRunLoopServiceMachPort ()
#3  0x3573d12a in __CFRunLoopRun ()
#4  0x356c04a4 in CFRunLoopRunSpecific ()
#5  0x356c036c in CFRunLoopRunInMode ()
#6  0x34af9b74 in -[NSRunLoop(NSRunLoop) runMode:beforeDate:] ()
#7  0x34b13522 in -[NSRunLoop(NSRunLoop) run] ()
#8  0x000c83fe in +[AFURLConnectionOperation networkRequestThreadEntryPoint:] at /Users/hiltonc/Documents/Code/LDSMobileApps/LDSScriptureHeroes/External/LDSMobileAccount/External/AFNetworking/AFNetworking/AFURLConnectionOperation.m:151
#9  0x34b05a80 in -[NSThread main] ()
Thread 8 com.apple.NSURLConnectionLoader, Queue : (null)
#0  0x357d0004 in mach_msg_trap ()
#1  0x357d0200 in mach_msg ()
#2  0x3573e3f2 in __CFRunLoopServiceMachPort ()
#3  0x3573d12a in __CFRunLoopRun ()
#4  0x356c04a4 in CFRunLoopRunSpecific ()
#5  0x356c036c in CFRunLoopRunInMode ()
#6  0x34b05bb8 in +[NSURLConnection(Loader) _resourceLoadLoop:] ()
#7  0x34b05a80 in -[NSThread main] ()
Thread 12 com.apple.CFSocket.private, Queue : (null)
#0  0x357e0570 in select$DARWIN_EXTSN ()
#1  0x35742640 in __CFSocketManager ()
#2  0x34989734 in _pthread_start ()
#3  0x349895f0 in thread_start ()

I really have no idea how to get to the bottom of this issue. Any ideas?

Answers


Releasing one and replacing it in a delegate call will surely cause this type of problem. Esentially you want to run (under ARC):

avPlayer.delegate = nil
[avPlayer stop];
avPlayer = nil; //overkill
avPlayer = [AVPlayer alloc] init....
avPlayer ....

If you find you need to do the above in a delegate method, then just wrap the above code in a block and dispatch it to the main queue (I'm assuming avPlayer is an ivar or property).


Need Your Help

how to preserve newline character in a string after converting to bytes?

java string stream byte bytearray

String str[] = {"1000458551||A210171046D86F9F6EE21B66FE9B1441E20EC1DEF9654A2D092162591C01D26F||1||7707||0||"

How to convert a JPEG2000 to JPEG in Python?

python image-processing jpeg2000

I have a JPEG 2000 Image, and want to convert it to a simple JPEG image, so that it will be visible in all browsers.