Recent

Author Topic: Possible compiler problem  (Read 2624 times)

daveinhull

  • Sr. Member
  • ****
  • Posts: 297
  • 1 divided by nothing must still be 1!
Possible compiler problem
« on: March 23, 2019, 09:12:34 am »
Hi,

I've found something funny which might (maybe) be a compiler problem so just wondering firstly if I've got something wrong and secondly if anyone else has a problem with the following code:

Code: Pascal  [Select][+][-]
  1. function GetSaveFilename (var MyFilename: string; Filter:string = ''; Title: string = ''): boolean;
  2. var
  3.   SaveDialog: TSaveDialog;
  4. begin
  5.   try
  6.     try
  7.       SaveDialog := TSaveDialog.Create (Nil);
  8.       SaveDialog.Title := Title;
  9.       SaveDialog.Filter := Filter;
  10.       if SaveDialog.Execute then
  11.         MyFilename := SaveDialog.FileName;
  12.     except
  13.       on E: Exception do
  14.         raise Exception.Create('Cannot get save filename' + cnCrLf + E.Message);
  15.     end;
  16.   finally
  17.     SaveDialog.Free;
  18.     if MyFilename <> '' then
  19.       result := True
  20.     else
  21.       result := False;
  22.   end
  23. end;
This compiles, but it results in an error about illegal variant operation, although I can say where as once it errors the debugger doesn't work.

But if I (incorrectly logic wise) change
Code: Pascal  [Select][+][-]
  1.     if MyFilename <> '' then
to
Code: Pascal  [Select][+][-]
  1.     if MyFilename = '' then
Then it doesn't error, but of course the logic is now wrong.

The calling code is
Code: Pascal  [Select][+][-]
  1. if GetSaveFilename (SaveFilename,'Excel files|*.xls*|All files|*.*','Please select an Excel file to save the year schedule') then
  2. begin
  3.   oExcel := OpenOffice ('Excel');
  4. end;
Any thoughts?

Dave
« Last Edit: March 23, 2019, 10:20:36 pm by daveinhull »
Version #:1.8.4 Date 2019-01-08 FPC Version: 3.0.4 and SVN Revision 57972 for x86_64-win64-win32/win64

daveinhull

  • Sr. Member
  • ****
  • Posts: 297
  • 1 divided by nothing must still be 1!
Re: Possible compiler problem
« Reply #1 on: March 23, 2019, 09:47:43 am »
An update:

I rewrote the code to
Code: Pascal  [Select][+][-]
  1.     function GetSaveFilename (var MyFilename: string; Filter:string = ''; Title: string = ''): boolean;
  2.     var
  3.       SaveDialog: TSaveDialog;
  4.     begin
  5.       result := False;
  6.       try
  7.         try
  8.           SaveDialog := TSaveDialog.Create (Nil);
  9.           SaveDialog.Title := Title;
  10.           SaveDialog.Filter := Filter;
  11.           if SaveDialog.Execute then
  12.           begin
  13.             MyFilename := SaveDialog.FileName;
  14.             result := True;
  15.           end
  16.         except
  17.           on E: Exception do
  18.             raise Exception.Create('Cannot get save filename' + cnCrLf + E.Message);
  19.         end;
  20.       finally
  21.         SaveDialog.Free;
  22.       end
  23.     end;
But it errors with the first line result := False; If I change this first line to Result := True it works (although again the logic is now wrong). It looks as if whenever I set the result to False it errors - I must be doing something really stupid and just can't see it!

Dave
Version #:1.8.4 Date 2019-01-08 FPC Version: 3.0.4 and SVN Revision 57972 for x86_64-win64-win32/win64

dbannon

  • Hero Member
  • *****
  • Posts: 2786
    • tomboy-ng, a rewrite of the classic Tomboy
Re: Possible compiler problem
« Reply #2 on: March 23, 2019, 10:49:13 am »
Quote
if GetSaveFilename (SaveFilename,'Excel files|*.xls*|All files|*.*','Please select an Excel file to save the year schedule') then

I think you need to show us how you declare SaveFilename ....

Davo
Lazarus 3, Linux (and reluctantly Win10/11, OSX Monterey)
My Project - https://github.com/tomboy-notes/tomboy-ng and my github - https://github.com/davidbannon

ASerge

  • Hero Member
  • *****
  • Posts: 2222
