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

Author Topic: Font .loadFromFile throwing a access violation. [SOLVED]  (Read 22443 times)

0 Members and 1 Guest are viewing this topic.

Flash619

  • Full Member
  • ***
  • Posts: 142
    • View Profile
Font .loadFromFile throwing a access violation. [SOLVED]
« on: November 18, 2012, 05:16:31 pm »
I have a bit of an issue where when I try to load a font, in this case, "arial.ttf" in my application, it throws a nice exception:
Quote
Unhandled exception at 0x519E1FF9 (sfml-graphics-2.dll) in Genesis.exe: 0xC0000005: Access violation writing location 0x00000000.

The code that causes it:
        DevHudFont.loadFromFile("arial.ttf");
        CurserPosText.setFont(DevHudFont);
 
You may notice it's not in a if statement, but it throws an exception either way.

The only time it does not throw an exception, is if it cannot find the font. ^^;; Then it just says it couldn't find the font/build the font face.

I should also point out that the font, is, in the same directory as the .exe when it runs. ;)

Any ideas?
« Last Edit: November 20, 2012, 05:28:22 pm by Flash619 »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10801
    • View Profile
    • development blog
    • Email
Re: Font .loadFromFile throwing a access violation.
« Reply #1 on: November 18, 2012, 05:44:02 pm »
Any ideas?

Quote from: ForumRules
It is important that:
  • the code is complete, so people can test it
  • the code is as simple as possible, the best is one main() with all the code inside -- people don't want to setup a complex project just to test your code
  • the code reproduces the initial problem
Complete rules can be found here. ;)
« Last Edit: November 18, 2012, 05:46:11 pm by eXpl0it3r »
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

FRex

  • Hero Member
  • *****
  • Posts: 1845
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: Font .loadFromFile throwing a access violation.
« Reply #2 on: November 18, 2012, 05:54:17 pm »
Quote
(sfml-graphics-2.dll)
You're mixing debug and release or developing in release, either is bad.
Back to C++ gamedev with SFML in May 2023

Flash619

  • Full Member
  • ***
  • Posts: 142
    • View Profile
Re: Font .loadFromFile throwing a access violation.
« Reply #3 on: November 18, 2012, 09:11:44 pm »
You're mixing debug and release or developing in release, either is bad.

No, I build it in release mode. sfe Movie is release mode only atm so I'm forced to. But you are correct, I probably should had mentioned that I was in release mode.

As for showing code. There is not a whole lot to show.

font.SetupText(font.DevHud,DevHudFont,CurserPosText);//accessing a function to setup font that is commonly used.
Which accesses:
/**
Sets the font and text provided based on the enum provided.
*/

void Font::SetupText(TextStyle s,sf::Font& f,sf::Text& t)
{
        Log consoleLog;
        if(s == Font::DevHud)
        {
                if(!f.loadFromFile("Resource/Font/arial.ttf"))
                {
                        consoleLog.critical("Unable to load font file!");
                }

                t.setColor(sf::Color::Green);
                t.setCharacterSize(40);
        }
}
 

I've narrowed it down to:

                if(!f.loadFromFile("Resource/Font/arial.ttf"))
                {
                        consoleLog.critical("Unable to load font file!");
                }
 

Easily enough because if I comment it out, it works.

My real question I suppose is if this could be a problem related to the font file itself?

Flash619

  • Full Member
  • ***
  • Posts: 142
    • View Profile
Re: Font .loadFromFile throwing a access violation.
« Reply #4 on: November 18, 2012, 10:04:45 pm »
So I just tested it with a different font and its not the font file. I have no clue what it is at this point.

So somehow by moving where the font was declared fixed it. but now I'm dealing with loop/event glitches. Hopefully I'll have a update that makes sense soon enough.
« Last Edit: November 18, 2012, 10:11:13 pm by Flash619 »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10801
    • View Profile
    • development blog
    • Email
Re: Font .loadFromFile throwing a access violation.
« Reply #5 on: November 18, 2012, 10:22:19 pm »
You still didn't get what a complete and minimal example is though. ;)
Complete means, there's not a single left which needs to be added to compile and run it.
Minimal means that it exist only out of the most basic set of commands/function calls/variables which are needed to reproduce the error. At best just a few lines within the main function.

