The foolproof solution is to wrap the contents of BeginUpdate and EndUpdate in a critical section or mutex. In essence, you need to prevent two (or more) threads from actually being in either of these functions at the same time, including not allowing a call to EndUpdate at the same time as a call to BeginUpdate... (Scenario: In thread one, EU decrements the counter to zero, and gets timesliced before even beginning to release the updater object. Now i thread two, BU increments the counter, sees that it is one, and creates a new updater object, and then gets timesliced. Thread one resumes, grabs the new object pointer and releases it. Result: thread two crashes on a NULL, and the old updater object is leaked. The critical section / mutex solution prevents thread two from actually entering BU before thread one has finished its work. Put the CRITICALSECTION structure in the host object!)
↧