Recent

Author Topic: I cannot save an unit on adding 3 new lines  (Read 1081 times)

Nicole

  • Hero Member
  • *****
  • Posts: 1303
I cannot save an unit on adding 3 new lines
« on: December 08, 2025, 05:03:21 pm »
There is something I never have seen before:

Setup: Lazarus runs within a WIn7-VM in VMWare. The host is Win 10 and all of this is really stable since many months.

Today, business as normal:
I have an unit and want to add another method to it. I do what I usually do, add method to the class, set parameters.
Did something else and planned to work on later. All normal. The VM was left alone in the background.


Suddenly I get the message my VMWare "does not react any more". ok, I rarely had this before, usually it works quite stable. It stabilized after closing some copy-jobs.
This stabilizing was hard as strg + alt + Enrf did not react any more.

Suddenly it stabilized and I tried to save my work within Lazarus

It looks like this (no content now):
function YahooTicker(ware: string; monat, jahr: integer;  Boerse: String= 'CBT'): string;
begin
end;


I hit "save", - and then it the lines with the function were gone from interface and implementation.
The unit is just not saved with these lines.
There is just the old version of it. Tried many, many times.
I tricked it, I could save it, but a different function "dissappeard" out of the unit instead.
The unit? Very normal, slime, about 500 lines.

Something very severe seems to have happened, but WHAT?

This I did:
- I restarted the IDE
- I restarted the VM
- I restarted the operating system within the VM
- I shut down the operating system and restarted it.
- I restarted the host's system
- I checked the rights for writing
- I checked the free space of the HD of host and VM
- I deleted all the files in x86_64-win64
- I closed all open files in the IDE and re-opened it
- I deleted the lps and restarted an re-opened it
- I tried adding a method to a differenct class: saving worked


What the.....?

Any ideas?
(Lazarus 4.0)

Attached I add them, is there anywhere a comma too many?!
Everythings compiles fine. However the effect is the same: As compiling means saving, the lines are gone after it.

=================

add after menu hours of frustration - which persists:
The problem comes probably from the "save"-function of the Laz 4.0 IDE.
I could edit and re-open the *.pas by notepad++
The strange thing: The problem is just there, if I save code, not if I save comments.

=======================

several hours later:
I ugraded to version 4.4. - just the same problem:
The moment I "save" an unit, the just edited methods disappear again.

HELP!

« Last Edit: December 08, 2025, 07:22:37 pm by Nicole »

jamie

  • Hero Member
  • *****
  • Posts: 7490
Re: I cannot save an unit on adding 3 new lines
« Reply #1 on: December 08, 2025, 08:21:12 pm »
Sounds like a read only status where u r storing your files
You read but notwrite.
The only true wisdom is knowing you know nothing

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 12015
  • Debugger - SynEdit - and more
    • wiki
Re: I cannot save an unit on adding 3 new lines
« Reply #2 on: December 08, 2025, 08:40:14 pm »
Well, I can open the unit with no problem (other than a tirade of issues about me not having IBQuery).

And that could be one possibility, even though I would not be sure how it links to "only text changed". But, I don't know if saving the file (which needs to ensure the lfm is uptodate), may for some reason trigger (write or at least read) access to the form in the designer, including the instance of IBQuery. Which leaves the chance that this could be doing anything. I.e. if at the time it could not connect to the db, but would try to, and block.

Of course that is just one possibility...

---------------------------

You could try to run the IDE with
lazarus.exe --debug-log=c:\log.txt

Or recompile the IDE with   -WC  and then watch the console window it will have for any error it may print when the issue happens.

Nicole

  • Hero Member
  • *****
  • Posts: 1303
Re: I cannot save an unit on adding 3 new lines
« Reply #3 on: December 08, 2025, 09:54:41 pm »
here is a very new frame without queries.

I cannot stop wondering!
And it is tough for me, - I cannot work on, the whole day today was wasted.

Looks at the screenshot, I hardly can believe what I wrote there.

Now it is Lazarus 4.4. instead of 4.0.
Just the same!!!

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 12015
  • Debugger - SynEdit - and more
    • wiki