If you'd provide such an example, we could most likely tell you with a simple look whar's wrong, if you wouldn't have found it on your own by then. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Flash619

  • Full Member
  • ***
  • Posts: 142
    • View Profile
Re: Font .loadFromFile throwing a access violation.
« Reply #6 on: November 18, 2012, 10:57:46 pm »
You still didn't get what a complete and minimal example is though. ;)
Complete means, there's not a single left which needs to be added to compile and run it.
Minimal means that it exist only out of the most basic set of commands/function calls/variables which are needed to reproduce the error. At best just a few lines within the main function.

If you'd provide such an example, we could most likely tell you with a simple look whar's wrong, if you wouldn't have found it on your own by then. ;)

Well the issue there is, that wouldn't show how I'm referencing anything, and would likely cause me to never be able to re produce the issue, thus finding no fix for it since it cannot be re produced.

I would also like to clarify my previous statement. The issue is not fixed. Its throwing graphic exceptions again, and is generally, completely unstable with anything involving font or text objects.

Also, it still seems the root of all torment is:
                if(!f.loadFromFile("Resource/Font/visitor1.ttf"))
                {
                        consoleLog.critical("Unable to load font file!");
                }
 

For some reason, after sfml loads in the font file, everything goes completely wack. I don't know if its how I'm loading a font file to a referenced font, or what it is. But its extremely unstable after that. To the point where the smallest change in the code can cause exception throwing. It actually has gotten to the point where I call it either a miracle/error when it doesn't throw one.

I have managed to 100% isolate the error to this file. It no longer even sets up the font with a reference.

.cpp
#include "stdafx.h"
#include "DevHudDisplay.h"
void DevHud::display(sf::Event& e,EngineWindow& glWindow)
{
                consoleLog.info("running_DH");
                setVars(glWindow);
                printToWindow(glWindow);
}
void DevHud::setVars(EngineWindow& glWindow)
{
        mouse_pos = sf::Mouse::getPosition(glWindow);

                std::string MousePosSX = "X: "+validate.EEVarToStr(mouse_pos.x);
                std::string MousePosSY = " Y: "+validate.EEVarToStr(mouse_pos.y);
            MousePos = "Mouse Pos: "+MousePosSX+" "+MousePosSY;

                CurserPosText.setString(MousePos);

                //font.SetupText(font.DevHud,DevHudFont,CurserPosText);
                if(!DevHudFont.loadFromFile("Resource/Font/visitor1.ttf"))
                {
                        consoleLog.critical("Unable to load font file!");
                }

                CurserPosText.setColor(sf::Color::Green);
                CurserPosText.setCharacterSize(40);

                CurserPosText.setFont(DevHudFont);

}
void DevHud::printToWindow(EngineWindow& glWindow)
{
        glWindow.draw(CurserPosText);
        glWindow.display();
}
 

.h
#ifndef DEVHUDDISPLAY_H
#define DEVHUDDISPLAY_H
#include "stdafx.h"
#include "WindowManagment.h"
#include "Genesis.h"
#include "ConsoleUtils.h"
class DevHud{
        Log consoleLog;
        Validate validate;
        Font font;
public:
        void display(sf::Event&,EngineWindow&);
private:
        bool Enabled;

        sf::Text CurserPosText;
        sf::Font DevHudFont;
        std::string MousePos;

        sf::Vector2i mouse_pos;

        void setVars(EngineWindow&);
        void printToWindow(EngineWindow&);
};
#endif
 

Hope that helps!
« Last Edit: November 18, 2012, 11:17:04 pm by Flash619 »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10801
    • View Profile
    • development blog
    • Email
