PrintPreview Resource Leak

Please post bug reports, feature requests, or any question regarding the DELPHI AREA projects here.

PrintPreview Resource Leak

Postby MeW » June 14th, 2016, 7:22 am

In the application I'm using an obscure resource leak occurred. I could not figure out, why my code would be wrong after having been 'Print Previewed'. Until I researched the Preview.pas itself. The TThumbnailPreview destructor does not free the LargeImages. When I added an extra line of code, the resource leaks were gone. So please fix the following:
Code: Select all
destructor TThumbnailPreview.Destroy;
begin
  LargeImages.Free; // <<==-- Add this line to prevent obscure resource leaks
  FPaperViewOptions.Free;
  Page.Free;
  inherited Destroy;
end;
MeW
Active Member
Active Member
 
Posts: 9
Joined: February 17th, 2005, 6:34 pm
Location: Netherlands

Re: PrintPreview Resource Leak

Postby sh17 » July 6th, 2016, 1:18 pm

sh17
Active Member
Active Member
 
Posts: 8
Joined: September 3rd, 2013, 11:38 am

Re: PrintPreview Resource Leak

Postby MeW » July 6th, 2016, 8:59 pm

(Y)
MeW
Active Member
Active Member
 
Posts: 9
Joined: February 17th, 2005, 6:34 pm
Location: Netherlands

Re: PrintPreview Resource Leak

Postby Kambiz » August 6th, 2016, 7:26 am

There should not be a memory leak, and there was none until Delphi 2010.

The LargeImages object is owned by TThumbnailPrevew instance. In Delphi, owner takes care of freeing the owned components when it is being freed.

Code: Select all
constructor TThumbnailPreview.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  if Assigned(AOwner) and (AOwner is TControl) then
    FixControlStyles(TControl(AOwner));
  FZoom := 10;
  FSpacingHorizontal := 8;
  FSpacingVertical := 8;
  FMarkerColor := clBlue;
  FPaperViewOptions := TPaperPreviewOptions.Create;              // This object is not owned by component, so it should be freed implicitly in the destructor
  FPaperViewOptions.OnChange := PaperViewOptionsChanged;
  Page := TBitmap.Create;                                        // This object is not owned by component too, so it should be freed implicitly
  ParentColor := True;
  ReadOnly := True;
  ViewStyle := vsIcon;
  LargeImages := TImageList.Create(Self);                        // "Self" is passed as owner, so "Self" is responsible for freeing the TImageList instance.
  Align := alLeft;
end;

destructor TThumbnailPreview.Destroy;
begin
  FPaperViewOptions.Free;
  Page.Free;
  inherited Destroy;
end;
Kambiz
User avatar
Kambiz
Administrator
Administrator
 
Posts: 2429
Joined: March 7th, 2003, 7:10 pm

Re: PrintPreview Resource Leak

Postby MeW » August 6th, 2016, 8:33 am

I agree, but the test case I had at hand proved otherwise. Haven't checked any further.. Should I? I've tested this in Delphi 10.1 Berlin.
MeW
Active Member
Active Member
 
Posts: 9
Joined: February 17th, 2005, 6:34 pm
Location: Netherlands

Re: PrintPreview Resource Leak

Postby Kambiz » August 7th, 2016, 8:25 am

Delphi 2010 was the last version of Delphi that I used for coding. Maybe the way that the owned components are managed is different in the newer versions of Delphi. If it is the case, then there would memory leak in most old components.
Kambiz
User avatar
Kambiz
Administrator
Administrator
 
Posts: 2429
Joined: March 7th, 2003, 7:10 pm


Return to DELPHI AREA Projects

Who is online

Users browsing this forum: No registered users and 24 guests

cron