Re: I cannot save an unit on adding 3 new lines
« Reply #4 on: December 08, 2025, 10:23:25 pm »
Ah, now....(Ah, jetzt, ja)

Remove empty methods. If you double clicked some event in the designer, and never wrote code for it...

The option is under: Tools > Options
Editor > Completion and hints
"auto remove empty methods"


And yes, you method isn't from the designer. But you wrote it in the top section of the class (the one without any public,protected,private) => i.e the published one that is for use by the designer.

Best to write any code of your own into a public/protected or private section of the form.

Bart

  • Hero Member
  • *****
  • Posts: 5666
    • Bart en Mariska's Webstek
Re: I cannot save an unit on adding 3 new lines
« Reply #5 on: December 08, 2025, 10:29:57 pm »
Yeah, annoying.
Delphi (at least up to D7) did that, you couldn't turn it of.
I always added a comment to those methods like:
Code: Pascal  [Select][+][-]
  1. procedure Button1Click(Sender: TObject);
  2. begin
  3.   //
  4. end;
That way, Delphi left it in there.

Bart

wp

  • Hero Member
  • *****
  • Posts: 13334
Re: I cannot save an unit on adding 3 new lines
« Reply #6 on: December 08, 2025, 10:30:47 pm »
"auto remove empty methods" is one of the features that I always turn on when installing a new Lazarus. In order not to lose the empty event handlers that I create from the object inspector I always add a double-slash as comment between the "begin" and "end" of the handler. This way the method is no longer "empty" and will not be removed any more when I incidentally save the form before entering real code.
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. begin
  3.   //              
  4. end;

And Martin is correct: The IDE acts only upon methods in the published section (the part above "private"). But there is really no need to declare anything in this section - consider it to be reserved for the IDE.
« Last Edit: December 08, 2025, 10:38:11 pm by wp »

Bart

  • Hero Member
  • *****
  • Posts: 5666
    • Bart en Mariska's Webstek
Re: I cannot save an unit on adding 3 new lines
« Reply #7 on: December 09, 2025, 09:24:53 am »
@wp: seconds apart and we come up with the same "solution"  :)

Bart

Nicole

  • Hero Member
  • *****
  • Posts: 1303
Re: I cannot save an unit on adding 3 new lines
« Reply #8 on: December 09, 2025, 10:45:47 am »
OMG - I feel like an idiot.
Originally the lost method was not empty.
I trapped myself in "reducing the problem to the essential" and forgot about this feature.

My problem seems to have been solved by all my actions and I did not even recognize that because of testing on and on with empty functions.

Thank you all for your patience and NOT forgetting about this old option!
The good news are, that I upgraded to version 4.4.

==========================

about this "where to write a method"

I am aware that there are sections private, public, published...
As I several time had troubles, because an existing method was not reachable in code I did not write, - I never use this sections.

I cannot se an advantage for them?
If there is a good reason, why I shall to it, - I am willing to learn.









Zvoni

  • Hero Member
  • *****
  • Posts: 3227
Re: I cannot save an unit on adding 3 new lines
« Reply #9 on: December 09, 2025, 11:15:54 am »
about this "where to write a method"

I am aware that there are sections private, public, published...
As I several time had troubles, because an existing method was not reachable in code I did not write, - I never use this sections.

I cannot se an advantage for them?
If there is a good reason, why I shall to it, - I am willing to learn.
Rule of thumb:
Any Procedures, Functions, Properties, simple Members of your own belong into Private/Protected/Public Sections
Exception: Your own Event-Handlers (especially for Forms) --> I put those into the "regular" Section, where the IDE puts the "regular" Events

Code: Pascal  [Select][+][-]
  1. TMyForm=Class(TForm)
  2.   btn1:TButton;
  3. ....
  4.   Procedure bnt1Click(Sender:TObject);  //Inserted by Lazarus, if you click in Object-Inspector
  5.   Procedure OnClick(Sender:TObject); //--> My Own Handler!!
  6.  
  7. //From here my own Stuff
  8. Private
  9.   FMyInteger:Integer;
  10. Protected
  11.   Procedure SetValue(AValue:Integer);
  12.   Function GetValue:Integer;
  13. Public
  14.   Property MyInteger:Integer Read GetValue Write SetValue;
  15. .....
  16. End;
  17.  
