Why is my C++ Class variable unexpectedly changing outside scope?
<p>I'm trying to reverse-Engineer and fix a Win32 Console Application which is throwing an unhandled exception.</p>

<p>I've got 32 instances of a class running in threads. These "Service" threads should each create / delete a unique instance of CMessage regularly and each of these CMessage objects is declared private in the Service class.</p>

<p>When I run the application in the debugger (I'm using Visual Studio 2005) the watch window for the CMessage indicates that right before a CMessage member function is invoked the address of CMessage changes to 0x00000000 - the cause of the exception.</p>

<p>There is no straightforward explanation that i can see for why this happens. Nothing untoward appears to be happening between the previous line of code and this one, so my suspicion is that somehow each thread is mistakenly referencing the CMessage object from one of the other threads in some cases (as at some point during the threads lifetime the CMessage object for that thread is deleted).</p>

<p>My question is: What is the best way to make sure that these CMessage objects are absoloutely thread safe and can't be modified / deleted from anywhere, except for the current class scope? I thought that this was what private was for, but I'm not entirely sure now. One thing I thought about doing was referring to the CMessage object as follows:</p>

<pre><code>this-&gt;myCMessage;
</code></pre>

<p>as opposed to just:</p>

<pre><code>myCMessage;
</code></pre>

<p>Would this make any difference?</p>
