Member variables in method are null when called as delegate from thread

Tim t.oliver at windowslive.com
Mon Jan 11 00:43:00 UTC 2021


Hi there,

I have something like this:

class Foo{
     MongoClient db;

     this(){
         db = connectMongoDB("127.0.0.1");
         void delegate()[string] commands = ["start": &this.start];
	MessageService messenger = new MessageService(8081, commands);
     }

     void start(){
         // Do something with db
     }

MessageService is a thread that deals with socket communication. 
When a command comes in, it calls the appropriate delegate given 
to it by commands. When MessageService calls the delegate for 
start, db is null. If I call start() in the Foo constructor it 
works just fine. Am I missing something here? Do delegates get 
called outside of their class context? I know I could just pass 
the db into start but I want to work out exactly why this is 
happening

Thanks in advance


More information about the Digitalmars-d-learn mailing list