Recent

Author Topic: Problems with a recursive procedure, something is not run sequentially.  (Read 1348 times)

TonP

  • Newbie
  • Posts: 5
Hello,

first time user of Lazarus/Free Pascal and I managed to convert a working Delphi app to Lazarus. It compiles ok but when running, something strange is happening.
The app runs an algorithm to solve a puzzle and is using a recursive procedure. Somehow the recursion is not accessing variables correctly. Only when I add a line with "showmessage" at a strategic point in the recursive procedure does the program run correctly. Can it be that there is something not executed sequentially and that intermediate access to the screen corrects this? It is a bit like what is mentioned in the following forum entry: https://forum.lazarus.freepascal.org/index.php?topic=47188.0
Can anyone give me a hint?
Thanks.

jamie

  • Hero Member
  • *****
  • Posts: 6133
When ever you are using windows type controls that involves message handling to make it update its view you need to call Application.Processmessages just after you set the content ..

 From there the control will update itself and should display the change and then you move on.
The only true wisdom is knowing you know nothing

TonP

  • Newbie
  • Posts: 5
Do you mean displaying info on screen? The problem is that I normally don't do any screen interaction in the procedure, to speed up things. Or am I missing your point here?

MarkMLl

  • Hero Member
  • *****
  • Posts: 6686
Do you mean displaying info on screen? The problem is that I normally don't do any screen interaction in the procedure, to speed up things. Or am I missing your point here?

What he means- and note that that's all I'm saying- is that if you don't have an APM occasionally in a tight loop your GUI won't get any CPU time and all sort of bad things can happen.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

TonP

  • Newbie
  • Posts: 5
That would be a pity, because it indeed works with Application.Processmessages but makes the program more than two times slower than the Delphi version...

MarkMLl

  • Hero Member
  • *****
  • Posts: 6686
You need to look very carefully at what those variables really are. Something is not quite as you expect.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

TonP

  • Newbie
  • Posts: 5
Well, I can't find anything suspicious in the program. Thanks anyway for the info!

MarkMLl

  • Hero Member
  • *****
  • Posts: 6686
I know you can't, that's what I said :-)

If the program doesn't work as expected without some APMs, then something that you're assuming us a simple variable isn't.

"When a programmer has a hard time finding a bug, it's because xe's looking in the wrong place."

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

jamie

  • Hero Member
  • *****
  • Posts: 6133
beware that running this code within a Debug session is slow...

So run it outside of debug instead.

also you don't need to keep calling the Application.ProcessMessages in your loop unless you are making some visual changes you want to show during the loop.

 
The only true wisdom is knowing you know nothing

MarkMLl

  • Hero Member
  • *****
  • Posts: 6686
beware that running this code within a Debug session is slow...

He didn't say he was.

Quote
also you don't need to keep calling the Application.ProcessMessages in your loop unless you are making some visual changes you want to show during the loop.

Yes, I was going to suggest that he did something like having nested loops so that he's only doing an APM every 100 mSec or so. /However/ since nobody, least of all OP, knows /why/ having an APM helps, only having the right values surface every 1000th iteration would be worse than useless.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

TonP

  • Newbie
  • Posts: 5
Did some more testing:
To speed things up I already replaced listboxes by stringlists before trying things in Lazarus, so no screen access was needed for the algorithm itself.
In a total duration of 105 seconds, I write a result string to a listbox twice. That is all the screen access that I do during the run.
This is with calling Application.ProcessMessages 19 times.
And I did run without debugging.
It works, but like I said, slower than in Delphi.
Anyway, this algorithm was a once off study for me, the full blown run took the Delphi version more than a week, so I am not going to repeat that with Lazarus;-)
At least I know now about a workaround for this specific issue and that I can migrate Delphi apps to Lazarus.
In case Delphi Community Edition licenses keep on causing problems, there will be an alternative.
Thanks again.

 

TinyPortal © 2005-2018