Re: Font .loadFromFile throwing a access violation.
« Reply #7 on: November 18, 2012, 11:36:40 pm »
Well the issue there is, that wouldn't show how I'm referencing anything, and would likely cause me to never be able to re produce the issue, thus finding no fix for it since it cannot be re produced.
Well then you'd do it wrong. If you know that the problem doesn't exist for a minimal example, then you should write this, so we can indefinitely exclude SFML as the source of the problem. And since the minimal example doesn't show the problem you'd have to go the other way, the harder way. Strip down everything unrelated of your code until you end up with very few lines that reproduce the problem. Going this way will often solve the problem on its own, since the mistake gets clear to you at some point. ;)

void DevHud::printToWindow(EngineWindow& glWindow)
{
        glWindow.draw(CurserPosText);
        glWindow.display();
}
 
You should always clear the window before drawing anything (except if that happens outside of this class, but then you shouldn't call display within this class).

class DevHud{
        Font font;

        sf::Font DevHudFont;
};
 
Having a class with the name Font next to sf::Font doesn't seem that nice. What does your Font class do, because it probably should have a different name...

But overall I don't see why your application should go nuts at the font loading operation. With a complete example one might find out where you screw up earlier. ;)
« Last Edit: November 18, 2012, 11:38:14 pm by eXpl0it3r »
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Flash619

  • Full Member
  • ***
  • Posts: 142
    • View Profile
Re: Font .loadFromFile throwing a access violation.
« Reply #8 on: November 18, 2012, 11:48:34 pm »
Well then you'd do it wrong. If you know that the problem doesn't exist for a minimal example, then you should write this, so we can indefinitely exclude SFML as the source of the problem. And since the minimal example doesn't show the problem you'd have to go the other way, the harder way. Strip down everything unrelated of your code until you end up with very few lines that reproduce the problem. Going this way will often solve the problem on its own, since the mistake gets clear to you at some point. ;)


