Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: [SFML2.4 - VB NET 4.5.2] Overlap memory with heavy font files.  (Read 10302 times)

0 Members and 1 Guest are viewing this topic.

hechelion

  • Newbie
  • *
  • Posts: 24
    • View Profile
Hi.
I have an very odd error.

I have finished a game (Evorales), this game works without any problem.
Now I am trying to add the Japanese language, which meant changing the fonts files (the ones I was using originally did not support Japanese characters).

At changing the fonts, the game began to give memory errors or texts that did not display well (and I don't mean string encoding problems).



Exactly the same source code, just changing the font files.

- Change "steamwreck" (35 kB)  for "NotoSerifCJKjp-hinted" (23 MB ), rear/write protected area memory error from sfml-graphics.dll when used SFMLwindows.draw(TEXTobject)
There was no error loading the font or creating the TEXT objects

- Change "steamwreck" (35 kB)  for  "mplus" (1300 kB), no memory error, but text strings were displayed with errors, for example "spaol" instead of "Español", etc.


After several hours of testing, I found that when creating a very big array to store the game's keystrokes, it overwritten the same memory area used by the font, causing this problem.

In my particular case, creating these variables somehow corrupts the font texture
Code: [Select]
Private plstGamePad(199999) As UInt32

Change 199999 for other number more small (999) or even more greater (999999) solve all previus TEXT problems.


My assumption is that the SFML implementation in NET does not reserve memory properly when loading font files. Apparently this error is notorious only with relatively large files.

I tried to produce a minimal code that would replicate the problem, but it was impossible, due to the limitations of the language when reserving memory.


eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: [SFML2.4 - VB NET 4.5.2] Overlap memory with heavy font files.
« Reply #1 on: August 23, 2021, 08:04:24 am »
Your conclusion is most likely not true, as SFML will be executed in the C++ library, which has proven for many years to work fine.
You might have some memory corruption somewhere else?
Maybe the font is faulty?

Have you tried updating to SFML.Net 2.5?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

hechelion

  • Newbie
  • *
  • Posts: 24
    • View Profile
Re: [SFML2.4 - VB NET 4.5.2] Overlap memory with heavy font files.
« Reply #2 on: August 24, 2021, 09:12:23 am »
Thanks for answering, I know that SFML is very stable, but vb net is a language that does not have pointers, that does not allow you to select where in memory you can create a variable.
Even if I wanted to, it would be impossible for me to overlay a memory area.

I don't have any other similar errors, neither in textures, nor in sound, only with the text.

I have not been able to test with SFML 2.5 because it is only distributed as nutmeg and I have never used net framework with nutmeg. updating could be cause of other errors due to my inexperience with nutmeg.

I know the font is not corrupted by the following:
I have been doing new tests and have found that the problem only occurs when creating a font from a byte stream.

This code work fine:
Code: [Select]
oMeta.oGameData.AddFont("mplus", New SFML.Graphics.Font("mplus.ttf"))

This code result in corrupt TEXT (but only, after this "Private plstGamePad(199999) As UInt32" )
Code: [Select]
Dim fStreamData As New FileStream("mplus.ttf", FileMode.Open)
Dim bdata As New BinaryReader(fStreamData)
Dim bytes = bdata.ReadBytes(bdata.BaseStream.Length)
oMeta.oGameData.AddFont("mplus", New SFML.Graphics.Font(bytes))
bdata.Close()
fStreamData.Close()

What bothers me, is that I have not been able to reproduce the error with minimal code.

I know that with a more complex code it is difficult to detect where the error occurs, so it is logical to think that the error is caused by my code.
But if my code was the problem. Why does changing the font load the problem disappear?
« Last Edit: August 24, 2021, 09:15:37 am by hechelion »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: [SFML2.4 - VB NET 4.5.2] Overlap memory with heavy font files.
« Reply #3 on: August 24, 2021, 04:44:45 pm »
Oh, you can't close the stream for a font object.
Not sure why the initial API design was chosen as "LoadFrom*" because for the font, it will actually go back to the file/stream and read new glyphs on the fly, as such the stream has to be kept alive as long as the font is used.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

hechelion

  • Newbie
  • *
  • Posts: 24
    • View Profile
Re: [SFML2.4 - VB NET 4.5.2] Overlap memory with heavy font files.
« Reply #4 on: August 24, 2021, 06:50:28 pm »
Thank eXpl0it3r for your answer.

My bad, the documentation is clear:
Quote
Warning
    SFML cannot preload all the font data in this function, so the stream has to remain accessible until the sf::Font object loads a new font or is destroyed.

don't notice this before, because all the resources (textures, sounds) I load as stream bytes, since they are packed.
Since the other resources don't have this behavior, I just used the same code to load the fonts.

My apologies and thanks for your answers.