fix race condition likely causing null ptr exception when registering a listener.
Fixes #218