Re: Possible compiler problem
« Reply #3 on: March 23, 2019, 11:12:25 am »
I've found something funny which might (maybe) be a compiler problem so just wondering firstly if I've got something wrong and secondly if anyone else has a problem with the following code:
There's no problem with this code, if replace unknown cnCrLf with LineEnding. But...
1. If the parameter is only for returning a value, it is better to mark it as out.
2. It is better to use the const modifier if the parameter does not change, especially for managed types.
3. The try finally block is not used correctly. Correct: AllocResource; try UseResource finally FreeResource end;
4. I suspect that on all platforms if SaveDialog.Execute returns true, the file name will not be empty.
With this in mind, we can rewrite as follows:
Code: Pascal  [Select][+][-]
  1. function GetSaveFilename(out MyFilename: string; const Filter: string = '';
  2.   const Title: string = ''): Boolean;
  3. var
  4.   SaveDialog: TSaveDialog;
  5. begin
  6.   SaveDialog := TSaveDialog.Create(nil);
  7.   try
  8.     try
  9.       SaveDialog.Title := Title;
  10.       SaveDialog.Filter := Filter;
  11.       Result := SaveDialog.Execute;
  12.       if Result then
  13.         MyFilename := SaveDialog.FileName;
  14.     except
  15.       on E: Exception do
  16.         raise Exception.Create('Cannot get save filename' + LineEnding + E.Message);
  17.     end;
  18.   finally
  19.     SaveDialog.Free;
  20.   end
  21. end;
I also think that exception handling is not particularly useful here.

daveinhull

  • Sr. Member
  • ****
  • Posts: 297
  • 1 divided by nothing must still be 1!
Re: Possible compiler problem
« Reply #4 on: March 23, 2019, 11:24:16 am »
@dbannon: SaveFilename is declared as a string;
@ASerge: Thanks for the comments and guidance, but it still doesn't work. I still get the error message and it fails at the line
Code: Pascal  [Select][+][-]
  1.     FApplicationHandlers[ahtModalEnd].CallNotifyEvents(Self);
when I cancel the savedialog, i.e. setting result to false as in my code example.
in the following application.inc code:
Code: Pascal  [Select][+][-]
  1. procedure TApplication.ModalFinished;
  2. begin
  3.   dec(FModalLevel);
  4.   RestoreStayOnTop(True);
  5.   if (FModalLevel = 0) then
  6.   begin
  7.     if Assigned(FOnModalEnd) then
  8.       FOnModalEnd(Self);
  9.     FApplicationHandlers[ahtModalEnd].CallNotifyEvents(Self);
  10.   end;
  11. end;

If I actually select a file in the savedialog it works, but then this returns True to result and that works in my code as well.


Dave
« Last Edit: March 23, 2019, 11:26:36 am by daveinhull »
Version #:1.8.4 Date 2019-01-08 FPC Version: 3.0.4 and SVN Revision 57972 for x86_64-win64-win32/win64

ASerge

  • Hero Member
  • *****
  • Posts: 2222
Re: Possible compiler problem
« Reply #5 on: March 23, 2019, 11:49:54 am »
but it still doesn't work. I still get the error message and it fails at the line
Please make a simple project so that we can see the problem.

daveinhull

  • Sr. Member
  • ****
  • Posts: 297
  • 1 divided by nothing must still be 1!
Re: Possible compiler problem
« Reply #6 on: March 23, 2019, 11:59:28 am »
Hi ASerge,

Ok, simple test attached.

Thanks
Dave
Version #:1.8.4 Date 2019-01-08 FPC Version: 3.0.4 and SVN Revision 57972 for x86_64-win64-win32/win64

Nitorami

  • Sr. Member
  • ****
  • Posts: 481
Re: Possible compiler problem
« Reply #7 on: March 23, 2019, 02:26:20 pm »
I believe the issue can be reduced to this

Code: Pascal  [Select][+][-]
  1. procedure Tform1.Button1click(Sender: Tobject);
  2. var
  3.         oExcel: OLEVariant;
  4. begin
  5.      CloseOffice ('Excel',oExcel)
  6. End;

daveinhull

  • Sr. Member
  • ****
  • Posts: 297
  • 1 divided by nothing must still be 1!
Re: Possible compiler problem
« Reply #8 on: March 23, 2019, 03:11:36 pm »
@Nitorami,

Yes it could, but the purpose of the code was to demonstrate the problem, not to do anything useful!

Thanks
Dave
Version #:1.8.4 Date 2019-01-08 FPC Version: 3.0.4 and SVN Revision 57972 for x86_64-win64-win32/win64

ASerge

  • Hero Member
  • *****
  • Posts: 2222
Re: Possible compiler problem
« Reply #9 on: March 23, 2019, 03:55:40 pm »
Ok, simple test attached.
In this simple test, GetSaveFilename works fine, but the CloseOffice always generates an error because statement "OfficeServer.Visible: = True;" use variable OfficeServer which is Unassigned.

daveinhull

  • Sr. Member
  • ****
  • Posts: 297
  • 1 divided by nothing must still be 1!
Re: Possible compiler problem
« Reply #10 on: March 23, 2019, 06:47:05 pm »
@ASerge,

Thank you so much, a stupid logic error on my part. However, the error messages and the debug didn't make it easy to find it as even putting in break points and trying to step through the code did't lead me to this line.
Again, thank you.

Dave
Version #:1.8.4 Date 2019-01-08 FPC Version: 3.0.4 and SVN Revision 57972 for x86_64-win64-win32/win64

 

TinyPortal © 2005-2018