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

0 Members and 1 Guest are viewing this topic.

Offline luke

  • Administrator
  • Seasoned Forum Regular
  • Posts: 324
    • View Profile
Re: Repeated calls to _LOADFONT with "monospace" can crash
« Reply #30 on: November 16, 2018, 04:04:46 am »
Minimal test case:
Code: QB64: [Select]
  1. _FONT _LOADFONT("c:\windows\fonts\cour.ttf", 16, "monospace")
  2. FOR i = 1 TO 100
  3.     F = _LOADFONT("c:\windows\fonts\cour.ttf", i, "monospace")
The "monospace" on the second _LOADFONT is required so the issue is likely with the character width calculation routine.

Offline luke

  • Administrator
  • Seasoned Forum Regular
  • Posts: 324
    • View Profile
Re: Repeated calls to _LOADFONT with "monospace" can crash
« Reply #31 on: November 16, 2018, 04:38:33 am »
Actually it can be reduced to just two lines, as long as you're willing to run it sufficiently many times:
Code: QB64: [Select]
  1. _FONT _LOADFONT("/media/winmain/Windows/Fonts/cour.ttf", 16, "monospace")
  2. F = _LOADFONT("/media/winmain/Windows/Fonts/cour.ttf", 12, "monospace")

EDIT: The location it ostensibly crashes at in FT changes each time. Fascinating.

EDIT 2: It even crashes if compiled with $console:only. At least it's not the glut thread messing things up.
« Last Edit: November 16, 2018, 07:50:07 am by luke »

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Repeated calls to _LOADFONT with "monospace" can crash
« Reply #32 on: November 16, 2018, 07:35:29 am »
And, oddly enough, I can get it to run 99.9% of the time with something as simple as:

FONT _LOADFONT("/media/winmain/Windows/Fonts/cour.ttf", 16, "monospace")
_DELAY 0.01
F = _LOADFONT("/media/winmain/Windows/Fonts/cour.ttf", 12, "monospace")

That 0.01 pause is enough on my PC to correct whatever the glitch is.  It's truly rare for it to fail after that (once every half hour of continuous looping for Pete; I have yet to experience another crash with _DELAY in there).

The issue with that sort of fix is others may not have the same speed/performance on their machines, so that delay may not be sufficient for them.  Others may not need that much delay, which means it could unnecessary lag up their programs.

I'm still digging...
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: Repeated calls to _LOADFONT with "monospace" can crash
« Reply #33 on: November 16, 2018, 01:42:41 pm »
Hi
please with what QB64IDE version is the issue coming out?
I've got a very different experience in my TOSHIBA i3 4GB RAM Windows 10 e QB64IDE version 1.2 revision [dev build] from git e490b1a !

Programming isn't difficult, only it's  consuming time and coffee

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Repeated calls to _LOADFONT with "monospace" can crash
« Reply #34 on: November 16, 2018, 02:44:58 pm »
The latest dev build, as well as the stable build have the issue for me.  I haven't tried any older ones.
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: Repeated calls to _LOADFONT with "monospace" can crash
« Reply #35 on: November 16, 2018, 02:50:23 pm »
I have no issue after REMMED  _LIMIT 25

here the code I use
Code: QB64: [Select]
  1. DIM NAMEF$, q AS LONG, F AS LONG
  2.  
  3. NAMEF$ = "C:\Windows\Fonts\arial.ttf" 'micross.ttf"
  4. 'JAVATEXT.TTF" 'GABRIOLA.TTF" 'LUCON.TTF" 'impact.ttf"
  5. q = _LOADFONT(NAMEF$, 16, "monospace")
  6.     _FONT q
  7.     PRINT q
  8. ScaleScreen 0 'initialize the font settings
  9.  
  10. PRINT "Hello World"
  11.  
  12. ScaleScreen 50
  13. ScaleScreen 200
  14. ScaleScreen 150
  15.  
  16.  
  17.  
  18.  
  19.  
  20. SUB ScaleScreen (percent AS INTEGER)
  21.     STATIC FontSize(4 TO 100), RanOnce
  22.     SHARED F AS LONG, NAMEF$
  23.     IF NOT RanOnce THEN
  24.         RanOnce = -1
  25.         FOR i = 4 TO 100
  26.             '            _LIMIT 25
  27.             F = _LOADFONT(NAMEF$, i, "monospace")
  28.             IF F > 0 THEN
  29.                 FontSize(i) = _FONTWIDTH(F)
  30.                 _FREEFONT F
  31.             ELSE
  32.                 PRINT F 'make and keep an array of font sizes so we don't have to keep loading them over and over
  33.             END IF
  34.         NEXT
  35.     END IF
  36.     IF percent = 0 THEN EXIT SUB
  37.     h = _FONTWIDTH
  38.     desiredscale = (percent / 100) * h
  39.     FOR i = 5 TO 100
  40.         IF FontSize(i) > desiredscale THEN EXIT FOR
  41.     NEXT
  42.     Ftemp = _FONT
  43.     F = _LOADFONT(NAMEF$, i - 1, "monospace")
  44.     _FONT F
  45.     IF Ftemp <> 16 THEN _FREEFONT Ftemp
  46.  
