title: Send string data from Thread to main form
body: <p>In Dephi, I create a thread, like this, which will send message to main form from time to time</p>
  
  <pre><code>Procedure TMyThread.SendLog(I: Integer);
  Var
    Log: array[0..255] of Char;
  Begin
    strcopy(@Log,PChar('Log: current stag is ' + IntToStr(I)));
     PostMessage(Form1.Handle,WM_UPDATEDATA,Integer(PChar(@Log)),0);
  End;
  
  procedure TMyThread.Execute;
  var
    I: Integer;
  begin
    for I := 0 to 1024 * 65536 do
    begin
      if (I mod 65536) == 0 then
      begin
        SendLog(I);
      End;
    End;
  end;
  </code></pre>
  
  <p>where WM_UPDATEDATA is a custom message, defined below:</p>
  
  <pre><code>const
    WM_UPDATEDATA = WM_USER + 100;
  </code></pre>
  
  <p>And in main form, it will do as follows to update the list:</p>
  
  <pre><code>procedure TForm1.WMUpdateData(var msg : TMessage);
  begin
    List1.Items.Add(PChar(msg.WParam));
  end;
  </code></pre>
  
  <p>However, as the Log string sent to the main form is a local variable, which will be destroyed after calling SendLog. While TForm1.WMUpdateData process the message asynchronously, so it is possible that when it is invoked, the Log string has already been destroyed. How to solve this problem?</p>
  
  <p>I think maybe I can allocate the string space in a global system space, and then pass it to the message, then after TForm1.WMUpdateData processes the message, it can destroy the string space in the global space. Is that a workable solution? How to implement this?</p>
  
  <p>Thanks</p>