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

Offline luke

  • QB64 Developer
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

  • QB64 Developer
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
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...

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
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.

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

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
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.  ;)

Offline Pete

  • Cuz I sez so, varmint!
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.

Offline SMcNeill

  • QB64 Developer
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.