Just had a weird crash in development in Simulator - first of a kind. The crash was on this line:
private lazy var regExTrimSet = NSCharacterSet(charactersInString: " \t\r\n_")
and the error was some malloc() already released (grrr, should have copied it). Should that be thread safe? [Its being referenced by a bunch of concurrent blocks at one point]
Also, I tried to find out where the Xcode console log might get written, but had no luck finding a link. Is there one that this error should be logged to?
Some notes added to clarify this situation in the recent Swift book.
In the Lazy Stored Property part of Stored Properties:
If a property marked with the
lazy
modifier is accessed by multiple threads simultaneously and the property has not yet been initialized, there is no guarantee that the property will be initialized only once.In the Type Properties section:
Stored type properties are lazily initialized on their first access. They are guaranteed to be initialized only once, even when accessed by multiple threads simultaneously, and they do not need to be marked with the
lazy
modifier.It seems not all `lazy` variables are guaranteed to be initialized once in multi-threaded environment. You need to explicitly program it so or use static property.
And as already noted, all Cocoa/Cocoa Touch classes and methods are not thread-safe, except ones explicitly declared as thread-safe in the documentations. Immutable classes are strongly expected to be thread-safe, but it is not guaranteed.