Bharat
Bharat

Reputation: 6866

Firemonkey - TPopUp memory issue

I am facing a strange issue. I have set of buttons in a panel and I want to show tooltip for each button. For that I am using TPopUp, but whenever mouse enter, I can observe that memory is increasing for the application. But if I comment the mouse enter and mouse leave events then memory doesn't increase. Did I miss something?

Whenever the mouse enters the button, I can see 0.3MB increase in my task manager.

TfrmEncode = class(TForm)    
    pnlTop: TPanel;
    btnSaveToJSON: TButton;
    procedure FormCreate(Sender: TObject);
    procedure btnSaveToJSONMouseEnter(Sender: TObject);
    procedure btnSaveToJSONMouseLeave(Sender: TObject);
  private
    { Private declarations }
    pop : TPopup;
    cb : TColorBox;
    labelText: TLabel;
  public
    { Public declarations }
  end;

implementation

{$R *.fmx}

procedure TfrmEncode.btnSaveToJSONMouseEnter(Sender: TObject);
begin
  Pop.IsOpen := True;
end;

procedure TfrmEncode.btnSaveToJSONMouseLeave(Sender: TObject);
begin
  Pop.IsOpen := False;
end;

procedure TfrmEncode.FormCreate(Sender: TObject);
begin
  try
    pop := TPopup.Create(self);
    pop.Parent:= self;
    pop.Width:=200;

    cb := TColorBox.Create(pop);
    cb.Align := TAlignLayout.Client;
    cb.Color := TAlphaColors.White;
    pop.AddObject(cb);

    labelText := TLabel.Create(pop);
    labelText.Align  :=TAlignLayout.alClient;
    labelText.Parent := pop;
    labelText.Text := 'This is the hint This is the hint This is the hint This is the hint This is the hint This is the hint This is the hint This is the hint This is the hint This is the hint';
    pop.AddObject(labelText);

    pop.PlacementTarget := btnSaveToJSON;
    pop.Placement:=TPlacement.BottomCenter;
  finally
  end;

end;

procedure TfrmEncode.FormDestroy(Sender: TObject);
begin
  FreeAndNil(pop);
end;

Upvotes: 1

Views: 481

Answers (1)

Dalija Prasnikar
Dalija Prasnikar

Reputation: 28516

There is a bug in TPopup control. Reported as RSP-21438

TPopup internally creates new TCustomPopupForm every time popup is open. However, that form does not get released when popup is closed (as it should) but only when popup control itself is destroyed.

There are few workarounds

1. Create new TPopup control on open and free it on close

2. Fix FMX.Controls and FMX.Forms

Error can be fixed in implementation section of the above units. That means you can copy FMX.Controls and FMX.Forms into your project folder and Delphi will use those fixed units instead of default ones.

Fix following code:

FMX.Controls - change constructor parameter from False to True - it means popup form will be automatically released on close.

function TPopup.CreatePopupForm: TFmxObject;
...
    NewForm := TCustomPopupForm.Create(Self, NewStyle, PlacementTarget, True);

FMX.Forms - assign AutoFree parameter to field.

constructor TCustomPopupForm.Create(AOwner: TComponent; AStyleBook: TStyleBook = nil; APlacementTarget: TControl = nil;
  AutoFree: Boolean = True);
var
  NewStyleBook: TStyleBook;
begin
  FAutoFree := AutoFree;
....

Upvotes: 1

Related Questions