Recent

Author Topic: openDialog.Execute... memory leack ?!  (Read 7609 times)

Lasfan

  • Newbie
  • Posts: 5
openDialog.Execute... memory leack ?!
« on: April 30, 2017, 12:19:56 pm »
Hello guys,

I have one problem. I have a simple project only with a Form1, Button1 and Label1. The idea is, when I press Button1, a function "getFileName" will be called. In this function, the openDialog will appear to let user to select a file name.  Then the function result (the selected file name ) will be displayed on Lable1. This works; at least it return the file name selected by the user. Simple, right?

But I have "Heaptrc unit(check for mem-leaks)(-gh)" checked on. That is at "Project Option...>Compiler options>Debugging".
BTW: I use Lazarus IDE v1.6.2, win7, 64bit

When I compile and run the program and press Button1, I can select the file name from the dialog and display it on Label1. However when I close the program, the heaptrc gives that there is a leak. see picture. Finally I get SIGSEGV exception.

Here is my code:

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls;
  9.  
  10. type
  11.   { TForm1 }
  12.  
  13.   TForm1 = class(TForm)
  14.     Button1: TButton;
  15.     Label1: TLabel;
  16.     procedure Button1Click(Sender: TObject);
  17.   private
  18.     { private declarations }
  19.   public
  20.     { public declarations }
  21.   end;
  22.  
  23. var
  24.   Form1: TForm1;
  25.  
  26.   Function getFileName:String;
  27.  
  28. implementation
  29.  
  30. {$R *.lfm}
  31.  
  32. { TForm1 }
  33.  
  34. procedure TForm1.Button1Click(Sender: TObject);
  35. begin
  36.    Label1.Caption:= getFileName;
  37. end;
  38.  
  39. Function getFileName:String;
  40. var
  41.   openDialog : TOpenDialog;
  42.   selectedFileName:String;
  43. begin
  44.   selectedFileName:='';
  45.   openDialog := TOpenDialog.Create(nil);
  46.   try
  47.     openDialog.Title:='Select a databse file';             // Title of Dialog
  48.     openDialog.InitialDir := 'C:\Users\Guest\Documents\';  // Starting directory
  49.     openDialog.Filter := 'Database files only|*.db';       // showonly files with .db extenstion
  50.  
  51.     if openDialog.Execute then                             //Open dialog
  52.        selectedFileName:= OpenDialog.FileName;
  53.   finally
  54.      openDialog.Free;
  55.   end;
  56.   result:=selectedFileName;
  57. end;
  58.  
  59. end
  60.  


   




« Last Edit: April 30, 2017, 12:32:02 pm by Lasfan »

wp

  • Hero Member
  • *****
  • Posts: 11830
Re: openDialog.Execute... memory leack ?!
« Reply #1 on: April 30, 2017, 12:58:48 pm »
I agree that this windows looks like an indication that there is a memory leak. But in fact there is none: Look at the line "0 unfreed memory blocks: 0" - this means: there are no unfreed memory blocks, or in other words, all memory blocks allocated have been freed at the end --> no memory leak.

If memory leaks would have been detected there would be a non-zero number in this line, and a (usually) long list, often extending into multiple dialog boxes, would follow indicating the lines in which unfreed memory had been allocated.

http://wiki.freepascal.org/heaptrc

Lasfan

  • Newbie
  • Posts: 5
Re: openDialog.Execute... memory leack ?!
« Reply #2 on: April 30, 2017, 01:10:59 pm »
I agree that this windows looks like an indication that there is a memory leak. But in fact there is none: Look at the line "0 unfreed memory blocks: 0" - this means: there are no unfreed memory blocks, or in other words, all memory blocks allocated have been freed at the end --> no memory leak.

If memory leaks would have been detected there would be a non-zero number in this line, and a (usually) long list, often extending into multiple dialog boxes, would follow indicating the lines in which unfreed memory had been allocated.

http://wiki.freepascal.org/heaptrc

Thank you very much for your reply, WP!

But what about the exception? Can just ignore it? Would you also please also compile the code if you can reproduce the same problem?

Thanks:)

RAW

  • Hero Member
  • *****
  • Posts: 868
Re: openDialog.Execute... memory leack ?!
« Reply #3 on: April 30, 2017, 01:27:45 pm »
What kind of exception exactly ?? Any further information ??
If I run your code then I don't get any exception. The form shows up, I select a file and after that the label.caption shows the path...

