Thema: Delphi GDI-Handles Leck

Einzelnen Beitrag anzeigen

DevilsCamp
(Gast)

n/a Beiträge
 
#1

GDI-Handles Leck

  Alt 9. Okt 2008, 08:17
Ich habe in einer Progress-Bar-Komponente folgenden Code zum anzeigen des Fortschritts als Text innerhalb der Komponente:

Delphi-Quellcode:
procedure TIrgendEineKomponente.PaintText(PaintRect: TRect; Canv: TCanvas);
var
  Ima2 : TBitmap;
  s : string;
  X : Integer;
  Y : Integer;
  diffx : Integer;
  t: string;
begin
  if fShowText then
  begin
    Ima2 := TBitmap.Create;
    Ima2.Width := Width;
    Ima2.Height := Height;
    diffx := 0;

    with Ima2.Canvas do
    begin
      CopyMode := cmBlackness;
      CopyRect(Rect(0, 0, Width, Height), Ima2.Canvas, Rect(0, 0, Width, Height));
      CopyMode := cmSrcCopy;
    end;

    with Ima2.Canvas do
    begin
      Font := Self.Font;
      Brush.Style := bsClear;
      Font.Color := clWhite;

      case fTextKind of
        mrgsPercent: s := Format('%0.*f%%', [FNachkommstellen, FPercentage]);
        mrgsXofY: s := Format('%0.0n / %0.0n', [1.0 * FPosition, 1.0 * FMaxWert]);
        mrgsValueOnly: s := Format('%0.0n', [1.0 * FPosition]);
        mrgsOwnText:
          begin
            if (not (csDesigning in ComponentState)) then
            begin
              if Assigned(FOnOwnText) then
                s := FOnOwnText(Self);
            end;
          end;
      end; // case

      if fTextKind <> mrgsXofY then
      begin
        with PaintRect do
        begin
          X := (Right - Left + 1) div 2 - TextWidth(S) div 2;
          Y := (Bottom - Top + 1 - TextHeight(S)) div 2;
        end; // with
      end
      else
      begin
        with PaintRect do
        begin
          X := (Right - Left + 1) div 2 - TextWidth(' / ') div 2;
          X := X - TextWidth(Format('%0.0n', [1.0 * FPosition]));
          Y := (Bottom - Top + 1 - TextHeight(S)) div 2;
        end;
      end;

      TextRect(PaintRect, X, Y, s);
    end; // with Ima2.Canvas

    Canv.CopyMode := cmSrcInvert;
    Canv.Draw(0, 0, Ima2);

    FreeAndNil(Ima2);
  end;
end;
Da ich mit Hilfe des Process Explorers festgestellt habe, dass die Anzahl der GDI-Handles während der Benutzung dieser Komponente steigt, sobald sich der Text ändert, habe ich folgende Theorie:
Im Code wird ja mit der Zeile
Ima2 := TBitmap.Create; ein TBitmap erzeugt (steigt da die Anzahl der GDI-Handles?) und mit
FreeAndNil(Ima2); wieder zerstört.
Was aber, wenn FreeAndNil nicht so funktioniert, wie es sollte, d.h. Ima2 NICHT zerstört wird? Dann würde ja das GDI-Handle nicht geschlossen werden, oder? Oder würde das automatisch mit dem beenden der Methode passieren?
  Mit Zitat antworten Zitat