Recent

Author Topic: External ACCESS VIOLATION error accessing a Class declared in another unit  (Read 1566 times)

AgriMensor

  • Jr. Member
  • **
  • Posts: 50
Laz IDE (2.2.6) under Windows 10 repeatedly gives this runtime error, even though it has successfully compiled before, so something must have happened! The Class in question is declared in Unit sbClrMsg:

Code: Pascal  [Select][+][-]
  1. uses
  2.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, ExtCtrls,
  3.   Buttons, Switches, ComCtrls, ECProgressBar, ECSwitch, InterfaceBase, Interfaces,
  4.  
  5.   {WordPlay Additionals}
  6.   BlowFish, ComObj, DOS, ExtDlgs, LCLIntf, LCLType, Math,
  7.   //* OSVerInfo,
  8.   VersionTypes, Variants, Win32Proc;
  9.  
  10. type
  11.  
  12.   { TfrmsbClrDlg }
  13.  
  14.   TfrmsbClrDlg = class(TForm)
  15.     bbtLeft1: TBitBtn;
  16.     bbtMiddle1: TBitBtn;
  17.     bbtRight1: TBitBtn;
  18.     imgMB_ICON: TImage;
  19.     lblText: TLabel;
  20.     lblHdr: TLabel;
  21.     pnlBkgClr: TPanel;
  22.     procedure bbtLeft1Click(Sender: TObject);
  23.     procedure bbtMiddle1Click(Sender: TObject);
  24.     procedure bbtRight1Click(Sender: TObject);
  25.  
  26.   private
  27.  
  28.   public
  29.  
  30.   end;
  31.  
  32. var
  33.   frmsbClrDlg: TfrmsbClrDlg;
  34.  
  35. implementation
  36.  
  37. {$R *.lfm}
  38.  
  39. { TfrmsbClrDlg }
  40.  
  41. uses
  42.  {Placed here rather than in Interface section to avoid circular references}
  43.   WordPlaySuperMsgVersion2;
  44.  
  45. procedure TfrmsbClrDlg.bbtLeft1Click(Sender: TObject);
  46. begin
  47.   sbClrDlgResult := mrYes;
  48. end;
  49.  
  50. procedure TfrmsbClrDlg.bbtMiddle1Click(Sender: TObject);
  51. begin
  52.   sbClrDlgResult := mrNo;
  53. end;
  54.  
  55. procedure TfrmsbClrDlg.bbtRight1Click(Sender: TObject);
  56. begin
  57.   sbClrDlgResult := mrCancel;
  58. end;
  59.  
  60. end.
  61.  

The offending Method in the calling Unit WordPlaySuperMsgVersion2 is this and the offending statement is frmsbClrDlg.lblHdr.Caption := 'Confirm program exit . . .';

Code: Pascal  [Select][+][-]
  1. Procedure TForm1.btnQuitClick(Sender: TObject);
  2. Begin
  3.         Form1.Height := MainFormExtendedHeight; // Retrieve correct height for Main form
  4.         Begin //* Setup the message form, display it and interrogate Modal Result
  5.         frmsbClrDlg.lblHdr.Caption := 'Confirm program exit . . .';
  6.         frmsbClrDlg.lblText.Caption := 'Are you sure you want to'
  7.                            +sLineBreak + 'end the program?';
  8.         frmsbClrDlg.lblText.Font.Height := 20; //* Size the Text
  9.         frmsbClrDlg.imgMB_ICON.Picture.graphic.LoadFromFile(ExtractFilePath(ParamStr(0)) + 'Warning@3x.ico');
  10.         frmsbClrDlg.bbtLeft1.Caption := '&Yes';
  11.         frmsbClrDlg.bbtLeft1.Kind := bkYes;
  12.         frmsbClrDlg.bbtRight1.Caption := '&No';
  13.         frmsbClrDlg.bbtRight1.Kind := bkNo;
  14.         frmsbClrDlg.bbtLeft1.Visible := True;
  15.         frmsbClrDlg.bbtMiddle1.Visible := False;
  16.         frmsbClrDlg.bbtRight1.Visible := True;
  17.         Form1.Hide; //* until next form is dismissed
  18.         frmsbClrDlg.ShowModal; //* Display the next form
  19.         If sbClrDlgResult = mrYes Then Form1.Close Else Form1.Show;
  20.     End;
  21. End;

Can anyone please help identify what I (or the IDE) are doing wrong? Thx!

alpine

  • Hero Member
  • *****
  • Posts: 1391
Is the frmsbClrDlg auto-created in Project Options\Forms?
"I'm sorry Dave, I'm afraid I can't do that."
—HAL 9000

AgriMensor

  • Jr. Member
  • **
  • Posts: 50