EDIT:
Code: Pascal  [Select][+][-]
  1. Unit Unit1;
  2.  {$MODE OBJFPC}{$H+}
  3.  
  4. Interface
  5.  USES
  6.   Classes,  SysUtils, Forms,
  7.   Controls, Dialogs,  StdCtrls;
  8.  
  9.  TYPE
  10.   TForm1 = Class(TForm)
  11.  
  12.    Button1: TButton;
  13.    Label1 : TLabel;
  14.  
  15.    Procedure Button1Click (Sender: TObject);
  16.    Function  getFileName: String;
  17.   End;
  18.  
  19.  VAR
  20.   Form1: TForm1;
  21.  
  22. Implementation
  23.  {$R *.LFM}
  24.  
  25.  
  26. Function TForm1.getFileName: String;
  27.   Var
  28.    openDialog      : TOpenDialog;
  29.    selectedFileName: String;
  30.  Begin
  31.   selectedFileName:= '';
  32.   openDialog:= TOpenDialog.Create(Self);
  33.    Try
  34.  
  35.     openDialog.Title     := 'Select a databse file';
  36.     openDialog.InitialDir:= 'C:\';
  37.     openDialog.Filter    := 'ALL|*.*';
  38.  
  39.      If openDialog.Execute
  40.      Then selectedFileName:= OpenDialog.FileName;
  41.  
  42.    Finally
  43.     openDialog.Free;
  44.    End;
  45.   Result:= selectedFileName;
  46.  End;
  47.  
  48.  
  49. Procedure TForm1.Button1Click(Sender: TObject);
  50.  Begin
  51.   Label1.Caption:= getFileName;
  52.  End;
  53.  
  54. End.
  55.  
« Last Edit: April 30, 2017, 01:43:49 pm by RAW »
Windows 7 Pro (x64 Sp1) & Windows XP Pro (x86 Sp3).

Lasfan

  • Newbie
  • Posts: 5
Re: openDialog.Execute... memory leack ?!
« Reply #4 on: April 30, 2017, 01:52:50 pm »
Thanks RAW for the replay.

Have you set "Heaptrc unit" to TRUE at your Project option? That is to check a memory leak. 

First I get the dialog as u can see in the picture. Then after few seconds I get SIGSEGV exception.

HeavyUser

  • Sr. Member
  • ****
  • Posts: 397
Re: openDialog.Execute... memory leack ?!
« Reply #5 on: April 30, 2017, 02:02:59 pm »
that is not an error. It is a report from heaptrc unit raised as an error. the relevant part is the line that says "0 unfreed memory blocks : 0" which means that there are no memory leaks detected. In case of memory leaks a number of lines with addresses will be shown, you can use those addresses to locate which objects are not freed. there is also a addin for the IDE that will allow you to jump on those lines when double clicking the reported leak, but it needs to read the report from the disk (which can be set up easily).

RAW

  • Hero Member
  • *****
  • Posts: 868
Re: openDialog.Execute... memory leack ?!
« Reply #6 on: April 30, 2017, 02:08:47 pm »
Quote
Have you set "Heaptrc unit" to TRUE at your Project option? That is to check a memory leak.
Yes, I used both (DEBUG MODE and RELEASE MODE)... no exceptions at all... (Windows 7 x64).
Looks like your problem is elsewhere in the code ?!
Windows 7 Pro (x64 Sp1) & Windows XP Pro (x86 Sp3).

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: openDialog.Execute... memory leack ?!
« Reply #7 on: April 30, 2017, 02:16:28 pm »
6248 memory blocks allocated indicates you have much more than a label and a button being instantiated.
Start a new project and save it in a new folder with just a label and a button, and copy your GetFileName function to the new project.
Delete the old project and see if your exception goes away.

RAW

  • Hero Member
  • *****
  • Posts: 868
Re: openDialog.Execute... memory leack ?!
« Reply #8 on: April 30, 2017, 02:28:21 pm »
Yeah, on my System: 3201 Memory Blocks are allocated...

EDIT:
see scr shot
« Last Edit: April 30, 2017, 02:32:11 pm by RAW »
Windows 7 Pro (x64 Sp1) & Windows XP Pro (x86 Sp3).

Lasfan

  • Newbie
  • Posts: 5
Re: openDialog.Execute... memory leack ?!
« Reply #9 on: April 30, 2017, 06:43:23 pm »
6248 memory blocks allocated indicates you have much more than a label and a button being instantiated.
Start a new project and save it in a new folder with just a label and a button, and copy your GetFileName function to the new project.
Delete the old project and see if your exception goes away.

The option "Generate debugging info for GDB" was selected in the Project Options. That is why the allocated memory is bigger. That makes the program slower and make the exe file biger. But it is not a leak!! That is not a problem at all.

After un-selecting that option and compiling it get less memory allocation. But the problem is still present (see the picture)

ASerge

  • Hero Member
  • *****
  • Posts: 2212
Re: openDialog.Execute... memory leack ?!
« Reply #10 on: April 30, 2017, 07:14:56 pm »
But the problem is still present (see the picture)
No problem.
If run without open dialog then (see where is 192 bytes).
If run with open dialog, then result the same, excluding comment for system startup.
« Last Edit: April 30, 2017, 07:16:29 pm by ASerge »

Lasfan

  • Newbie
  • Posts: 5
Re: openDialog.Execute... memory leack ?!
« Reply #11 on: April 30, 2017, 07:32:57 pm »
Thank you very much all of you guys!

Well, if this is not  really a memory leak, then I  consider that my question is answered! :)

If anybody has different opinion or anything to suggest me, I would be glad to hear it.

Bye for now. 

RAW

  • Hero Member
  • *****
  • Posts: 868
Re: openDialog.Execute... memory leack ?!
« Reply #12 on: April 30, 2017, 09:10:32 pm »
The same thing on my system ... (OpenDialog)  :)

Windows 7 Pro (x64 Sp1) & Windows XP Pro (x86 Sp3).

 

TinyPortal © 2005-2018