it is your code with some control to get no error rising up from a loading error like a file not found!
Programming isn't difficult, only it's  consuming time and coffee

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: Repeated calls to _LOADFONT with "monospace" can crash
« Reply #36 on: November 16, 2018, 03:10:30 pm »
about Luke minimal test I got these result
1. no error
2 error if I add these lines of code

Quote
print i; " ";
_freefont F

here whole code
Code: QB64: [Select]
  1. _FONT _LOADFONT("c:\windows\fonts\cour.ttf", 16, "monospace")
  2. FOR i = 1 TO 100
  3.     F = _LOADFONT("c:\windows\fonts\cour.ttf", i, "monospace")
  4.     PRINT i; " ";
  5.     _FREEFONT F
  6.  

moreover
using  _DELAY 0.01 or _DELAY 0.1 before the FOR has no results
while using this _DELAY after _FREEFONT F in the FOR loop
it lets to work the program with no error....
It may be a latency about the malloc work behind _LOADFONT  and _FREEFONT...
what do you think about it?
Programming isn't difficult, only it's  consuming time and coffee

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Repeated calls to _LOADFONT with "monospace" can crash
« Reply #37 on: November 16, 2018, 04:10:04 pm »
@Tempodi -- That's the issue with these type of race conditions; they're intermittent and hard to trace/duplicate.  Your PC may be fast enough (or slow enough) that it might never experience the issue. 

Seems like what we're dealing with is a race condition where the _FONT routine is doing something which ends up erroring out when it finally finishes in the _LOADFONT routine.  The pause after _FONT allows it to finish what it's doing before _LOADFONT starts.

The only issue with a set time for a pause is how long to make that pause.  Different PCs may resolve the race condition at different times, so a set pause isn't the ideal solution.  What we need is to find the root of the issue and then trap it at the source.

So, for now, we're still digging and looking for the final source of the issue.  ;)
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: Repeated calls to _LOADFONT with "monospace" can crash
« Reply #38 on: November 16, 2018, 07:22:04 pm »
Hi
please with what QB64IDE version is the issue coming out?
I've got a very different experience in my TOSHIBA i3 4GB RAM Windows 10 e QB64IDE version 1.2 revision [dev build] from git e490b1a !

I get the error at random times in QB64 v 1.1 on Win 10. I made an auto-run version of Steve's program and noticed that you can increase the delay times but eventually you get a crash. It could take hours, but it will happen. The solution is to keep the font statement in a loop until it completes. That would mean making something whatever library the font statements are in to handle that. That's above my pay grade, as I don't dabble in C/C++ programming.
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
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Repeated calls to _LOADFONT with "monospace" can crash
« Reply #39 on: November 16, 2018, 07:51:28 pm »
I'm not so certain we need a loop at the _FONT statement, so much as we may need to set a flag there (FontInUse) and then pause LOADFONT to stop it from running until the flag is cleared.  Due to multi threads, pausing the end of _FONT may not be as useful as pausing the start of _LOADFONT, until it's finished.

Let's see what Luke and Fell come up with, over the next few days.  I can't seem to pinpoint the issue, so maybe they'll have a little better luck with it.
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline moises1953

  • Newbie
  • Posts: 55
    • View Profile
Re: Repeated calls to _LOADFONT with "monospace" can crash
« Reply #40 on: May 15, 2020, 05:02:38 am »
_LOADFONT needs the use of style$ variable, not a constant.
This example works OK:
Code: QB64: [Select]
  1. 'MultipleFontLoad
  2. 'SCREEN 0 may use only one FONT. When changed, changes full screen
  3. 'Otherwise graphic modes preserve the screen contents when change font
  4. '_LOADFONT needs the use of style$ variable instead of constant
  5. CONST MINSIZE = 8, MAXSIZE = 100
  6. DIM i AS INTEGER, hFont(MAXSIZE) AS LONG
  7. f$ = "cour.ttf"
  8. style$ = "MONOSPACE"
  9.  
  10. PRINT "LOADING:"; f$;
  11. FOR i = MINSIZE TO MAXSIZE
  12.   PRINT i;
  13.   hFont(i) = _LOADFONT(f$, i, style$)
  14.   IF hFont(i) <= 0 THEN EXIT FOR
  15. IF i > MAXSIZE THEN
  16.   DO
  17.     PRINT "Font sizes:"; MINSIZE; "-"; MAXSIZE;
  18.     INPUT ". Select size"; fontsize%
  19.     IF fontsize% < MINSIZE OR fontsize% > MAXSIZE THEN EXIT DO
  20.     _FONT hFont(fontsize%)
  21.   LOOP
  22.   PRINT "Fail"; i; hFont(i)
  23.  
  24. FOR i = MINSIZE TO MAXSIZE
  25.   _FREEFONT hFont(i)
  26.  
  27.