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

Offline luke

  • QB64 Developer
Re: Repeated calls to _LOADFONT with "monospace" can crash
« Reply #30 on: November 16, 2018, 07:04:46 AM »
Minimal test case:
Code: [Select]
_FONT _LOADFONT("c:\windows\fonts\cour.ttf", 16, "monospace")
FOR i = 1 TO 100
    F = _LOADFONT("c:\windows\fonts\cour.ttf", i, "monospace")
NEXT i
END
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, 07: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: [Select]
_FONT _LOADFONT("/media/winmain/Windows/Fonts/cour.ttf", 16, "monospace")
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, 10:50:07 AM by luke »

Offline SMcNeill

  • QB64 Developer
Re: Repeated calls to _LOADFONT with "monospace" can crash
« Reply #32 on: November 16, 2018, 10: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, 04: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 !


Offline SMcNeill

  • QB64 Developer
Re: Repeated calls to _LOADFONT with "monospace" can crash
« Reply #34 on: November 16, 2018, 05: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, 05:50:23 PM »
I have no issue after REMMED  _LIMIT 25

here the code I use
Code: [Select]
DIM NAMEF$, q AS LONG, F AS LONG

NAMEF$ = "C:\Windows\Fonts\arial.ttf" 'micross.ttf"
'JAVATEXT.TTF" 'GABRIOLA.TTF" 'LUCON.TTF" 'impact.ttf"
q = _LOADFONT(NAMEF$, 16, "monospace")
IF q THEN
    _FONT q
ELSE
    PRINT q
END IF
ScaleScreen 0 'initialize the font settings

PRINT "Hello World"
SLEEP

ScaleScreen 50
SLEEP
ScaleScreen 200
SLEEP
ScaleScreen 150
SLEEP





SUB ScaleScreen (percent AS INTEGER)
    STATIC FontSize(4 TO 100), RanOnce
    SHARED F AS LONG, NAMEF$
    IF NOT RanOnce THEN
        RanOnce = -1
        FOR i = 4 TO 100
            '            _LIMIT 25
            F = _LOADFONT(NAMEF$, i, "monospace")
            IF F > 0 THEN
                FontSize(i) = _FONTWIDTH(F)
                _FREEFONT F
            ELSE
                PRINT F 'make and keep an array of font sizes so we don't have to keep loading them over and over
            END IF
        NEXT
    END IF
    IF percent = 0 THEN EXIT SUB
    h = _FONTWIDTH
    desiredscale = (percent / 100) * h
    FOR i = 5 TO 100
        IF FontSize(i) > desiredscale THEN EXIT FOR
    NEXT
    Ftemp = _FONT
    F = _LOADFONT(NAMEF$, i - 1, "monospace")
    _FONT F
    IF Ftemp <> 16 THEN _FREEFONT Ftemp
END SUB
it is your code with some control to get no error rising up from a loading error like a file not found!

Re: Repeated calls to _LOADFONT with "monospace" can crash
« Reply #36 on: November 16, 2018, 06: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: [Select]
_FONT _LOADFONT("c:\windows\fonts\cour.ttf", 16, "monospace")
FOR i = 1 TO 100
    F = _LOADFONT("c:\windows\fonts\cour.ttf", i, "monospace")
    PRINT i; " ";
    _FREEFONT F
NEXT i
END

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?

Offline SMcNeill

  • QB64 Developer
Re: Repeated calls to _LOADFONT with "monospace" can crash
« Reply #37 on: November 16, 2018, 07: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, 10: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, 10: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.