Recent

Author Topic: [SOLVED] Access Violation - Database is Locked  (Read 3971 times)

1HuntnMan

  • Sr. Member
  • ****
  • Posts: 421
  • From Delphi 7 to Lazarus
    • NewFound Photo Art
[SOLVED] Access Violation - Database is Locked
« on: September 26, 2025, 06:21:12 pm »
Doing testing on an application that's mostly finished.  Testing adding, deleting and editing records. But, after maybe adding 3 new records to one form, i.e. Clients or adding a couple of Reference records. I get an access violation-Ok/Abort.  Have to abort because it's locked if you click OK. Reading online it can be caused by multiple issues: null pointers (dereferencing, dangles, uninitialized pointers). Memory mgt.??? Tried step-by-step but that's almost a waste of time.

Anyone have any advice how to home in on the issue? 
« Last Edit: December 21, 2025, 03:19:43 pm by 1HuntnMan »

Khrys

  • Sr. Member
  • ****
  • Posts: 376
Re: Access Violation
« Reply #1 on: September 29, 2025, 06:59:47 am »
Run it with the debugger; the program will stop at the line where the AV occurs (make sure to compile with debug information enabled and with  -O1  or even  -O0).
If you still can't figure out the root cause, take a look at valgrind (compile with  -gv).
If that still doesn't cut it, you might want to compile FPC with LLVM support enabled and enable Address Sanitizer (ASAN) with  -Clfsanitize=address.

jamie

  • Hero Member
  • *****
  • Posts: 7486
Re: Access Violation
« Reply #2 on: September 30, 2025, 02:22:47 am »
Enable HeapTRc and then add only one record or perform one duty just prior to where you know it's going to crash on the next step and exit the app, read the Heap report for leaks.

 You can also try this with just starting and exiting the app to see if you have memory still alive, which shouldn't be.

  You could also be doing some buffer overruns during your app which is corrupting your memory space.

 Etc. The list goes on, but ensure you have HeapTrc running so you can check the memory report on exit, having some sort of leak indication of unclean memory can mean a couple of things, you forgot to free something, which isn't too bad or your code is doing something that corrupts your code and messes around with the head manager, which will give you bad memory reports at times and or just overwrite your code.

 Other things like doing multi-threading accessing common LCL code from more than a single thread can do nice things to your code, too, along with running timers.

Jamie
The only true wisdom is knowing you know nothing

1HuntnMan

  • Sr. Member
  • ****
  • Posts: 421
  • From Delphi 7 to Lazarus
    • NewFound Photo Art
Re: Access Violation
« Reply #3 on: October 17, 2025, 09:05:19 pm »
Back to a point where I need to figure out these ACCESS VIOLATION's, i.e. Project raised exception class 'External: ACCESS VIOLATION' with message: Access violation executing address $00000000000000, I didn't count the zeros.
This doesn't mean anything to me. For example, some of my forms in the app have no issue but a few do for example my Clients form, I can test, add, delete, etc. testing. No issue. I can save/post or just launch it and not make any changes.  But, if I even just execute this form from the main form the second time I get ACCESS VIOLATION error above when I close it the second time and have to close the application.  Here's my code for the FormClose procedure at the bottom.

Khrys mentioned using valgrind (compile with  -gv). I'm going to download ValGrind but also when Khrys advised compile with -gv. Where do you add the -gv parameters?  Is that in Run Parameters in Command line parameters, just add -gv?

Code: Pascal  [Select][+][-]
  1. procedure TFrmClientsMgt.FormClose(Sender: TObject;
  2.   var CloseAction: TCloseAction);
  3. begin
  4.   DSrcClients.OnDataChange:= Nil;
  5.   QryClients.AfterPost:= Nil;
  6.   QryClients.AfterDelete:= Nil;
  7.   QryCntks.DataSource:= Nil;
  8.   keys.Free;
  9.   QryCountClnts.Close;
  10.   with QryClients do
  11.     begin
  12.       if State in [dsEdit, dsInsert] then
  13.         if MessageDLg('Changes pending, save current updates?', mtConfirmation,
  14.          [mbYes, mbNo],0) = mrYes then
  15.           begin
  16.             Post;
  17.             ApplyUpdates;
  18.             PMSDataModule.TransPMSDB.CommitRetaining;
  19.           end
  20.       else Cancel;
  21.     end;
  22.   QryCntks.Close;
  23.   QryClients.Close;
  24.   PMSDataModule.CloseLookUpTables;
  25.   //if (cDestroying in ComponentState) then Exit; //=> error cDestroying...
  26. end;
  27.  

Also, when I'm testing about every other time I exit without making any modifications, the main form which is just a menu with SpeedButtons for the various parts of the application, I will get an access violation with a different address...

