title: Delphi XE5 - Threads and memory leak when operation on TBitmap
body: <p>Application works as I'd like but there are quite big memory leakage.
  Every event that throttles one thread gives me 4 TBitmaps and 2 TStrokeBrush that are lost.</p>
  
  <p>The procedure DrawSine(); is triggered in Execute in Synchronize statement:</p>
  
  <pre><code>procedure SineThread.DrawSine();
  var
    sin_T : Extended;
    Point2 : TPoint;
    I : Integer;
  begin
    TempBitmap.SetSize(Twidth, Theight);
    TempBitmap.Canvas.BeginScene();
    TempBitmap.Canvas.Stroke.Kind := TBrushKind.bkSolid;
    TempBitmap.Canvas.Stroke.Color := claLime;
    TempBitmap.Canvas.Clear(TAlphaColorRec.Black);
    for I := 0 to Twidth do
    begin
        sin_T := Sin(((I - Tphas)/100.0) * Tfreq);
  
        Point2.X := Round(I);
        Point2.Y := Round(sin_T * Tampl) + Round(Theight/2.0);
  
        if I = 0 then
        begin
          Point1.X := Round(I);
          Point1.Y := Round(sin_T * Tampl) + Round(Theight/2.0);
          TempBitmap.Canvas.DrawLine(Point1, Point2, 1.0, TempBrush);
        end
        else
        begin
          if I = Twidth then
          begin
            TempBitmap.Canvas.DrawLine(Point1, Point2, 1.0, TempBrush);
            Point1.X := Round(I);
            Point1.Y := Round(Theight/2.0);
          end
          else
          begin
            TempBitmap.Canvas.DrawLine(Point1, Point2, 1.0, TempBrush);
            Point1.X := Point2.X;
            Point1.Y := Point2.Y;
          end;
       end;
    end;
    TempBitmap.Canvas.EndScene();
  end;
  </code></pre>
  
  <p>SineThread Constructor and Destructor:</p>
  
  <pre><code>constructor SineThread.Create(CreateSuspended: Boolean);
  begin
    inherited Create(CreateSuspended);
    try
      TempBitmap := TBitmap.Create();
      TempBrush := TStrokeBrush.Create(TBrushKind.bkSolid, TAlphaColorRec.White);
    finally
      Twidth := 0;
      Theight := 0;
      Tampl := 0;
      Tphas := 0;
      Tfreq := 0;
      Point1 := Point(0,0);
    end;
  end;
  
  destructor SineThread.Destroy();
  begin
    inherited Destroy();
    TempBitmap.Free();
    TempBrush.Free();
  end;
  </code></pre>
  
  <p>OnTerminate when finishing thread looks like:</p>
  
  <pre><code>procedure TForm1.OnTerminateProc1(Sender: TObject);
  var
    TempStream : TMemoryStream;
  begin
    try
      TempStream := TMemoryStream.Create();
    finally
      (Sender as SineThread).GetBitmap.SaveToStream(TempStream);
      Image1.Bitmap.LoadFromStream(TempStream);
      TempStream.Free();
    end;
  end;
  </code></pre>
  
  <p>The Trigger() procedure is started every the the value on TrackBars change:</p>
  
  <pre><code>procedure TForm1.Trigger(Sender: TObject);
  var
    sine1_thread : SineThread;
    sine2_thread : SineThread;
    sineSum_thread : SineSumThread;
  begin
    try
      begin
        sine1_thread := SineThread.Create(True);
        sine2_thread := SineThread.Create(True);
        sineSum_thread := SineSumThread.Create(True);
      end;
    finally
      begin
        sine1_thread.SetSineParams(TrackBar1.Value, TrackBar2.Value, TrackBar3.Value);
        sine1_thread.SetImageParams(Trunc(Image1.Width), Trunc(Image1.Height));
        sine1_thread.FreeOnTerminate := True;
        sine1_thread.OnTerminate := OnTerminateProc1;
        sine1_thread.Start();
        sine2_thread.SetSineParams(TrackBar4.Value, TrackBar5.Value, TrackBar6.Value);
        sine2_thread.SetImageParams(Trunc(Image2.Width), Trunc(Image2.Height));
        sine2_thread.FreeOnTerminate := True;
        sine2_thread.OnTerminate := OnTerminateProc2;
        sine2_thread.Start();
        sineSum_thread.SetSineParams(TrackBar1.Value, TrackBar2.Value, TrackBar3.Value, TrackBar4.Value, TrackBar5.Value, TrackBar6.Value);
        sineSum_thread.SetImageParams(Trunc(Image3.Width), Trunc(Image3.Height));
        sineSum_thread.FreeOnTerminate := True;
        sineSum_thread.OnTerminate := OnTerminateProc3;
        sineSum_thread.Start();
      end;
    end;
  end;
  </code></pre>