Thanks, alpine!.
The answer to your question is "No". I have, however, tried it and I get the following error messages in the .lpr file (WPSMV2.lpr).

AgriMensor

  • Jr. Member
  • **
  • Posts: 50
Compile Project, Target: WPSMV2.exe: Exit code 1, Errors: 2
WPSMV2.lpr(24,26) Error: Identifier not found "TfrmsbClrDlg"
WPSMV2.lpr(24,40) Error: Identifier not found "frmsbClrDlg"

cdbc

  • Hero Member
  • *****
  • Posts: 2214
    • http://www.cdbc.dk
Hi
USES is missing a unit.
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 3.6 up until Jan 2024 from then on it's both above &: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 4.99

AgriMensor

  • Jr. Member
  • **
  • Posts: 50
Thanks, cdbc!

I can't see that a Unit is missing in the calling routine:

Code: Pascal  [Select][+][-]
  1. unit WordPlaySuperMsgVersion2;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6. {Items declared in the INTERFACE section are Global}
  7.  
  8. uses
  9.   {LCL and 3rd-party Components}
  10.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, ExtCtrls,
  11.   ComCtrls, Buttons, ECProgressBar, ECSwitch, InterfaceBase, Interfaces,
  12.   Switches, Types,
  13.   {WordPlay Additionals}
  14.   BlowFish, ComObj, DOS, ExtDlgs, LCLIntf, LCLType, Math, sbClrMsg,
  15.   sbSuperMsg2Unit, Settings,
  16.   //* OSVerInfo,
  17.   VersionTypes, Variants, Win32Proc;

and in the called method (sbClrMsg), the caller appears in a Uses statement in the Implementation section:

Code: Pascal  [Select][+][-]
  1. implementation
  2.  
  3. {$R *.lfm}
  4.  
  5. { TfrmsbClrDlg }
  6.  
  7. uses
  8.  {Place here rather than in Interface section to avoid circular references:}
  9.   WordPlaySuperMsgVersion2;


cdbc

  • Hero Member
  • *****
  • Posts: 2214
    • http://www.cdbc.dk
Hi
Look in "WPSMV2.lpr"...
:: In lazarus:
- 'Project' -> 'View Source'...
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 3.6 up until Jan 2024 from then on it's both above &: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 4.99

AgriMensor

  • Jr. Member
  • **
  • Posts: 50
Hi Benny!

I have tried shoving units from Available forms to Auto-create forms in Project Options --> Forms and in the .lpr file (see my previous response to alpine). Here is the .lpr:

Code: Pascal  [Select][+][-]
  1. program WPSMV2;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. uses
  6.   {$IFDEF UNIX}
  7.   cthreads,
  8.   {$ENDIF}
  9.   {$IFDEF HASAMIGA}
  10.   athreads,
  11.   {$ENDIF}
  12.   Interfaces, // this includes the LCL widgetset
  13.   Forms,
  14.   { you can add units after this }
  15.   WordPlaySuperMsgVersion2;
  16.  
  17. {$R *.res}
  18.  
  19. begin
  20.   RequireDerivedFormResource:=True;
  21.   Application.Scaled:=True;
  22.   Application.Initialize;
  23.   Application.CreateForm(TForm1, Form1);
  24.   Application.Run;
  25. end.

That didn't work (again, see my reply to alpine), so what exactly are you suggesting?

Thanks!

cdbc

  • Hero Member
  • *****
  • Posts: 2214
    • http://www.cdbc.dk
Hi
Well, by the looks of it, you're treating the 'dialogform' as if it was auto-created... It's NOT!
so try this:
Code: Pascal  [Select][+][-]
  1. program WPSMV2;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. uses
  6.   {$IFDEF UNIX}
  7.   cthreads,
  8.   {$ENDIF}
  9.   {$IFDEF HASAMIGA}
  10.   athreads,
  11.   {$ENDIF}
  12.   Interfaces, // this includes the LCL widgetset
  13.   Forms,
  14.   { you can add units after this }
  15.   WordPlaySuperMsgVersion2; // this provides the dlgform
  16.  
  17. {$R *.res}
  18.  
  19. begin
  20.   RequireDerivedFormResource:=True;
  21.   Application.Scaled:=True;
  22.   Application.Initialize;
  23.   Application.CreateForm(TForm1, Form1);
  24.   Application.CreateForm(TfrmsbClrDlg, frmsbClrDlg); // HERE
  25.   Application.Run;
  26. end.
If I've got the names right, this should get you out of trouble...  :D
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 3.6 up until Jan 2024 from then on it's both above &: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 4.99

AgriMensor

  • Jr. Member
  • **
  • Posts: 50
Thanks for your persistence in trying to help, Benny!  :-)
Unfortunately, although I have tried your suggestion again, this didn't get me out of trouble as it duplicates exactly what I tried before in my response to the post from alpine, i.e. it gives the following error messages:

