Recent

Author Topic: [Solved] Initialization routine problem  (Read 4383 times)

JFistere

  • New Member
  • *
  • Posts: 25
[Solved] Initialization routine problem
« on: May 05, 2016, 02:17:18 am »
I have a routine at the end of my main routine (Is an Initialization routine part of Free Pascal?) that reads initialization variables and fills in Text fields. It runs fine until I get to the line:

Edit1.Text =: IniLine;

These attempts also failed:

Form1.Edit1.Text =: IniLine;                            I really expected this form to work       Error running: External: SIGSEGV
System.Form1.Edit1.Text =: IniLine;                       Error Compiling: Form1 not found

What am I doing wrong?

« Last Edit: May 10, 2016, 02:16:46 am by JFistere »

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: Initialization routine problem
« Reply #1 on: May 05, 2016, 03:02:59 am »
What am I doing wrong?
For instance it is ":=" and not "=:"

Other than that, it is impossible to tell as you left out the complete context of your code.

Not being able to access a component on a form directly, means you have not placed your code inside one of the form's methods.

Getting a sigv when using the Form1 name, means the form isn't initialized yet.

Unit "system" is a unit that has no knowledge of your Forms whatsoever.

And then there's a small matter of what IniLine actually is. You neglected to mention that fact as well.

Please help yourself and show some compilable code, in its smallest form possible and which is causing you such issues.

It is _very_ tiresome to invoke psychic powers in order to look at the same screen you are looking at (or look into your mind, for that matter).

JFistere

  • New Member
  • *
  • Posts: 25
Re: Initialization routine problem
« Reply #2 on: May 05, 2016, 07:13:31 am »
I stand properly chastised. Back to you in a few moments.

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: Initialization routine problem
« Reply #3 on: May 05, 2016, 07:46:50 am »
Quote
I stand properly chastised.
My words may have sounded harsh, but it really is the only (possible) way to get a proper answer to your question(s).

It is/was in no way meant to punish you (the compiler will do that to you more than enough  :-X )

Quote
Back to you in a few moments.
Please take your time, and in case it is not possible to get something to compile or only crashes when run, then simply show what you already got (context), explain what you intended to do (your goal) and in case it failed why it doesn't work (e.g. compiler error or run-time error or otherwise)

JFistere

  • New Member
  • *
  • Posts: 25
Re: Initialization routine problem
« Reply #4 on: May 05, 2016, 08:47:09 am »
I was not the least upset or offended in the least by your statement, which I took the way you meant.

Here's my Initiaization routine:
Code: Pascal  [Select][+][-]
  1. Initialization
  2.  
  3. begin
  4.   ShowMessage('Initialing...');
  5.   IniFilename := 'MultiSearchIni.txt';
  6.   AssignFile(IniFile,IniFilename);
  7.   Reset(IniFile);
  8.   Readln(IniFile,IniLine);     //  DataFile name
  9.   ShowMessage(IniLine);
  10.   Form1.Edit2.Text:= IniLine;
  11.   Readln(IniFile,IniLine);     // Search string
  12.   ShowMessage(IniLine);
  13.   Form1.Edit1.Text := IniLine;
  14.   System.Close(IniFile);
  15. end;
  16.  
  17. end.
  18.  

It runs until it tries to write Edit2.Text, when I get a SIGSEGV error.    (Think that was the code but I can't reproduce it at the moment.)

But, for some reason, in my latest build it doesn't produce that error but gets into an initialization loop with no error.

I've attached the main routine in case you want to take a look at it.

...........

I've worked with it some more, and the SISSEGV now occurs only once after restarting the whole system. The iteration behavior seems to have something to do with the fact that the program does not shut down properly after the error. I'm quitting for the night, and I'll be working on it tomorrow afternoon.

Thanks for your help!
« Last Edit: May 05, 2016, 09:14:12 am by JFistere »

Awkward

  • Full Member
  • ***
  • Posts: 135
Re: Initialization routine problem
« Reply #5 on: May 05, 2016, 08:58:56 am »
i think, Edit2 control not exists in Initialization section because  form is not created yet.

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: Initialization routine problem
« Reply #6 on: May 05, 2016, 09:04:53 am »
Thank you for having given your 'issue' a understandable context.

(i am assuming here that you did not do anything specific to your main project source-code (.lpr file).

Let's start with some theory. Here in the reference manual we can read:
Quote
The initialization block is used to initialize certain variables or execute code that is necessary for the correct functioning of the unit. The initialization parts of the units are executed in the order that the compiler loaded the units when compiling a program. They are executed before the first statement of the program is executed.

What that means, is that the code you placed inside the unit's initialization section is executed _before_ the main application is started. The main application (.lpr project file) takes care of initializing the Application object and creates (and then adds) your custom Forms to the Application.

That also means that at the time your code inside the initialization section is executed, none of the visual components are 'valid', and therefor can't be used.

fwiw: the usage of "Form1.Edit2.Text:= IniLine;" is the correct one in this context, but as said, the form does nto exist yet, unfortunately resulting in a sigv error.


But, there are several ways to 'overcome' this issue:
1) on your main form create a OnCreate Event handler, and place your code inside there
2) Still use your initialization section, but store your string in a temp variable that you use later on to be displayed on your form (inside your edit control). You can also use the OnCreate event of your Form for that.

I would personally opt for option 1, as that let's you experience better how the OOP concept of Lazarus works.

PS: here is something you perhaps might like  :D
« Last Edit: May 05, 2016, 09:13:26 am by molly »

JFistere

  • New Member
  • *
  • Posts: 25
Re: Initialization routine problem
« Reply #7 on: May 10, 2016, 02:15:32 am »
I used method 1) and it works just fine. Thanks.

I wanted to attach my code in case you needed it, but I never located the correct files. Earlier you mentioned attaching .lpr files but a. The attached explanatory note does not allow that file type and b. I couldn't find them anyway. Any clarification will be appreciated. (Since is not really a new programming question, I felt it was okay to attach my question to another question!)

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: [Solved] Initialization routine problem
« Reply #8 on: May 10, 2016, 02:40:26 am »
afaik it is perfectly ok to ask such side questions in the same topic (after all it is more or less related).

Since i saw you asking in the other topic as well. If you use lazarus to program with, then you (hopefully) have created a new project. Lazarus keeps track of all the files for you that way.

There is an option inside lazarus editor that lets you 'export' all files required so that someone else is able to compile the project, or in case you want to relocate your projects.

The function can be found inside Lazarus under project/publish project. It will open a dialog in which you can change some setting and tell to which location you would like to publish the files belonging to your project.

tbh, i am not aware that the online attachment editor refuses to allow .lpr files (it might very well be the case, i don't use it often myself). Usually a complete project is too big anyhows, so the best thing to do would be to zip up the published files into a single file and attach that to your post.

As an advise to a new user: try to create a new directory for each new project so that a project is located inside it's own directory. It certainly will circumvent some headaches when your projects grow in number of files and/or size.

In case you want to see the contents of your .lpr (project) file, please see menu option projects/View project source. It will open your .lpr project file's content into the lazarus editor. It is more or less 'hidden' as usually you don't have any usage for editing that file (unless wanting to customize application startup). Be aware that 'messing' around with the contents of a project file _might_ confuse lazarus code-editor tools (e.g. sometimes it can lose track of auto opening forms).

 

TinyPortal © 2005-2018