You should always clear the window before drawing anything (except if that happens outside of this class, but then you shouldn't call display within this class).

Having a class with the name Font next to sf::Font doesn't seem that nice. What does your Font class do, because it probably should have a different name...

But overall I don't see why your application should go nuts at the font loading operation. With a complete example one might find out where you screw up earlier. ;)

Well, the Font is what held the function within it for autmatically setting the properties of the text/font that was referenced to it. I could rename it to SetFont and see how that works out.

In other news. It seems I have narrowed it down further. I shoved everything into one function:

void DevHud::display(EngineWindow& glWindow)
{
                consoleLog.info("running_DH");
                mouse_pos = sf::Mouse::getPosition(glWindow);

                std::string MousePosSX = "X: "+validate.EEVarToStr(mouse_pos.x);
                std::string MousePosSY = " Y: "+validate.EEVarToStr(mouse_pos.y);
            MousePos = "Mouse Pos: "+MousePosSX+" "+MousePosSY;
               
                if(!DevHudFont.loadFromFile("Resource/Font/visitor1.ttf"))
                {

                }

                CurserPosText.setFont(DevHudFont);

                CurserPosText.setString(MousePos);

                glWindow.draw(CurserPosText);
            glWindow.display();
                //setVars(glWindow);
                //printToWindow(glWindow);
}
 

Now however I'm receiving a new exception.

Quote
Unhandled exception at 0x58412F78 (sfml-system-2.dll) in Genesis.exe: 0xC0000005: Access violation reading location 0x00000000.

Now it's the system that doesn't like my code. ^^;; Oh yay. *sarcasm*

Hmm, Maybe it would help to know how this function was being called? Here ya go:

void Genesis::TestOverlays(EngineWindow& glWindow)
{
        DevHud devHud;
        while(true)
        {
                sf::Event e;
        while(glWindow.pollEvent(e))
                {  
                        ConsoleLog.info("EVENT CALLED");
                        devHud.display(glWindow);
                        if((e.type == sf::Event::EventType::KeyPressed)&&(e.key.code == sf::Keyboard::Escape))
                        {
                                return;
                        }
                }
                glWindow.display();
               
        }
}
 

I have it display by itself so that it doesn't get overlapped by something else..... But I suppose I could just set it to print and then display the changes in the main class.


eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10801
    • View Profile
    • development blog
    • Email
Re: Font .loadFromFile throwing a access violation.
« Reply #9 on: November 19, 2012, 12:35:56 am »
Okay, you should never draw/display anything inside the event loop. It not only will lead to strange behavior (an event gets only triggered occasionally if you do something) but it's also very bad practice.
You should also never call window.display() twice in one iteration, because the second one will overwrite the first one.
And as already said, you should always clear (window.clear()) the render window before drawing anything on it.

As I already said, I can't help you further, because I'm not gonna guess what could maybe be to cause that comes from code XYZ I don't have access to. If you can confirm that this minimal and complete example does also crash, then it's something with SFML/your setup. If not, then it's something in your code.

#include <SFML/Graphics.hpp>
#include <iostream>

int main()
{
    sf::RenderWindow window(sf::VideoMode(1024, 765), "Mandelbrot");
    window.setFramerateLimit(60);

    sf::Font font;
    if(!font.loadFromFile("PATH-TO/arial.ttf"))
        std::cout << "OMG OMG OMG!" << std::endl;

    sf::Text text("Test Text 123", font, 20);
    text.setPosition(20.f, 20.f);

    while(window.isOpen())
    {
        sf::Event event;
        while(window.pollEvent(event))
        {
            if(event.type == sf::Event::Closed)
                window.close();
        }

        window.clear();
        window.draw(text);
        window.display();
    }
}
 
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Flash619

  • Full Member
  • ***
  • Posts: 142
    • View Profile
Re: Font .loadFromFile throwing a access violation.
« Reply #10 on: November 19, 2012, 01:07:55 am »
Okay, you should never draw/display anything inside the event loop. It not only will lead to strange behavior (an event gets only triggered occasionally if you do something) but it's also very bad practice.
You should also never call window.display() twice in one iteration, because the second one will overwrite the first one.
And as already said, you should always clear (window.clear()) the render window before drawing anything on it.

As I already said, I can't help you further, because I'm not gonna guess what could maybe be to cause that comes from code XYZ I don't have access to. If you can confirm that this minimal and complete example does also crash, then it's something with SFML/your setup. If not, then it's something in your code.

The code you provided, DOES work. 100%, no errors at all. ...not sure why mine wont. But at least we know its not SFML.

I was un aware of drawing/displaying in a event loop.... What would be a better way to go about changing/displaying something when the mouse moves then? o.O I just put it in the event loop because I figured it seemed like a easy way to do it. *that however does not mean its correct*

Access to the code? I've posted 90% of the code involved with this error apart from listing all the code from every class. XD But if you do indeed want more, please tell me what of, *or what you would need to see more of*, so that I can post it/send it and such. :)

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Font .loadFromFile throwing a access violation.
« Reply #11 on: November 19, 2012, 08:06:44 am »
Maybe you could compile in debug mode and get a much more detailed output from the debugger? If the code path that is used to reproduce the problem has calls to sfeMovie functions, then comment them out so that your app doesn't crash because of this.
Laurent Gomila - SFML developer

Flash619

  • Full Member
  • ***
  • Posts: 142
    • View Profile
Re: Font .loadFromFile throwing a access violation.
« Reply #12 on: November 19, 2012, 01:52:29 pm »
Maybe you could compile in debug mode and get a much more detailed output from the debugger? If the code path that is used to reproduce the problem has calls to sfeMovie functions, then comment them out so that your app doesn't crash because of this.

Today after class I plan on re writing devHud in a more usable manner.

Currently it works like.
  • Called when a event is heard to toggle on/off.
  • Draw and Display are within the Event Loop

I plan to make it that.

  • It has a function to return the sf::Text object so that it does not require a draw function within itself, within the event loop.
  • That it has a function to be called within the event loop for changing a bool variable to enable/disable its tracking capabilities
  • If it's never been enabled, or it has been disabled, and its return text function is called, it will return a blank sf::Text. Hmm Or I could add a function to return a bool and do something like.....
if(devHud.Enabled)
{
//Draw Stuff here.
}
 
    I'll decide later.

Once I get all of that done, and my code looking nice. I will switch to debug and post some useful info! :)

Flash619

  • Full Member
  • ***
  • Posts: 142
    • View Profile
