Author Topic: Repeated calls to _LOADFONT with "monospace" can crash  (Read 29008 times)

0 Members and 1 Guest are viewing this topic.

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • Steve’s QB64 Archive Forum
Repeated calls to _LOADFONT with "monospace" can crash
« on: November 15, 2018, 10:00:26 am »
Code: QB64: [Select]
  1. _FONT _LOADFONT("cour.ttf", 16, "monospace")
  2. ScaleScreen 0 'initialize the font settings
  3.  
  4. PRINT "Hello World"
  5.  
  6. ScaleScreen 50
  7. ScaleScreen 200
  8. ScaleScreen 150
  9.  
  10.  
  11.  
  12.  
  13.  
  14. SUB ScaleScreen (percent AS INTEGER)
  15.     STATIC FontSize(4 TO 100), RanOnce
  16.     IF NOT RanOnce THEN
  17.         RanOnce = -1
  18.         FOR i = 4 TO 100
  19.             _LIMIT 25
  20.             F = _LOADFONT("cour.ttf", i, "monospace")
  21.             FontSize(i) = _FONTWIDTH(F) 'make and keep an array of font sizes so we don't have to keep loading them over and over
  22.             _FREEFONT F
  23.         NEXT
  24.     END IF
  25.     IF percent = 0 THEN EXIT SUB
  26.     h = _FONTWIDTH
  27.     desiredscale = (percent / 100) * h
  28.     FOR i = 5 TO 100
  29.         IF FontSize(i) > desiredscale THEN EXIT FOR
  30.     NEXT
  31.     Ftemp = _FONT
  32.     F = _LOADFONT("cour.ttf", i - 1, "monospace")
  33.     _FONT F
  34.     IF Ftemp <> 16 THEN _FREEFONT Ftemp
  35.  
  36.  

Try the code above, and it works fine.

Remove the _LIMIT inside the SUB and then run it.  It'll die instantly.

For whatever reason, there has to be a timed gap somewhere between _LOADFONT and _FREEFONT when running them in a loop like this, or else the program simply explodes.
« Last Edit: November 15, 2018, 04:37:32 pm by SMcNeill »
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline johnno56

  • Forum Resident
  • Posts: 1270
  • Live long and prosper.
Re: Repeated calls to _LOADFONT with "monospace" can crash
« Reply #1 on: November 15, 2018, 02:48:40 pm »
I ran the program on my Linux box 'as is' and ran just fine. Commented out the 'limit' command and it appeared not to run. Then executed via a 'terminal'. The IDE did not change in any way but the 'terminal' displayed 'Segmentation fault'. I hope this is helpful.

J
Logic is the beginning of wisdom.

Offline johnno56

  • Forum Resident
  • Posts: 1270
  • Live long and prosper.
Re: Repeated calls to _LOADFONT with "monospace" can crash
« Reply #2 on: November 15, 2018, 02:58:12 pm »
I was curios as to why the 'limit' was put there... Experimented a little and figured it was to 'slow' the process... Left the 'limit' commented out and inserted a '_DELAY 0.1' before the '_FREEFONT' and it worked just fine.

My theory is: The font is being loaded... QB64 does its thing... _FREEFONT is encountered, possibly before the 'load' has finished, and promptly dies because there is no font to 'free'.... Just a guess... Thoughts?
Logic is the beginning of wisdom.

FellippeHeitor

  • Guest
Re: Repeated calls to _LOADFONT with "monospace" can crash
« Reply #3 on: November 15, 2018, 03:14:33 pm »
No crash in Windows XP even with _LIMIT commented out. I assume you're on to a fix to it, Steve?
« Last Edit: November 15, 2018, 03:16:02 pm by FellippeHeitor »

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • Steve’s QB64 Archive Forum
Re: Repeated calls to _LOADFONT with "monospace" can crash
« Reply #4 on: November 15, 2018, 03:32:35 pm »
No crash in Windows XP even with _LIMIT commented out. I assume you're on to a fix to it, Steve?

No idea WTH the problem is.  I can't see any reason why we'd set fault from this.  If anybody has any thoughts, I'd love to hear them.
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • Steve’s QB64 Archive Forum
Re: Repeated calls to _LOADFONT with "monospace" can crash
« Reply #5 on: November 15, 2018, 03:36:15 pm »
I was curios as to why the 'limit' was put there... Experimented a little and figured it was to 'slow' the process... Left the 'limit' commented out and inserted a '_DELAY 0.1' before the '_FREEFONT' and it worked just fine.

My theory is: The font is being loaded... QB64 does its thing... _FREEFONT is encountered, possibly before the 'load' has finished, and promptly dies because there is no font to 'free'.... Just a guess... Thoughts?