Compile Project, Target: WPSMV2.exe: Exit code 1, Errors: 2
WPSMV2.lpr(24,26) Error: Identifier not found "TfrmsbClrDlg"
WPSMV2.lpr(24,40) Error: Identifier not found "frmsbClrDlg"

I think I'll try to  manually create the form and see how that goes, but if you (or other forum members) can suggest something else, I'd be grateful!
The code has worked OK before, so I've probably lost something important along the way when I started a new project in a clean directory. But, I've no ide what! :-(

Steve

cdbc

  • Hero Member
  • *****
  • Posts: 2214
    • http://www.cdbc.dk
Hi Steve
Did you by any chance move those type & variable to 'implementation', i.e.: 'out of sight'?!?
...Otherwise, you'll have to upload a zip with the offending parts, preferably compilable, so we can see / touch for ourselves... Most of the times it's just a small move of code or an oversight...
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 3.6 up until Jan 2024 from then on it's both above &: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 4.99

alpine

  • Hero Member
  • *****
  • Posts: 1391
Quote
Compile Project, Target: WPSMV2.exe: Exit code 1, Errors: 2
WPSMV2.lpr(24,26) Error: Identifier not found "TfrmsbClrDlg"
WPSMV2.lpr(24,40) Error: Identifier not found "frmsbClrDlg"

You don't have sbClrMsg
included in the uses section of WPSMV2.lpr. (Which is parsed as represents the project).

Benny pointed it out in reply #4.

How did this source file sbClrMsg.pas even get into your project? Copied from another folder? If so - make sure that every time you do this you add the file into the Project Inspector.
And then, if you're not certain how exactly to create/use/free the form, just list it into 'Auto-create forms:'.

« Last Edit: May 18, 2025, 11:49:22 am by alpine »
"I'm sorry Dave, I'm afraid I can't do that."
—HAL 9000

AgriMensor

  • Jr. Member
  • **
  • Posts: 50
Hi Benny!

Thx for the new post and  helpful comments!
As I have previously posted, the only reference appearing in an Implementation section is this, which is in the sbClrMsg unit:

Code: Pascal  [Select][+][-]
  1. var
  2.   frmsbClrDlg: TfrmsbClrDlg;
  3.  
  4. implementation
  5.  
  6. {$R *.lfm}
  7.  
  8. { TfrmsbClrDlg }
  9.  
  10. uses
  11.  {Placed here rather than in Interface section to avoid circular references:}
  12.   WordPlaySuperMsgVersion2;

Thx!

cdbc

  • Hero Member
  • *****
  • Posts: 2214
    • http://www.cdbc.dk
Hi
Na! Then try this:
Code: Pascal  [Select][+][-]
  1. program WPSMV2;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. uses
  6.   {$IFDEF UNIX}
  7.   cthreads,
  8.   {$ENDIF}
  9.   {$IFDEF HASAMIGA}
  10.   athreads,
  11.   {$ENDIF}
  12.   Interfaces, // this includes the LCL widgetset
  13.   Forms,
  14.   sbClrMsg,  // <-- HERE
  15.   WordPlaySuperMsgVersion2;
  16.  
  17. {$R *.res}
  18.  
  19. begin
  20.   RequireDerivedFormResource:=True;
  21.   Application.Scaled:=True;
  22.   Application.Initialize;
  23.   Application.CreateForm(TForm1, Form1);
  24.   Application.CreateForm(TfrmsbClrDlg, frmsbClrDlg); // HERE
  25.   Application.Run;
  26. end.
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 3.6 up until Jan 2024 from then on it's both above &: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 4.99

AgriMensor

  • Jr. Member
  • **
  • Posts: 50
Hi alpine, Hi Benny!

Thank you both very much for your perseverance and help with this issue :-)

Of course, you've both come up with the same solution - my incompetence in missing that I'd left out sbClrMsg in the .lpr Uses statement. How many times have I stared at it and not noticed??? Urgh - a schoolboy error on my part from someone who started using Pascal when Delphi 2 was a boy!!!

Code: Text  [Select][+][-]
  1. "How did this source file sbClrMsg.pas even get into your project? Copied from another folder? If so - make sure that every time you do this you add the file into the Project Inspector.
  2. And then, if you're not certain how exactly to create/use/free the form, just list it into 'Auto-create forms:'.

It got into the project because I save most of my code in a backup repository of .txt files. I copied and pasted several of these Units into the new project Units and removed those Units names in the Uses statements when they were no longer necessary. I must have accidently deleted "sbClrMsg," when I was doing this - that's the only thing I can think of. That's no excuse for not having spotted such a simple error though :-(

It's now working fine. Once again, many thanks to you both :-)

Steve


 

TinyPortal © 2005-2018