开发者

Ran AQTime on my application and maybe found Memory Leak

I am running an application developed with RAD Studio XE or Delphi XE under Windows 7. After rebuilding my application from previous project files, I have been testing its functionality, but the application would only run for about a month or so and starts to fail slowly. This application is supposed to run 24/7 for all time, unless Windows OS fails. So, I ran AQTime on the application for few h开发者_JS百科ours and closely watched the results as the program was running. What I noticed was this. With everything else being constant and still in numbers, under resource profiling Brush, Handle, Pen and another Pen are slowly increasing in numbers especially the second pen and Brush resources. Pen seems to be increasing in numbers by about 522 every second. Also, number of handle is going up but very slowly - maybe every 15 minutes. On a side note: some of our users have had a total catastrophic failure, where Windows OS will die completely to a point that you have to reinstall Windows again and everything else.

AQTime result:
Class_Name     Object_Name
Brush          Brush:54,947
Handle         Handle:44,559  --Handle is increasing slowly
Pen            Pen:53,378   
Pen            Pen:54,915     --Pen is increasing every second by 522. 

The application's main window is always going to be displayed on the screen.

UPDATE2:

pen, oldPen Bursh and oldBursh are declared within a base class. They are assigned everytime within the following procedure and the procedure is used throughout the program for drawing elements right on the TForm like circle, polygon, square, line, etc.

procedure TMakerGraphic.SaveCanvas;
begin
  oldPen.Assign(myForm.Canvas.Pen);
  oldBrush.Assign(myForm.Canvas.Brush);
  myForm.Canvas.Pen.Assign(Pen);
  myForm.Canvas.Brush.Assign(Brush);
end;

procedure TMakerGraphic.RestoreCanvas;
begin
  myForm.Canvas.Pen.Assign(oldPen);
  myForm.Canvas.Brush.Assign(oldBrush);
end;

The Only time these variables are released is when the elements on the TForm is deleted as shown by the following Free procedure.

destructor TMakerGraphic.Free;
begin
  Pen.Free;
  Brush.Free;
  oldPen.Free;
  oldBrush.Free;
  inherited Free;
end;

So, is that mean my application is leaking memory?

Any input will be greatly appreciated. Thank you.


It seems that instances of Pen and Brush are not freed properly.

If you are using the built-in drawing procedures of TCanvas, use

Canvas.Pen.Assign(anotherPen)

to fill in a new pen.

If you are using gdiplus.dll via IGDIPlus, each drawing procedure will take a argument of IGPPen or IGPBrush. Thus, either declaring the variables to pass through as IGPPen/IGPBrush; or declaring them as TGPPen/TGPBrush, and free them afterwards.

================= temporary space for sample source code ==========

I would think the following code, as OP gives, does not incur memory/resource leak.

    unit Unit1;

    interface

    uses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs;

    type
      TForm1 = class(TForm)
        procedure FormCreate(Sender: TObject);
      private
        { Private declarations }
        currPen, prevPen: TPen;
        currBrush, prevBrush: TBrush;

        procedure FormPaint(Sender: TObject);
        procedure FormDestroy(Sender: TObject);

        procedure SaveCanvas;
        procedure RestoreCanvas;

      public
        { Public declarations }
      end;

    var
      Form1: TForm1;

    implementation

    {$R *.dfm}

    procedure TForm1.FormCreate(Sender: TObject);
    begin
      currPen := TPen.Create;
      prevPen := TPen.Create;

      currBrush := TBrush.Create;
      prevBrush := TBrush.Create;

      Self.OnPaint := Self.FormPaint;
      Self.OnDestroy := Self.FormDestroy;
    end;

    procedure TForm1.FormPaint(Sender: TObject);
    begin
      SaveCanvas;
      RestoreCanvas;
    end;

    procedure TForm1.FormDestroy(Sender: TObject);
    begin
      prevPen.Free;
      prevBrush.Free;
      currPen.Free;
      currBrush.free;
    end;

    procedure TForm1.SaveCanvas;
    begin
      prevPen.Assign(Self.Canvas.Pen);
      prevBrush.Assign(Self.Canvas.Brush);
      Self.Canvas.Pen.Assign(currPen);
      Self.Canvas.Brush.Assign(currBrush);
    end;

    procedure TForm1.RestoreCanvas;
    begin
      Self.Canvas.Pen.Assign(prevPen);
      Self.Canvas.Brush.Assign(prevBrush);
    end;

    end.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