Re: Font .loadFromFile throwing a access violation.
« Reply #13 on: November 19, 2012, 08:50:09 pm »
OK update. I re did the class for the most part so it works a bit different.

I have no compile errors, no debug errors, but also no text displays. ... Not sure why. It seems almost like the font is blank, but I receive no errors when loading the font. I get no errors at all really. But as I said, No text is printed to the screen. I'll post the updated code.

.cpp
#include "stdafx.h"
#include "DevHudDisplay.h"
DevHud::DevHud()
{
        Enabled = false;
        //Setting up defaults for font/text elements.
        fontUtils.SetupText(fontUtils.DevHud,DevHudFont,CurserPosText);
        CurserPosText.setFont(DevHudFont);
        CurserPosText.setPosition(0,0);
}
void DevHud::gatherData(EngineWindow& glWindow,sf::Event& e)
{

                consoleLog.info("running_DH");
                if(e.type == sf::Event::EventType::KeyPressed)//If we detect a tilde toggle the enable variable.
                {
                        if(e.key.code == sf::Keyboard::Tilde)
                        {
                                ToggleDevHud();
                        }
                }


                if(Enabled)//Only set the vars if its enabled.
                {
                        consoleLog.info("Enabled");
                        setVars(glWindow);
                }
}
void DevHud::setVars(EngineWindow& glWindow)
{
                //Mouse Position Vars
        mouse_pos = sf::Mouse::getPosition(glWindow);
                std::stringstream PositionLine;
                PositionLine << "Mouse Pos: " << "X: " << mouse_pos.x << " Y: " << mouse_pos.y;
                MousePos = PositionLine.str();
                CurserPosText.setString(MousePos);
}
void DevHud::ToggleDevHud()
{
        if(Enabled)
        {
                Enabled = false;
        }else if(!Enabled){
                Enabled = true;
        }
}
void DevHud::Draw(EngineWindow& glWindow)
{
        if(Enabled)//Only draw to the render window if enabled.
        {
                glWindow.draw(CurserPosText);
        }
}
 

.h
#ifndef DEVHUDDISPLAY_H
#define DEVHUDDISPLAY_H
#include "stdafx.h"
#include "WindowManagment.h"
#include "Genesis.h"
#include "ConsoleUtils.h"
class DevHud{
        Log consoleLog;
        Validate validate;
        FontUtils fontUtils;
public:
        DevHud();
        void gatherData(EngineWindow&,sf::Event&);
        void ToggleDevHud();
        void Draw(EngineWindow&);
private:
        bool Enabled;

        sf::Text CurserPosText;
        sf::Font DevHudFont;
        sf::String MousePos;
        sf::Vector2i mouse_pos;

        void setVars(EngineWindow&);
};
#endif
 

The function that sets up the font:
void FontUtils::SetupText(TextStyle s,sf::Font& f,sf::Text& t)
{
        Log consoleLog;
        if(s == FontUtils::DevHud)
        {
                consoleLog.info("Loading the Font");
                if(!f.loadFromFile("Resource/Font/visitor2.ttf"))
                {
                        consoleLog.critical("Unable to load font file!");
                }
                t.setColor(sf::Color::Green);
                t.setCharacterSize(40);
        }
}
 


Calling Point
        DevHud devHud;
        while(true)
        {
                sf::Event e;
        while(glWindow.pollEvent(e))
                {  
                        ConsoleLog.info("EVENT CALLED");
                        devHud.gatherData(glWindow,e);
                        if((e.type == sf::Event::EventType::KeyPressed)&&(e.key.code == sf::Keyboard::Escape))
                        {
                                return;
                        }
                }
                glWindow.clear();
                devHud.Draw(glWindow);
                glWindow.display();
               
        }
 

I moved around some console logs and checked. Every part of the code is running. Its just not displaying anything. o.O

I'll keep tinkering but if you see something wrong, do tell me! :)

cire

  • Full Member
  • ***
  • Posts: 138
    • View Profile
Re: Font .loadFromFile throwing a access violation.
« Reply #14 on: November 19, 2012, 09:00:32 pm »
  CurserPosText.setPosition(0,0);

Have you tried a different position?