« Last Edit: December 09, 2025, 11:17:55 am by Zvoni »
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

creaothceann

  • Sr. Member
  • ****
  • Posts: 250
Re: I cannot save an unit on adding 3 new lines
« Reply #10 on: December 09, 2025, 11:44:10 am »
I am aware that there are sections private, public, published... As I several time had troubles, because an existing method was not reachable in code I did not write, - I never use these sections. I cannot see an advantage for them?

A field in your class may have a limited valid range, for example it could be defined as AnsiChar but should only contain alphanumerical characters in certain circumstances. Code from outside of the class could store another character in that field, and the program state/behavior would now be undefined.

Another possibility is that changing one field or calling a certain method should update several fields at once and/or call other internal methods. Code from outside of the class is probably better off not doing these implementation details "manually" all throughout the codebase.

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 12015
  • Debugger - SynEdit - and more
    • wiki
Re: I cannot save an unit on adding 3 new lines
« Reply #11 on: December 09, 2025, 01:25:15 pm »
I am aware that there are sections private, public, published...
As I several time had troubles, because an existing method was not reachable in code I did not write, - I never use this sections.

I cannot se an advantage for them?
If there is a good reason, why I shall to it, - I am willing to learn.

Well first of all: The top section (the one after class, without any marker) belongs to the IDE.
If you don't want the IDE to play with your code in unexpected ways, then use any other section.


Also that top section is "published" (at least in forms and similar). And that means it produces extra data, that goes into your final exe. But your code doesn't benefit from that data. So you just add extra bytes. (It's called RTTI...)




Otherwise you can put all you need into "Public".

But other section can provide peace of mind. Example.
You have a function
  procedure ChangeInternalFlags;

If this is called by accident, all kind of odd and hard to trace side effects may arise.

And then you have
  procedure SafelyChangeInternalFlags;
  procedure RevertInternalFlags;
  procedure UpdateInternalFlags;

Those have checks, and ensure that ChangeInternalFlags is only called when that is correct.

So only those 3 functions should be allowed to call ChangeInternalFlags. All other code must call one of those three.

Then you do want
  ChangeInternalFlags
to be private (actually even "strict private")
that way the compiler will tell you if you accidentally try to call it from elsewhere.


I have in some cases even gone to the trouble of doing a nested structure, to protect the field even more... But that is a truly rare case.






Zvoni

  • Hero Member
  • *****
  • Posts: 3227
Re: I cannot save an unit on adding 3 new lines
« Reply #12 on: December 09, 2025, 01:30:35 pm »
adding to my post above after reading martin's answer:
I put the Event-Handler in the "Top" Section (see martin's answer) if i want to assign it via the Object-Inspector!!
If i assign via code it goes into Private/Protected/Public
« Last Edit: December 10, 2025, 09:06:45 am by Zvoni »
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

Nicole

  • Hero Member
  • *****
  • Posts: 1303
Re: I cannot save an unit on adding 3 new lines
« Reply #13 on: December 09, 2025, 06:37:50 pm »
Thank you for the explanations.
What is the difference between published, public and nothing as header?

The idea, that my exe becomes smaller, is one I like.
How much smaller?

The idea, that the IDE is blocked to use my code, is the second one, I like.
Which section serves this purpose and which does not?

Zvoni

  • Hero Member
  • *****
  • Posts: 3227
Re: I cannot save an unit on adding 3 new lines
« Reply #14 on: December 10, 2025, 09:06:05 am »
Thank you for the explanations.
What is the difference between published, public and nothing as header?

The idea, that my exe becomes smaller, is one I like.
How much smaller?

The idea, that the IDE is blocked to use my code, is the second one, I like.
Which section serves this purpose and which does not?
https://www.freepascal.org/docs-html/ref/refse34.html

Published - Same as Public, but generates RTTI. Needed for e.g. To set Values in the Object-Inspector of the IDE
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

 

TinyPortal © 2005-2018