Strange crash with multithreading and -[NSUserDefaults synchronize]
Strange crash with multithreading and -[NSUserDefaults synchronize]
- Subject: Strange crash with multithreading and -[NSUserDefaults synchronize]
- From: Martin Redington <email@hidden>
- Date: Sun, 12 Nov 2006 17:10:52 +0000
I'm seeing strange crashes with multiple worker threads writing to
NSUserDefaults.
My worker thread sends a network request, and then processes the
response. As it iterates over the elements of the response, it writes
to NSUserDefaults, and calls synchronize for each element.
This works fine when only one worker thread is running. When multiple
worker threads are running, I get EXC_BAD_ACCESS crashes in one of
the worker thread quite frequently. The stack trace is
0 objc_msgSend
1 ??
2 [NSRunLoop runMode:beforeDate:]
3 [PrivateClass performWorkerThreadSelector:]
(the core method for the worker threads calls [NSRunLoop
runMode:beforeDate:] within a loop)
I've stripped the code down as much as possible, to eliminate
possible sources for the crash. A call to -[NSUserDefaults
synchronize] is sufficient to cause it - its not necessary to
actually write. This call takes place within a @synchronized block,
and I've confirmed in the debugger that the worker threads do block
each other out appropriately. The synchronize call itself does not
crash.
I've tried calling synchronize on the main thread, and blocking the
worker thread until it returns, but I still see the crashes, albeit
less frequently.
If I call synchronize without any thread-safety measures (i.e.
outside a synchronized block), I see crashes, but with a different
stack trace, and often somewhere during the popping of the
AutoRelease pool.
Its hard to believe that synchronize could be responsible for this,
but when I remove that call, the crashes go away.
I've seen various previous threads which discuss the thread-safety,
or not, of NSUserDefaults - I'm not so sure that they're directly
relevant here, given that we still get the crashes when the
NSUserDefaults calls are synchronized, or performed on the main thread.
If anyone has seen something like this before, or has any tips on
working out, from the debugger, what object its trying to access,
that would be wonderful.
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden