Recent

Author Topic: [SOLVED] Access violation when closing application running inside IDE  (Read 994 times)

Eloir

  • New member
  • *
  • Posts: 8
Application runs and closes fine when running standalone (outside IDE).
Trying to track the problem, running step by step, I reach Form.Close(), and there I get this 'access violation' error.
« Last Edit: April 28, 2023, 07:23:27 pm by Eloir »

Eugene Loza

  • Hero Member
  • *****
  • Posts: 729
    • My games in Pascal
Re: Access violation when closing application running inside IDE
« Reply #1 on: April 26, 2023, 08:11:02 pm »
Most often case for such issues is when something "gets freed twice".

E.g. in case you create a list of some items that "owns" those items like

Code: Pascal  [Select][+][-]
  1. MyList = TObjectList.Create(true);
  2. MyClass := TObject.Create;
  3. MyList.Add(MyClass);
  4. MyClass.Free; // now the class is freed and MyList has a dangling pointer inside
  5. MyList.Free; // it tries to call (danglingpointer).Free and crashes

I don't remember if TComponent derivatives automatically handle free notification, if not then e.g.

Code: Pascal  [Select][+][-]
  1. MyClass := TComponent.Create(Form1);
  2. MyClass.Free; // now Form1 has a dangling pointer to "owned" child
  3. Form1.Close; // this will crash, as it will attempt to (danglingpointer).Free

Of course usually it's much more entangled. Like "Class1 has a list inside which contains and owns Object1, and frees the list in the Destructor; Class2 has a list inside which contains and owns Object1 and frees the list in the Destructor; both classes are parented to Form and when form frees, it frees Class1 (which frees list inside, which frees Object1) and then frees Class2 (which frees list inside which tries to free Object1 again and crashes)". These bugs are rather hard to debug, so usually it's a good idea to "remember" what changes started causing this issue - and see what could get screwed with ownership there.
My FOSS games in FreePascal&CastleGameEngine: https://decoherence.itch.io/ (Sources: https://gitlab.com/EugeneLoza)

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 12116
  • Debugger - SynEdit - and more
    • wiki
Re: Access violation when closing application running inside IDE
« Reply #2 on: April 26, 2023, 08:35:32 pm »
Well what exactly does it say when you get the error? What is the dialog title (not the message, but the title - on top, on the top border)?

And does the IDE (debugger) react to it? If you open the "stack window", is there some content?

Eloir

  • New member
  • *
  • Posts: 8
Re: Access violation when closing application running inside IDE
« Reply #3 on: April 28, 2023, 07:21:39 pm »
Thank you for your reply.
I solved this my problem by adding this code on Form.Close() procedure:
Code: Pascal  [Select][+][-]
  1. begin
  2.   CloseAction := caFree;
  3.   Release;
  4. end;
  5.  

 

TinyPortal © 2005-2018