Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Unable to load font multiple times — Gideros Forum

Unable to load font multiple times

______ Member
edited July 2012 in General questions
The following program throws this error after about 70 loops.
main.lua:5: foo.ttf: No such file or directory.
stack traceback:
local font
 
for i = 1, 200 do
	print(i)
	font = TTFont.new("foo.ttf", i)
end
My game requires loading font multiple times. Is this a bug or my error?
People call me "underscore".
Tagged:

Comments

  • It would seem that there is some-kind of error-possible the file is locked by being open at the same time too many times, it might be in the OS.

    Anyway way don't you just load all your fonts into globals at runtime and just access them?

    _G["font1"] = TTFont.new("font1.ttf, 1)
    _G["font2"] = TTFont.new("font2.ttf, 2)
    etc..

    Just an option.
    ThumbHurt Games / FB: ThumbHurt Games / FB: Eli/Teranth | Skype: teranth37
  • ______ Member
    Hmm, yeah. I was thinking of doing that. I'm probably using 5-6 different sizes of fonts.

    Just wanted to know why this was throwing an error.

    Also, if I were using 70 sizes, wouldn't that also throw an error?
    People call me "underscore".
  • While I am not an expert at the low level IO on iOS/Android, my guess is your loop is using up all the available file handles in the system, since it is looping fast and loading them all from the files.

    Another option might be to create BitmapFonts instead of using the TTF files and load them into texture memory and access them that way--you can of course also scale then instead of directly using different font sizes I would suppose.
    ThumbHurt Games / FB: ThumbHurt Games / FB: Eli/Teranth | Skype: teranth37
  • ______ Member
    Also, forgot to mention, my real game doesn't do this. It's loading fonts after several seconds, and still throws that error on around ~ 10 loads. This was just a reduced test case.
    People call me "underscore".
  • ______ Member
    Another option might be to create BitmapFonts instead of using the TTF files and load them into texture memory and access them that way--you can of course also scale then instead of directly using different font sizes I would suppose.
    Do BitmapFont's scale method scale fonts perfectly? No pixelation?

    People call me "underscore".
  • ______ Member
    Ok, now this is weird. I ran the program a few times. Here's the output.
    Uploading finished.
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    main.lua:5: foo.ttf: No such file or directory.
    stack traceback:
    Uploading finished.
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    main.lua:5: foo.ttf: No such file or directory.
    stack traceback:
    Uploading finished.
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    main.lua:5: foo.ttf: No such file or directory.
    stack traceback:
    Uploading finished.
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    main.lua:5: foo.ttf: No such file or directory.
    stack traceback:
    Uploading finished.
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    main.lua:5: foo.ttf: No such file or directory.
    stack traceback:
    Uploading finished.
    1
    2
    3
    4
    5
    6
    7
    8
    9
    main.lua:5: foo.ttf: No such file or directory.
    stack traceback:
    Uploading finished.
    1
    2
    3
    4
    5
    6
    7
    8
    main.lua:5: foo.ttf: No such file or directory.
    stack traceback:
    Uploading finished.
    1
    2
    3
    4
    5
    6
    7
    main.lua:5: foo.ttf: No such file or directory.
    stack traceback:
    Uploading finished.
    1
    2
    3
    4
    5
    6
    main.lua:5: foo.ttf: No such file or directory.
    stack traceback:
    Uploading finished.
    1
    2
    3
    4
    5
    main.lua:5: foo.ttf: No such file or directory.
    stack traceback:
    Uploading finished.
    1
    2
    3
    4
    main.lua:5: foo.ttf: No such file or directory.
    stack traceback:
    Uploading finished.
    1
    2
    3
    main.lua:5: foo.ttf: No such file or directory.
    stack traceback:
    Uploading finished.
    1
    2
    main.lua:5: foo.ttf: No such file or directory.
    stack traceback:
    foo.ttf is uploading.
    main.lua is uploading.
    Uploading finished.
    It's like the program is "running out of fonts" after every start. The program crashed every time, except the last. Gideros Player crashed on the last try.

    People call me "underscore".
  • ______ Member
    Aand it's gone.

    Started Player again, runs upto 200 fine. Tried about 10 times.

    But I did restart the player yesterday too, it was still giving the errors.
    People call me "underscore".
  • Weird really, just to stress test i run this code many times in my player and no errors. All prints are perfect. I will continue to test with it if i will receive any error i will inform here.
    local font,font2,font3,font4
     
    for i = 1, 2000 do
    	print(i)
    	font = TTFont.new("Roboto-Regular.ttf", i)
    	font2 = TTFont.new("Turkish_Times_New_Roman.ttf", i)
    	font3 = TTFont.new("Roboto-Regular.ttf", i)
    	font4 = TTFont.new("Turkish_Times_New_Roman.ttf", i)
    end
  • ______ Member
    Started again for me. Runs upto 202 now. Then 201. Then 200. Then 199, and so on.

    Something is definitely wrong.
    People call me "underscore".
  • @___ i agree after this post i increased my stress tests counter to 5000 and runs more than 10 times and no errors at all. Maybe something wrong with the ttf font it self or the folder that it is staying?
  • ______ Member
    Nope, not a font problem.

    Just downloaded PT Sans from http://www.fontsquirrel.com/fonts/PT-Sans , and used PTC55F.ttf

    First run - 181, second run - 180, third run - 179, ...

    And the folder is the root folder in this test, and /resources/fonts/ in my real game. So can't be that either.
    People call me "underscore".
  • did you try it in real device? for exaple make a button and try many times? Maybe it is player problem only? Because as i said i tried in my gideros studio many times i am not receiving any errors? Maybe you have some extra code, if you send me your project i can try it.
  • ______ Member
    edited July 2012
    Absolutely no other code.
    ➜  font-bug-test  ls
    PTC55F.ttf          font-bug-test.gproj main.lua
    ➜  font-bug-test  cat font-bug-test.gproj 
    <project>
     <properties autorotation="0" fps="60" scaleMode="0" iosDevice="0" xcodeVersion="0" packageName="com.yourdomain.yourapp" logicalHeight="480" logicalWidth="320" retinaDisplay="0" assetsOnly="0" orientation="0" architecture="0">
      <imageScales/>
     </properties>
     <file source="PTC55F.ttf"/>
     <file source="main.lua"/>
    </project>
    ➜  font-bug-test  cat main.lua 
    local font
     
    for i = 1, 200 do
    	print(i)
    	font = TTFont.new("PTC55F.ttf", i)
    end
    I can upload a zip if you still want, but the commands above show that those are the only files present.

    My Apple dev account is pending approval, so I cannot test on a real device yet :(

    Can you try increasing i's limit to maybe a million? See if it crashes anytime soon?
    People call me "underscore".
  • ok now i am icreasing it hope my computer will survive:D
  • talistalis Guru
    edited July 2012
    @___
    i make it 100000 and receive this error more than 2 times.(Can not check all it is so long:D)
    main.lua:5: Roboto-Regular.ttf: Error while reading font file.
    stack traceback:
    main.lua:5: Roboto-Regular.ttf: Error while reading font file.
    stack traceback:
    It is weird from this test i understand that this error is generated randomly.
  • ______ Member
    Hmm, that's a different error than mine.

    I got "No such file or directory."
    People call me "underscore".
  • atilimatilim Maintainer
    There is definitely a bug here. Most probably, TTFont keeps the file open and OS run out of file handles when TTFont's aren't collected fast enough. I'll look at it and post an update here.

    Thank you very much for detailed analysis :)
  • ______ Member
    @atilim

    I don't think that would explain the fact that my game used to crash with the font just being loaded 10-12 times, with the exact same message? That too, with several seconds gap in between.
    People call me "underscore".
  • @atilim thanks. Waiting to hear from you.
  • amnonamnon Member
    edited July 2018
    Had the same issue when loading a few TTFonts. Invoking collectgarbage("collect") every few TTFont.new seems to solve it. Not elegant, but works.
  • SinisterSoftSinisterSoft Maintainer
    That's Lua's garbage collection. I do this to keep Garbage collection a non-issue...
    collectgarbage("setstepmul",1000)
    collectgarbage()
    Then in my main game loop (an ENTER_FRAME) I have this:
    collectgarbage("step")
    --print(collectgarbage("count")*1024)
    Uncomment the line to make sure the garbage amount is fairly constant and doesn't 'run away'. You can regulate it by changing the 1000 setting for setstepmul.

    Likes: Apollo14

    Coder, video game industry veteran (since the '80s, ❤'s assembler), arrested - never convicted hacker (in the '90s), dad of five, he/him (if that even matters!).
    https://deluxepixel.com
    +1 -1 (+1 / -0 )Share on Facebook
Sign In or Register to comment.