My only thought there is, "Why would _FONTWIDTH work then for us, on the previous line?"  If there's no font to free, how can we get the width of that font?
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
Re: Repeated calls to _LOADFONT with "monospace" can crash
« Reply #6 on: November 15, 2018, 03:41:10 pm »
ScaleScreen? My older version does not support ScaleScreen. What is it?

Pete
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • Steve’s QB64 Archive Forum
Re: Repeated calls to _LOADFONT with "monospace" can crash
« Reply #7 on: November 15, 2018, 04:28:34 pm »
ScaleScreen? My older version does not support ScaleScreen. What is it?

Pete

It's a sub inside the example which will scale a text screen by X amount.  Scroll the code inside the code box next time.  :D
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • Steve’s QB64 Archive Forum
Re: Repeated calls to _LOADFONT with "monospace" can crash
« Reply #8 on: November 15, 2018, 04:34:02 pm »
Narrowed down the issue to being _LOADFONT related, but have no idea WHY it's screwing up. 

Separate the routine to 3 different loops and you can tell; _FONTHEIGHT and _FREEFONT work just fine in a loop.  The _LOADFONT, however, will seg fault and die in a loop, unless a delay is installed so it can do its thing without glitching out.

**********

On a completely unrelated note, I did find a memory leak in _LOADFONT and can easily fix it -- but it has absolutely no bearing on the current issue. 
« Last Edit: November 15, 2018, 04:41:11 pm by SMcNeill »
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
Re: Repeated calls to _LOADFONT with "monospace" can crash
« Reply #9 on: November 15, 2018, 04:42:24 pm »
Ah, it looked like the code ended at the SLEEP statement. I was wondering why ScreenScale wasn't _ScreenScale if it was a new keyword. Anyway, I gave it a run and it worked fine until I removed the _LIMIT 25, and then it crashed without message. I'm running QB64 v 1.1 from September 2016 on a Windows 10. I actually have a more current version on my Win 7, but it is away at a resort for a well deserved rest.

Pete
« Last Edit: November 15, 2018, 04:44:59 pm by Pete »
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • Steve’s QB64 Archive Forum
Re: Repeated calls to _LOADFONT with "monospace" can crash
« Reply #10 on: November 15, 2018, 04:44:38 pm »
Ah, it looked like the code ended at the SLEEP statement. I was wondering why ScreenScale wasn't _ScreenScale if it was a new keyword. Anyway, I gave it a run and it worked fine. I'm running QB64 v 1.1 from September 2016 on a Windows 10. I actually have a more current version on my Win 7, but it is away at a resort for a well deserved rest.

Pete

Did you comment out the _LIMIT and then try it?  Without that delay, that's when it dies.
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
Re: Repeated calls to _LOADFONT with "monospace" can crash
« Reply #11 on: November 15, 2018, 04:46:20 pm »
I just edited my post. It does crash when _LIMIT 25 is remarked out.

PS: Sorry for the short attention span. I'm watching a bigger crash of NVIDIA stock. 14% down in after hours upon releasing earnings and over 25% down from its high. Lucky for me, I don't own shares. Fascinating times.

Pete 
« Last Edit: November 15, 2018, 04:54:05 pm by Pete »
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • Steve’s QB64 Archive Forum
Re: Repeated calls to _LOADFONT with "monospace" can crash
« Reply #12 on: November 15, 2018, 04:52:35 pm »
And, here's another odd bit of behavior which has me utterly baffled:

I can purge libq using the batch files inside the internal/c folder, compile, and the program runs perfectly fine --- ONCE.  I can click the exe to try and run it a second time, and it dies.  I can recompile it, and it dies.  Nothing works...

But, if I purge libqb and then recompile, it runs perfectly fine ONCE...

I'm utterly baffled to explain that behavior.
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
Re: Repeated calls to _LOADFONT with "monospace" can crash
« Reply #13 on: November 15, 2018, 04:57:49 pm »
Interesting that if I change to _LIMIT 1, it doesn't crash, but the window just stays open and nothing prints to it. Key presses have no effect. If I change to _LIMIT 10, it completely crashes. _LIMIT 25 works.

Pete
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • Steve’s QB64 Archive Forum
Re: Repeated calls to _LOADFONT with "monospace" can crash
« Reply #14 on: November 15, 2018, 05:07:34 pm »
Interesting that if I change to _LIMIT 1, it doesn't crash, but the window just stays open and nothing prints to it. Key presses have no effect. If I change to _LIMIT 10, it completely crashes. _LIMIT 25 works.

Pete

Limit slows the process.  Limit 1 traps you in the loop for 96 seconds.  (1 time per second, from I = 4 to 100)...  You just got to leave it open that long.

Limit 10 should work perfectly fine...  That's a longer delay than limit 25.   (10 times a second would take 9.6 seconds, vs 25 times a second for 4 seconds run time.)

As I say, the behavior has me lost ATM.
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!