1HuntnMan

  • Sr. Member
  • ****
  • Posts: 421
  • From Delphi 7 to Lazarus
    • NewFound Photo Art
Re: Access Violation
« Reply #4 on: October 17, 2025, 09:11:55 pm »
Okay, just figured out that valgrind is for Linux.  I'll look at something similar for Winders 11...
Tks

cdbc

  • Hero Member
  • *****
  • Posts: 2561
    • http://www.cdbc.dk
Re: Access Violation
« Reply #5 on: October 17, 2025, 09:46:33 pm »
Hi
Quote
Project raised exception class 'External: ACCESS VIOLATION' with message: Access violation executing address $00000000000000
This address is equivalent to NIL ~ you're calling something/method of a class that is Free'd / unassigned / never assigned / FreeAndNil'ed etc...
Try stepping the debugger until you reach this error, around there, will be something that is nil, got free'd on form closing, so the 2. time it's nil  %)
Do you close or disconnect you db-components on form-close?!?
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE6/QT6 -> FPC Release -> Lazarus Release &  FPC Main -> Lazarus Main

Zvoni

  • Hero Member
  • *****
  • Posts: 3226
Re: Access Violation
« Reply #6 on: October 20, 2025, 08:15:29 am »
Don't Close/Free objects in Form-Close.
That belongs to FormDestroy.

And why in blazes are you setting the Events (and one Property) to Nil???
Don't.

The only fishy thing i see:
Line 5 you Nil AfterPost
Line 16 you do Post
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

1HuntnMan

  • Sr. Member
  • ****
  • Posts: 421
  • From Delphi 7 to Lazarus
    • NewFound Photo Art
Re: Access Violation
« Reply #7 on: October 21, 2025, 09:35:15 pm »
Okay, thanks for all the help.  No more issues. After getting rid of Nil'ing. I overkilling, LoL.

But, I still get an Access Violation most of the time when I'm running and testing the application and closing the Main Form. The Main Form shouldn't have any Queries, tables, etc. Those are all closed when exiting/closing a particular form.  The Main Form is just a menu File - Tools - Help and Speedbtns under the menu across the top to access Clients, Contacts, Appointments, etc.  I have one Close btn to close the app.  I'm checking to make sure no tables are open, etc.  The error is always "Project PMS raised exception class 'External ACCESS VIOLATION" with message: Access violation executing address $00000000064DC530 at address 64DC530. Which doesn't mean anything to me. But, if I run the application from the .exe file not within Laz, I usually never get this Access Violation but once in awhile.  The last time I saw it was a week ago. So, I'm continue finishing and then attack this issue.

Zvoni

  • Hero Member
  • *****
  • Posts: 3226
Re: Access Violation
« Reply #8 on: October 22, 2025, 09:06:56 am »
Okay, thanks for all the help.  No more issues. After getting rid of Nil'ing. I overkilling, LoL.

But, I still get an Access Violation most of the time when I'm running and testing the application and closing the Main Form. The Main Form shouldn't have any Queries, tables, etc. Those are all closed when exiting/closing a particular form.  The Main Form is just a menu File - Tools - Help and Speedbtns under the menu across the top to access Clients, Contacts, Appointments, etc.  I have one Close btn to close the app.  I'm checking to make sure no tables are open, etc.  The error is always "Project PMS raised exception class 'External ACCESS VIOLATION" with message: Access violation executing address $00000000064DC530 at address 64DC530. Which doesn't mean anything to me. But, if I run the application from the .exe file not within Laz, I usually never get this Access Violation but once in awhile.  The last time I saw it was a week ago. So, I'm continue finishing and then attack this issue.
As has been said many times here: activate the Debugger (gdb or fpdebug), and you can see which line causes it
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

1HuntnMan

  • Sr. Member
  • ****
  • Posts: 421
  • From Delphi 7 to Lazarus
    • NewFound Photo Art
Re: Access Violation
« Reply #9 on: October 22, 2025, 03:20:07 pm »
Okay, activate the debugger?? Haven't tried this but researching. My Build mode is Default, no Checks and assertion, Run uses the debugger is Checked, Generate info for the debugger is checked, Debugger info is set to Dwarf 3 (beta) (-gw3) with other choices Dwarf 2 (-gw2), Dwarf 2 with sets and Stabs (-gs).  I need to research and learn the correct way when I get back to these issue after finishing the other forms.  Thanks...

cdbc

  • Hero Member
  • *****
  • Posts: 2561
    • http://www.cdbc.dk
Re: Access Violation
« Reply #10 on: October 22, 2025, 03:33:02 pm »
Hi
The default values are pretty decent, just use them as is... It should work right out of the box \o/
You're set to begin debugging, just for kicks - click in the left gutter at a codeline in a method, you should see a red spot marking the breakpoint, where execution will stop... when it stops hover the mouse over some value... ;D
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE6/QT6 -> FPC Release -> Lazarus Release &  FPC Main -> Lazarus Main

