jbsf / blindside

Blindside provides dependency injection capabilities for Objective-C on iOS and OS X
MIT License
71 stars 43 forks source link

fix BSSingleton.source access with multi-thread #56

Open RetVal opened 8 years ago

RetVal commented 8 years ago

dispatch_apply(1000, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(size_t idx) { [inject getInstance:[TestClass class]]; // which bind to BSSingleton scope, because BSSingleton's source is nonatomic, will crash inside [scope scope:provider], the BSSingleton BSScope protocol implement. });

baek-jinoo commented 7 years ago

We are seeing a bug where multiple background thread activities are causing a crash with the stack trace as seen below. Can we get someone to talk about this fix for the ones that involve the [BSSinglton scope:] call? With regards to inFlightKeys I am wondering if it's related to the Thread specific dictionary. Let me know what you think.

screen shot 2016-10-21 at 12 20 19 pm screen shot 2016-10-21 at 12 21 27 pm screen shot 2016-10-21 at 12 22 07 pm screen shot 2016-10-21 at 12 23 17 pm
RetVal commented 7 years ago

the BSSingleton scope, because BSSingleton's source is nonatomic, [scope scope:provider] will try to use the source of BSSingleton, step is

  1. retain it
  2. use it
  3. set the source to nil
  4. release it

But in the multi-thread environment, [singleton scope:] will set the singleton's source property, [singleton provide:injector:] will reset the source to nil if instance is nil, and the source property is not marked as atomic, so getter and setter will case the multi-thread problem.