title: Delphi: Should a thread ever be created "not suspended"?
body: <p>I've been trying to track down a memory leak in Jedi VCL's <code>JvHidControllerClass.pas</code>, which i came across this change in the source history:</p>
  
  <p><strong>Older revision:</strong></p>
  
  <pre><code>constructor TJvHidDeviceReadThread.CtlCreate(const Dev: TJvHidDevice);
  begin
    inherited Create(True);
    Device := Dev;
    NumBytesRead := 0;
    SetLength(Report, Dev.Caps.InputReportByteLength);
  end;
  </code></pre>
  
  <p><strong>Current revision:</strong></p>
  
  <pre><code>constructor TJvHidDeviceReadThread.CtlCreate(const Dev: TJvHidDevice);
  begin
    inherited Create(False);
    Device := Dev;
    NumBytesRead := 0;
    SetLength(Report, Dev.Caps.InputReportByteLength);
  end;
  </code></pre>
  
  <p>From experience, i've discovered that if you create a thread <strong>not</strong> suspended:</p>
  
  <pre><code>inherited Create(False);
  </code></pre>
  
  <p>then the thread immediately starts running. In this case it will attempt to access an object that has not been initialized yet:</p>
  
  <pre><code>procedure TJvHidDeviceReadThread.Execute;
  begin
     while not Terminated do
     begin
       FillChar(Report[0], Device.Caps.InputReportByteLength, #0);
       if Device.ReadFileEx(Report[0], Device.Caps.InputReportByteLength, @DummyReadCompletion) then
  </code></pre>
  
  <p>right away trying to fill <code>Report</code>, and access the object <code>Device</code>. Problem is that they haven't been initialized yet; those are the <em>next</em> lines after the thread has started:</p>
  
  <pre><code>  Device := Dev;
    NumBytesRead := 0;
    SetLength(Report, Dev.Caps.InputReportByteLength);
  </code></pre>
  
  <p>i realize this is a race condition; and the chances of a user experiencing a crash in production are pretty low, so it's probably harmless to leave the race-crash.</p>
  
  <p>But am i way off? Am i missing something? Does calling:</p>
  
  <pre><code>BeginThread(nil, 0, @ThreadProc, Pointer(Self), Flags, FThreadID);
  </code></pre>
  
  <p>not start a thread off and running right away? Is this really a race condition regression that was (intentionally) added to JVCL? Is there some secret about </p>
  
  <pre><code>CreateSuspended(False);
  </code></pre>
  
  <p>that makes it the <em>correct</em> code over:</p>
  
  <pre><code>CreateSuspended(True);
  ...
  FDataThread.Resume;
  </code></pre>
  
  <p>?</p>
  
  <p>After having been burned by mistakenly calling </p>
  
  <pre><code>TMyThread.Create(False)
  </code></pre>
  
  <p>i've filed it in my brain as <strong>never</strong> correct. Is there any valid use for letting a thread start right away (when you have to initialize values)?</p>