1HuntnMan

  • Sr. Member
  • ****
  • Posts: 421
  • From Delphi 7 to Lazarus
    • NewFound Photo Art
Re: Access Violation
« Reply #11 on: October 22, 2025, 07:38:02 pm »
Benny, thanks.  I have seen the red dot before but thought it was something to do when I was stepping thru after Run to Cursor. I probably had accidentally click in the gutter.  Alrighty then, thanks.

1HuntnMan

  • Sr. Member
  • ****
  • Posts: 421
  • From Delphi 7 to Lazarus
    • NewFound Photo Art
Re: Access Violation
« Reply #12 on: November 13, 2025, 06:22:12 pm »
What is a dummy explanation for including this in a DataModule just after the Implementation and uses:

{%CLASSGROUP 'System.Classes.TPersistent'}

I'm trying to eliminate these DataBase is Locked and Access Violations in this application.  I'm thinking to start all over.  Also, what's the difference between a DataModule and just a form/unit.  Basically what I use the DataModule for is for the Connection and Transaction for the rest of the application. But, all the queries/datasouces for LookUp Tables I have on the DataModule, i.e. Countries, States, Payment Status, Priorities, etc.  These little tables are just lookup's for populating data to other tables on forms such as Clients Mgt., Contacts Mgt., Appointments, Proposals, Sales Orders, etc.  these forms use the DataModule for the Connection/Transaction. So, to sum it up, DataModule is where the Connection/Transaction is set, then the queries/datasources are all on the DataModule for the LookUp tables.

Clients, Contacts, Appointments, etc. have their own Queries/DataSources on their forms.  I USE the datamodule for the connection/trans and for DropDownLookUps for a Country/States, etc.  Here's the LPR:

Code: Pascal  [Select][+][-]
  1. program PMS;
  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.   { The PMS units are listed below ... }
  15.   PMSDM, PMSMain, datetimectrls, printer4lazarus, lazcontrols;
  16.  
  17.  
  18. {$R *.res}
  19.  
  20. begin
  21.   RequireDerivedFormResource:=True;
  22.   Application.Scaled:=True;
  23.   Application.Initialize;
  24.   Application.CreateForm(TPMSDataModule, PMSDataModule);
  25.   Application.CreateForm(TFrmPMSMain, FrmPMSMain);
  26.   Application.Run;
  27. end.
  28. { EoF: PMS.lpr }
  29.  

Here's the new LPR:
Code: Pascal  [Select][+][-]
  1. program PMS;
  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.   { The Main PMS unit is listed below... }
  15.   PMSDataBase, PMSMain;
  16.  
  17. {$R *.res}
  18.  
  19. begin
  20.   RequireDerivedFormResource:=True;
  21.   Application.Scaled:=True;
  22.   {$PUSH}{$WARN 5044 OFF}
  23.   Application.MainFormOnTaskbar:=True;
  24.   {$POP}
  25.   Application.Initialize;
  26.   Application.CreateForm(TFrmPMSData, FrmPMSData);
  27.   Application.CreateForm(TFrmPMSMain, FrmPMSMain);
  28.   Application.Run;
  29. end.
  30.  

Why a DataModule???

Lulu

  • Sr. Member
  • ****
  • Posts: 362
Re: Access Violation
« Reply #13 on: November 13, 2025, 07:29:53 pm »
Hi, I see i one thing that can produce issue: the data module is created BEFORE the main form.
Someone more experimented than me can say if it is ?
wishing you a nice life!
GitHub repositories https://github.com/Lulu04

1HuntnMan

  • Sr. Member
  • ****
  • Posts: 421
  • From Delphi 7 to Lazarus
    • NewFound Photo Art
Re: Access Violation
« Reply #14 on: November 13, 2025, 10:27:57 pm »
Well, I learned a while back that the DataModule should be launched first, then the Main Form. But, can't remember, it's been more than a year ago. But, back then I had issues with a different application. But, it was small and just ditched the DataModule and put Connection and Transaction on the main form and the other couple of forms I put the Queries and DataSources on the those particular forms called by the main form.  This application has 7 forms called by the main form. And as I was designing with a DataModule, when testing after awhile I started getting Access Violations and sometimes Database is Locked.  I run stepping thru and the Database is Locked just after I exited the Main Form.

So, I'm testing to use just a form/unit instead of a DataModule.  I could just skip and put all the lookup tables on the main form but quite cluttered.  Also, I designed this about a year ago with TDbf tables, never had issues with Access Violations.

I'll be back later and going to start over and test every step of the way...

 

TinyPortal © 2005-2018