Recent

Author Topic: Break and StringLists  (Read 1578 times)

AMJF

  • New Member
  • *
  • Posts: 30
Break and StringLists
« on: October 25, 2021, 06:20:45 pm »
I have a program that uses StringLists and declares them at Form.Activate.

However, my program uses special configuration files that will terminate if any errors are found, and when the termination happens, I first Free the StringLists, and then Break. But I don't know if Break will execute the Form.Deactivate part of the program, where I have another copy of the Free commands.

My question simply is: Does Break quit the program without executing Form.Deactivate or not?

howardpc

  • Hero Member
  • *****
  • Posts: 3833
Re: Break and StringLists
« Reply #1 on: October 25, 2021, 06:40:18 pm »
Break does not execute anything.
It is simply a syntactical alternative to a goto someLabel instruction, exiting the local (for, while, repeat) loop in which you have placed it.

AMJF

  • New Member
  • *
  • Posts: 30
Re: Break and StringLists
« Reply #2 on: October 25, 2021, 07:26:55 pm »
In that case, it's best if I keep the copy of the Free commands at both the Form.Deactivate and the condition in which the Break is used?

jamie

  • Hero Member
  • *****
  • Posts: 5024
Re: Break and StringLists
« Reply #3 on: October 25, 2021, 07:32:37 pm »
No, Simply use FreeAndNil instead, that will do Free first and then set the pointer to NIL.

Free will test if the pointer is NIL and if so not attempt to free it.

That is one the main reasons FreeAndNil was created..

Also, using Application.Terminate maybe more of what you need.
The only true wisdom is knowing you know nothing

AMJF

  • New Member
  • *
  • Posts: 30
Re: Break and StringLists
« Reply #4 on: October 25, 2021, 08:41:16 pm »
I'm trying Application.Terminate, seems to be working. Only hope it does Free the StringList memory, last thing I want is memory leaks :)

howardpc

  • Hero Member
  • *****
  • Posts: 3833
Re: Break and StringLists
« Reply #5 on: October 25, 2021, 09:41:40 pm »
Application.Terminate will not free stringlists you have manually instantiated. How can the Application object possibly know about them?
Don't use OnActivate to create the stringlists, and don't use OnDeactivate to free them.

Create them in the form's OnCreate, and free them in the form's OnDestroy.
If you have to use OnActivate, use it only to check for the existence and validity of the file(s) to which the stringlists correspond, though I would have thought OnCreate was a better place for this check.

If some error is encountered, post a suitable error message and simply call Close to close the main form, which shuts down the app.

This will then free the stringlists correctly through the form's OnDestroy event handler.
« Last Edit: October 25, 2021, 09:44:31 pm by howardpc »

 

TinyPortal © 2005-2018