Recent

Author Topic: Access Violation  (Read 1587 times)

1HuntnMan

  • Sr. Member
  • ****
  • Posts: 397
  • From Delphi 7 to Lazarus
    • NewFound Photo Art
Access Violation
« 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? 

Khrys

  • Sr. Member
  • ****
  • Posts: 344
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: 7308
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: 397
  • 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: 397
  • 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: 2466
    • 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 -> FPC 3.2.2 -> Lazarus 4.0 up until Jan 2025 from then on it's both above &: KDE6/QT6 -> FPC 3.3.1 -> Lazarus 4.99

Zvoni

  • Hero Member
  • *****
  • Posts: 3138
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: 397
  • 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: 3138
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: 397
  • 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: 2466
    • 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 -> FPC 3.2.2 -> Lazarus 4.0 up until Jan 2025 from then on it's both above &: KDE6/QT6 -> FPC 3.3.1 -> Lazarus 4.99

1HuntnMan

  • Sr. Member
  • ****
  • Posts: 397
  • 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.

 

TinyPortal © 2005-2018