CrossGeeks / GeofencePlugin

Geofence Plugin for Xamarin iOS and Android
MIT License
48 stars 22 forks source link

issue #24 fix for OnConnected also issue #23 for accessing Regions #26

Closed jasonmerecki closed 4 years ago

jasonmerecki commented 4 years ago

This is a fix for the crash caused by issue #24, a null pointer, with the top of the stack trace at GeofenceImplementation.OnConnected. The issue is that the callback is registered by the InitializeGoogleAPI method which is called by the constructor. But, the callback may then be executed by another thread, before the thread executing the constructor has fully completed and initialized all variables (or that constructor thread may be done but the variables are not published to all threads).

The fix is to guard the initialization logic with the same lock used to guard other variables in the class. This will ensure the constructor will finish, before the callback directly accesses any of the member variables initialized by the constructor logic.

This also addresses the crash from issue #23, which is a crash caused when the Regions dictionary is modified by a thread while another thread is iterating. The top of the stack trace is "Dictionary.ValueCollection[TKey,TValue].CopyTo". The root cause is that a list is passed to "StartMonitoring(Regions.Values.ToList())" but the underlying Dictionary can be modified while the list is being created. The resulting List is guarded by a lock, but it's too late. The whole Regions Dictionary should be guarded by the lock each time it is accessed.

rdelrosario commented 4 years ago

Thanks for your contribution!