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

Author Topic: Fonts, don't we all love fonts...  (Read 7045 times)

0 Members and 1 Guest are viewing this topic.

Maximilian

  • Newbie
  • *
  • Posts: 13
    • View Profile
Fonts, don't we all love fonts...
« on: October 16, 2013, 08:42:50 pm »
So, I've been working with sf::Text and mainly sf::Font a little bit lately.
Sf::Font has been randomaly throwing errors at me whenever I edit my code. To Give you an example I need to show you two iterations of the same things, except that one will cause a crash and the other wont.

First of:

This won't cause a crash:
IN MAIN:

        graph.addNode("Dublin", 0, 500);
        graph.addNode("Edinburough", 150, 450);
        graph.addNode("Cambridge", 250, 650);
        graph.addNode("London", 250, 730);
        graph.addNode("Oslo", 700, 340);
        graph.addNode("Reyjkavik", 530, 120);
        graph.addNode("Narvik", 740, 100);
        graph.addNode("Gothenburg", 760, 490);
        graph.addNode("Stockholm", 890, 440);
        graph.addEdge(5, 4, 2000);
        graph.addEdge(7, 3, 140);
        graph.addEdge(0, 1, 245);
        graph.addEdge(1, 0, 199);
        graph.addEdge(2, 1, 99);
        graph.addEdge(0, 2, 180);
        graph.addEdge(2, 0, 30);
        graph.addEdge(0, 4, 55);
        graph.addEdge(2, 3, 68);
        graph.addEdge(4, 0, 70);
        graph.addEdge(4, 3, 102);
        graph.addEdge(3, 2, 200);
 

Where I'm using the font in text;ยจ
                sf::Text tempTxt;
                tempTxt.setFont(font);
                tempTxt.setString(nodes.at(j)->name);
                tempTxt.setPosition(sf::Vector2f(nodes.at(j)->x, nodes.at(j)->y-20));
                tempTxt.setCharacterSize(30);
                tempTxt.setColor(sf::Color(255, 255, 0, 255));
                window.draw(tempTxt);
 

Yes, there are a bunch of other things in there. But they seem to be irrelevant to my issue. Also, the
sf::Font font; line is a private variable in my class and the font is loaded with the normal font.loadFromFile("string");

Now to the interesting part:
if the part of main that builds my graph looks like this:
std::ifstream graphFile;
        std::string nodeFile="C:\\nodes.txt", edgeFile="C:\\edges.txt";
        graphFile.open(nodeFile);
        if(!graphFile.is_open()){
                std::cout<<"failed to open file: "<<nodeFile<<"\n";
                system("pause");
                return 0;
        }
        while(!graphFile.eof()){
                double x, y;
                std::string name;
                graphFile>>name;
                graphFile>>x;
                graphFile>>y;
                graph.addNode(name, x, y);
        }
        graphFile.close();
        graphFile.open(edgeFile);
        if(!graphFile.is_open()){
                std::cout<<"failed to open file: "<<edgeFile<<"\n";
                system("pause");
                return 0;
        }
        while(!graphFile.eof()){
                int start, stop;
                double cost;
                graphFile>>start;
                graphFile>>stop;
                graphFile>>cost;
                graph.addEdge(start, stop, cost);
        }
        graphFile.close();
 

and the part that uses text stays the same, my code crashes. Now, for accuracy's sake I want to make it clear that yes, the result of the two different pieces of code are both the exact same, I'm using the same input just from a file instead.

Sure, there are other things related to this I'm sure. But iuf anyone has any clue about what I have done wrong so that I can hopew to find it, please tell me!!!

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Fonts, don't we all love fonts...
« Reply #1 on: October 16, 2013, 08:51:33 pm »
There are many things that you can do to solve this problem, other than throwing your code on a forum and waiting ;)

You can first use your debugger to know what crashes and where.
You can then try to reproduce the problem in a minimal code so that it becomes more obvious.

After that, if you still fail to solve your problem, we'll be able to help you for sure with the extra pieces of information that you would provide from these steps.
Laurent Gomila - SFML developer

Maximilian

  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: Fonts, don't we all love fonts...
« Reply #2 on: October 16, 2013, 08:56:58 pm »
Well, quite frankly I have not been idle the last few days! The debugger information is minimal at best. It is simply saying that the stack around various different variables is getting corrupted (to confirm, yes these are all around the sf::Text variable). Second, the problem is nigh on impossible to reproduce in minimal code, as this is an example of ONE of the issues I have been having ewith this. It seems completely arbitrairy, sometimes a small change in my code renders the same issue. I wish I could provide minimalized code. But quite frankly: I don't think the problem is in my code, I think it is in my setup of visual studio 2012. I'm throwing it out here because I have already done everything I can to try and find the actual issue.

binary1248

  • SFML Team
  • Hero Member
  • *****
  • Posts: 1405
  • I am awesome.
    • View Profile
    • The server that really shouldn't be running
Re: Fonts, don't we all love fonts...
« Reply #3 on: October 16, 2013, 09:17:46 pm »
If your debugger is telling you that memory is getting corrupted around a certain variable, it doesn't have to have anything to do with it. Since the sf::Text gets created on the stack, it could mean any other stack variable screwing with the memory as well. And just so that you know, the sf::Text itself allocates most of its contained data on the heap so the amount of space it truly takes up on the stack is minimal.

Normally the compiler should warn you if you do anything that could lead to stack corruption (as opposed to heap corruption). Did you heed all of its warnings? If this happens with the exact same code, but only sometimes when running the application, it could also be a hint at uninitialized memory. Did you check for that?

How big is this project in LOC? If it is less than 1000 lines, provide the code and I could take a look at it, because this whole scenario sounds fishy.
SFGUI # SFNUL # GLS # Wyrm <- Why do I waste my time on such a useless project? Because I am awesome (first meaning).

Maximilian

  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: Fonts, don't we all love fonts...
« Reply #4 on: October 16, 2013, 09:25:27 pm »
1. yes, It shuld be less than 1000 lines. 2. The error has now changed (funny enough xd)
The way I read the nodes into the graph don't matter anymore, now all that matters is if I load the font or not... If I dont load the font I dont get errors, If I do, then voila! (also, the error now seems to be a dequeue an empty queue error, which I'm not causing myself for sure!).

Anyway, submitting my code below:

Source.cpp
#include "Graph.h"
#include <SFML\Window.hpp>
#include <fstream>


int main(){
        Graph graph;
        std::ifstream graphFile;
        std::string nodeFile="C:\\nodes.txt", edgeFile="C:\\edges.txt";
        graphFile.open(nodeFile);
        if(!graphFile.is_open()){
                std::cout<<"failed to open file: "<<nodeFile<<"\n";
                system("pause");
                return 0;
        }
        while(!graphFile.eof()){
                double x, y;
                std::string name;
                graphFile>>name;
                graphFile>>x;
                graphFile>>y;
                graph.addNode(name, x, y);
        }
        graphFile.close();
        graphFile.open(edgeFile);
        if(!graphFile.is_open()){
                std::cout<<"failed to open file: "<<edgeFile<<"\n";
                system("pause");
                return 0;
        }
        while(!graphFile.eof()){
                int start, stop;
                double cost;
                graphFile>>start;
                graphFile>>stop;
                graphFile>>cost;
                graph.addEdge(start, stop, cost);
        }
        graphFile.close();
        std::string from="Dublin", to="London";
        if(graph.pathFrom(from, to))
                std::cout<<"There is a path from "<<from<<" to "<<to<<"!";
        else
                std::cout<<"There is no path from "<<from<<" to "<<to<<"!";
        int num=graph.sPathFrom(from, to);
        if(num>0){
                std::cout<<"\nThe shortest path from "<<from<<" to "<<to<<" is on "<<num<<" flight(s)!\n";
                std::stack<std::string> rout;
                rout=graph.cPath(from, to);
                std::cout<<"This is the cheapest rout:\n";
                int lim=rout.size();
                for(int n=0; n<lim; n++){
                        std::cout<<rout.top();
                        rout.pop();
                        if(rout.size()>0)
                                std::cout<<"->";
                }
        }
        sf::RenderWindow window(sf::VideoMode(1000, 800), "Plot!");
        window.setPosition(sf::Vector2i(500, 100));
        window.setFramerateLimit(45);
        sf::View view(sf::Vector2f(400, 400), sf::Vector2f(1000, 1000));
        sf::Vector2i mousePos;
        float dx=1, dy=1;
        window.setView(view);
        bool clicked=false;
        while(window.isOpen()){
                sf::Event event;
                while(window.pollEvent(event))
                {
                        if(event.type==sf::Event::MouseButtonPressed){
                                if(event.mouseButton.button==sf::Mouse::Left)
                                        clicked=true;
                        }
                        if(event.type==sf::Event::MouseButtonReleased){
                                if(event.mouseButton.button==sf::Mouse::Left)
                                        clicked=false;
                        }
                        if(event.type==sf::Event::MouseMoved&&clicked){
                                view.move(-dx*(event.mouseMove.x-mousePos.x), -dy*(event.mouseMove.y-mousePos.y));
                        }
                        if(event.type == sf::Event::MouseWheelMoved)
                                view.zoom(1.f+event.mouseWheel.delta*0.1f);
                        if(event.type==sf::Event::Closed)
                                window.close();
                }
                if(sf::Keyboard::isKeyPressed(sf::Keyboard::Q))
                        view.zoom(1.0309);
                if(sf::Keyboard::isKeyPressed(sf::Keyboard::E))
                        view.zoom(0.97);
                if(sf::Keyboard::isKeyPressed(sf::Keyboard::W))
                        view.move(0, -5);
                if(sf::Keyboard::isKeyPressed(sf::Keyboard::S))
                        view.move(0, 5);
                if(sf::Keyboard::isKeyPressed(sf::Keyboard::A))
                        view.move(-5, 0);
                if(sf::Keyboard::isKeyPressed(sf::Keyboard::D))
                        view.move(5, 0);
                mousePos=sf::Mouse::getPosition(window);
                window.setView(view);
                window.clear();
                graph.draw(window);
                window.display();
        }
        return 0;
}
 

Graph.h
#ifndef GRAPH_INCLUDE
#define GRAPH_INCLUDE
#include <vector>
#include <string>
#include <iostream>
#include <cstdlib>
#include <string>
#include <sstream>
#include <SFML\Window.hpp>
#include <SFML\Graphics.hpp>
#include <SFML\Graphics\Shape.hpp>
#include <cmath>
#include <queue>
#include <stack>

struct Node;

struct Edge{
        double size;
        Node *start, *end;
};

struct Node{
        std::string name;
        double x, y;
        std::vector<Edge*> edges;
        int num;
};

class Graph{
        public:
                Graph();
                ~Graph();
                int numNodes();
                void addNode(std::string name, double x, double y);
                void addEdge(int start, int end, double size);
                void draw(sf::RenderWindow& window);
                bool pathFrom(std::string from, std::string target);
                int     sPathFrom(std::string from, std::string target);
                std::stack<std::string> cPath(std::string from, std::string target);
        private:
                std::vector<Node*> nodes;
                sf::Font font;
                sf::Text tempTxt;
                bool dFirst(int num, std::string target, std::vector<int>& visited);
                int bFirst(std::queue<int>& q, std::string target, std::vector<int>& visited, int num, int steps, int addedThisLevel, int addedNextLevel, int level);
                bool contains(int num, std::vector<int> visited);
                int* heu(int end);
                void aStar(std::vector<int>& open, std::vector<int>& closed, int* heuristic, double* fValue, int* pointers, double* gScore, int target, int start);
};

Graph::Graph(){
        font.loadFromFile("C:\\Windows\\Fonts\\Calibri.ttf");
        ;
}

std::stack<std::string> Graph::cPath(std::string from, std::string target){
        bool test1=true, test2=true;
        int num1, num2;
        std::stack<std::string> ret;
        for(int n=0; n<nodes.size(); n++){
                if(nodes.at(n)->name==from){
                        test1=false;
                        num1=n;
                }
                if(nodes.at(n)->name==target){
                        test2=false;
                        num2=n;
                }
        }
        int* heuristic;
        int* pointers = new int [nodes.size()];
        double* gScore = new double[nodes.size()];
        double* fValue = new double [nodes.size()];
        std::vector<int> open;
        std::vector<int> closed;
        for(int n=0; n<nodes.size(); n++){
                pointers[n]=-1;
                fValue[n]=100000000;
                gScore[n]=0;
        }
        heuristic=heu(num2);
        open.push_back(num1);
        aStar(open, closed, heuristic, fValue, pointers, gScore, num2, num1);
        int n=num2;
        while(n!=num1){
                ret.push(nodes.at(n)->name);
                n=pointers[n];
        }
        ret.push(nodes.at(n)->name);
        delete [] gScore;
        delete [] heuristic;
        delete [] pointers;
        delete [] fValue;
        return ret;
}


void Graph::aStar(std::vector<int>& open, std::vector<int>& closed, int* heuristic, double* fValue, int* pointers, double* gScore, int target, int start){
        fValue[start]=heuristic[start];
        while(!open.empty()){
                double checkF=fValue[open.at(0)];
                int current, nCur;
                for(int n=0; n<open.size(); n++){
                        if(fValue[open.at(n)]<=checkF){
                                checkF=fValue[open.at(n)];
                                current=open.at(n);
                                nCur=n;
                        }
                }
                if(current==target)
                        return;
                open.erase(open.begin()+nCur);
                closed.push_back(current);
                for(int n=0; n<nodes.at(current)->edges.size(); n++){
                        double tentGScore=gScore[current]+nodes.at(current)->edges.at(n)->size;
                        double tentFScore=tentGScore+heuristic[nodes.at(current)->edges.at(n)->end->num];
                        if(contains(nodes.at(current)->edges.at(n)->end->num, closed)&&tentFScore>=fValue[nodes.at(current)->edges.at(n)->end->num])
                                continue;
                        else if(!contains(nodes.at(current)->edges.at(n)->end->num, open) || tentFScore<fValue[nodes.at(current)->edges.at(n)->end->num]){
                                pointers[nodes.at(current)->edges.at(n)->end->num]=current;
                                gScore[nodes.at(current)->edges.at(n)->end->num]=tentGScore;
                                fValue[nodes.at(current)->edges.at(n)->end->num]=tentFScore;
                                if(!contains(nodes.at(current)->edges.at(n)->end->num, open))
                                        open.push_back(nodes.at(current)->edges.at(n)->end->num);
                        }
                }
        }
}

int* Graph::heu(int end){
        int* a = new int [nodes.size()];
        for(int n=0; n<nodes.size(); n++)
                a[n]=sPathFrom(nodes.at(n)->name, nodes.at(end)->name);
        return a;
}


int Graph::sPathFrom(std::string from, std::string target){
        bool test1=true, test2=true;
        int num;
        std::vector<int> visited;
        std::queue<int> q;
        for(int n=0; n<nodes.size(); n++){
                if(nodes.at(n)->name==from){
                        test1=false;
                        num=n;
                }
                if(nodes.at(n)->name==target)
                        test2=false;
        }
        if(test1||test2)
                return -1;
        return bFirst(q, target, visited, num, 0, 0, 0, 0);
}

int Graph::bFirst(std::queue<int>& q, std::string target, std::vector<int>& visited, int num, int steps, int addedThisLevel, int addedNextLevel, int level){
        q.pop();
        Node node=*nodes.at(num);
        if(node.name==target)
                return level;
        if(node.edges.empty()&&q.empty())
                return -1;
        for(int n=0; n<node.edges.size(); n++){
                if(!contains(node.edges.at(n)->end->num, visited)){
                        q.push(node.edges.at(n)->end->num);
                        visited.push_back(node.edges.at(n)->end->num);
                        addedNextLevel++;
                }
        }
        if(steps==addedThisLevel){
                level++;
                addedThisLevel=addedNextLevel;
                addedNextLevel=0;
                steps=0;
        }
        return bFirst(q, target, visited, q.front(), steps+1, addedThisLevel, addedNextLevel, level);
}

bool Graph::pathFrom(std::string from, std::string target){
        bool test1=true, test2=true;
        int num;
        std::vector<int> visited;
        for(int n=0; n<nodes.size(); n++){
                if(nodes.at(n)->name==from){
                        test1=false;
                        num=n;
                }
                if(nodes.at(n)->name==target)
                        test2=false;
        }
        if(test1||test2)
                return false;
        return dFirst(num, target, visited);
}

bool Graph::dFirst(int num, std::string target, std::vector<int>& visited){
        Node node=*nodes.at(num);
        if(node.name==target)
                return true;
        if(node.edges.empty())
                return false;
        for(int n=0; n<node.edges.size(); n++){
                if(!contains(node.edges.at(n)->end->num, visited)){
                        visited.push_back(node.edges.at(n)->end->num);
                        if(dFirst(node.edges.at(n)->end->num, target, visited))
                                return true;
                }
        }
        return false;
}

bool Graph::contains(int num, std::vector<int> visited){
        for(int n=0; n<visited.size(); n++){
                if(visited.at(n)==num)
                        return true;
        }
        return false;
}

Graph::~Graph(){
        for(int n=0; n<nodes.size(); n++){
                for(int i=0; i<nodes.at(n)->edges.size(); i++)
                        delete nodes.at(n)->edges.at(i);
                delete nodes.at(n);
        }
}

int Graph::numNodes(){
        return nodes.size();
}

void Graph::addNode(std::string name, double x, double y){
        Node* temp = new Node;
        temp->name=name;
        temp->x=x;
        temp->y=y;
        temp->num=nodes.size();
        nodes.push_back(temp);
}

void Graph::addEdge(int start, int end, double size){
        if(!(end<nodes.size()&&start<nodes.size()))
                return;
        Edge* temp = new Edge;
        temp->size=size;
        temp->start=nodes.at(start);
        temp->end=nodes.at(end);
        nodes.at(start)->edges.push_back(temp);
}


void Graph::draw(sf::RenderWindow& window){
        for(int j=0; j<nodes.size(); j++){
                for(int i=0; i<nodes.at(j)->edges.size(); i++){
                        double startX=nodes.at(j)->edges.at(i)->start->x;
                        double startY=nodes.at(j)->edges.at(i)->start->y;
                        double endX=nodes.at(j)->edges.at(i)->end->x;
                        double endY=nodes.at(j)->edges.at(i)->end->y;
                        sf::Vertex line[2]={
                                sf::Vertex(sf::Vector2f(startX, startY)),
                                sf::Vertex(sf::Vector2f(endX, endY))
                        };
                        window.draw(line, 2, sf::Lines);
                        sf::CircleShape temp(5, 3);
                        temp.setOrigin(5, 5);
                        temp.setPosition(endX, endY);
                        double theta;
                        if(endX==startX&&startY<endY)
                                theta=3.1415/2;
                        else if(endX==startX&&endY<startY)
                                theta=3*3.1415/2;
                        else{
                                theta=atan((endY-startY)/(startX-endX));
                                if(endX>startX)
                                        theta=3.1415-theta;
                                if(endY>startY)
                                        theta=-theta;
                                if(endX>startX&&endY<startY)
                                        theta=-theta;
                                if(endX<startX&&endY>startY)
                                        theta=atan((endY-startY)/(startX-endX));
                        }
                        temp.rotate(30);
                        temp.rotate(-180/3.1415*theta);
                        temp.move(10*cos(theta), -10*sin(theta));
                        window.draw(temp);
                        std::ostringstream strs;
                        strs << nodes.at(j)->edges.at(i)->size;
                        std::string str = strs.str();
                        tempTxt.setFont(font);
                        tempTxt.setString(str);
                        tempTxt.setPosition((startX+endX)/2+(startX-endX)/5, (startY+endY)/2+(startY-endY)/5);
                        tempTxt.setCharacterSize(30);
                        tempTxt.setColor(sf::Color(0, 255, 255, 255));
                        window.draw(tempTxt);
                }
        }
        for(int j=0; j<nodes.size(); j++){
                sf::CircleShape temp(5, 60);
                temp.setOrigin(5, 5);
                temp.setPosition(sf::Vector2f(nodes.at(j)->x, nodes.at(j)->y));
                temp.setFillColor(sf::Color(255, 0, 0, 255));
                window.draw(temp);
                tempTxt.setFont(font);
                tempTxt.setString(nodes.at(j)->name);
                tempTxt.setPosition(sf::Vector2f(nodes.at(j)->x, nodes.at(j)->y-20));
                tempTxt.setCharacterSize(30);
                tempTxt.setColor(sf::Color(255, 255, 0, 255));
                window.draw(tempTxt);
               
        }
}

#endif
 

EDIT:
Also, the nodes.txt is on the form:

"nodeName" xPosition yPosition
"nodeName" xPosition yPosition

and the edges.txt is on the form:

startNode endNode size
startNode endNode size

where I'm pretty sure my bad practise of sometimes calling the cost of an edge it's "size", which is a dumb thing to do...
« Last Edit: October 16, 2013, 09:42:40 pm by Maximilian »

binary1248

  • SFML Team
  • Hero Member
  • *****
  • Posts: 1405
  • I am awesome.
    • View Profile
    • The server that really shouldn't be running
Re: Fonts, don't we all love fonts...
« Reply #5 on: October 16, 2013, 10:01:48 pm »
Leaving VS2012 at the default warning level, your code already yields a lot of warnings, this is the output at level 4:
Code: [Select]
warning C4018: '<' : signed/unsigned mismatch
warning C4018: '<' : signed/unsigned mismatch
warning C4018: '<' : signed/unsigned mismatch
warning C4018: '<' : signed/unsigned mismatch
warning C4018: '<' : signed/unsigned mismatch
warning C4018: '<' : signed/unsigned mismatch
warning C4018: '<' : signed/unsigned mismatch
warning C4018: '<' : signed/unsigned mismatch
warning C4018: '<' : signed/unsigned mismatch
warning C4018: '<' : signed/unsigned mismatch
warning C4018: '<' : signed/unsigned mismatch
warning C4018: '<' : signed/unsigned mismatch
warning C4018: '<' : signed/unsigned mismatch
warning C4018: '<' : signed/unsigned mismatch
warning C4018: '<' : signed/unsigned mismatch
warning C4244: 'argument' : conversion from 'double' to 'float', possible loss of data
warning C4244: 'argument' : conversion from 'double' to 'float', possible loss of data
warning C4244: 'argument' : conversion from 'double' to 'float', possible loss of data
warning C4244: 'argument' : conversion from 'double' to 'float', possible loss of data
warning C4244: 'argument' : conversion from 'double' to 'float', possible loss of data
warning C4244: 'argument' : conversion from 'double' to 'float', possible loss of data
warning C4018: '<' : signed/unsigned mismatch
warning C4244: 'argument' : conversion from 'double' to 'float', possible loss of data
warning C4244: 'argument' : conversion from 'double' to 'float', possible loss of data
warning C4305: 'argument' : truncation from 'double' to 'float'
warning C4305: 'argument' : truncation from 'double' to 'float'
warning C4701: potentially uninitialized local variable 'num2' used
warning C4701: potentially uninitialized local variable 'current' used
warning C4701: potentially uninitialized local variable 'nCur' used
warning C4701: potentially uninitialized local variable 'num' used
warning C4701: potentially uninitialized local variable 'num' used
As you can see, you might have a lot of uninitialized variables that you evaluate in your branches, hence possibly undefined behaviour. Always, always initialize your POD (int, float, char, etc.) variables with values on definition. Your computer does not zero initialize them for you although that is what many people think.

Although it isn't probable to be the cause of your crashes, you should also consider embracing the RAII idiom and not resorting to using manual memory management. This means, try to avoid using new and delete. You can create the objects as usual, and just copy/move them around as required, this is not Java. In C++11 (which your compiler supports) there should be almost no overhead.

If all else fails and the debugger is not effectively usable, you can always resort to using std::cout every now and then to output status information and values of variables. It doesn't hurt to have feedback about what is really going on in all those loops, since no human can possibly keep track of what would happen with large datasets just by looking at the code.
SFGUI # SFNUL # GLS # Wyrm <- Why do I waste my time on such a useless project? Because I am awesome (first meaning).

Maximilian

  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: Fonts, don't we all love fonts...
« Reply #6 on: October 16, 2013, 10:14:54 pm »
I understand your concerns perfectly well. However, these qarnings are in fact NOT the cause of the problem, trust me on this one, as they are in fact never a real issue. I know about the way c++ handles this and I would not have written my code without baring this in mind. Furthermore, the issue can be narrowed down the use of sf::Font. The errors do not occur if I do not open a font file from memory.

I know it is easy to dismiss it as being yielded by the lazy shortcuts. But I assure you that the way I have used them shuld not yield any significant errors. Furthermore, I have tested the code, it runs fine under all circumstances. One third thing, the errors do not occur in the same scope as the warnings.

This is most surely an issue related to sf::Font, I just don't understand why... :/

wintertime

  • Sr. Member
  • ****
  • Posts: 255
    • View Profile
Re: Fonts, don't we all love fonts...
« Reply #7 on: October 16, 2013, 11:04:10 pm »
It was too much code for me to read all, but from what you have written my conclusion is you most likely have written some code that corrupts memory and then sometimes it does manifest in wrong behaviour of unrelated code - the font and you had also written about a (de-)queue getting corrupted. It can be difficult to accept, but if two unrelated things bug out its most likely your own code.
Those warnings about uninitialized data could be the hint you need, maybe if you fix them your problem is gone, or maybe not and you would have to look at all pointers if any gets out of range of its allocated memory.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Fonts, don't we all love fonts...
« Reply #8 on: October 16, 2013, 11:05:04 pm »
You're using the debug SFML libraries when you build in debug mode, right?
Laurent Gomila - SFML developer

Maximilian

  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: Fonts, don't we all love fonts...
« Reply #9 on: October 17, 2013, 12:56:16 am »
Absolutely! Stranges thing is: The error is now, after fixing the dequeu (which was my fault), a buffer overrun error. This confuses me, as I have written several projects in SFML before and never had this issue. And before you ask, yes the debug/release .lib files linked are correct, I spent an hour checking that over and over again it feels like.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Re: Fonts, don't we all love fonts...
« Reply #10 on: October 17, 2013, 09:07:30 am »
This looks like undefined behavior and may be a result of having a lot of low-level code (manual memory management, raw arrays, uninitialized variables, ...). You should really consider binary1248's advice, idiomatic C++ with RAII will make your code simpler and less error-prone.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Maximilian

  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: Fonts, don't we all love fonts...
« Reply #11 on: October 17, 2013, 09:47:50 am »
Ok, so after having 1. Fixed the uninitialized variables (which, as I will stick to, was not an issue in the first place) 2. Gone through the code from start to finish several times without finding any possibility for memory leaks. I'm forced to assume that is not the cause of the problem. Please trust me when I say this: My code runs a simple graph datastructure. I have spent alot of time on projects like this one before and my errors are not related to missmanagedment of memory on my part. Yet the problem persists.

binary1248

  • SFML Team
  • Hero Member
  • *****
  • Posts: 1405
  • I am awesome.
    • View Profile
    • The server that really shouldn't be running
Re: Fonts, don't we all love fonts...
« Reply #12 on: October 17, 2013, 10:36:03 am »
C++ is very unforgiving when it comes to making mistakes, nobody should assume that stack corruption doesn't happen just because they took every precaution to prevent it. A completely unrelated mistake could lead to any effect, hence the term undefined behaviour. We have had many alleged bugs with sf::Font and sf::Text before, so it is probably one of the classes that we have looked at the most, and by now I can say that it is nigh impossible that it is subject to some internal error. Either the error comes from the Font data source, because it is not properly structured or whatever, or the error comes from the user code. Another possibility is that Visual Studio is being annoying again with its wide string support. Did you try changing that and seeing if it works?

Since this only happens in conjunction with your code and is not reproducible on its own, you must understand that we have to assume that your code is at fault. If your data set is not too huge, can't you step through your code line by line in the debugger and see where it crashes? Optimized path search algorithms shouldn't perform too much on small data sets anyway. Even if it takes moderately long, it is still less time than spending it here discussing possibilities. Without concrete information, we are doing nothing but guessing.
SFGUI # SFNUL # GLS # Wyrm <- Why do I waste my time on such a useless project? Because I am awesome (first meaning).

Maximilian

  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: Fonts, don't we all love fonts...
« Reply #13 on: October 17, 2013, 10:41:52 am »
Ok, so I have been coming at you guys from the wrong angle (i have been annoyed as ***)
Here is the most minimal code I could produce that would yield the buffer overrun issue!
I'm building in both release and debug!

In debug it says the stack around graph gets corrupted, and in release I get a buffer overrun error!
They both happen after system("pause");

This is the code:

Graph.h:

#ifndef GRAPH_INCLUDE
#define GRAPH_INCLUDE
#include <SFML\Graphics.hpp>


class Graph{
        private:
                sf::Font font;
                sf::Text tempTxt;
};

#endif
 

Source.cpp
#include "Graph.h"

int main(int argc, char** argv){
        Graph graph;
        system("pause");
        return 0;
}
 

This code causes the buffer overrun on destruction of the Graph graph; I.E it happens after system("pause");

It is not my memeory management, its proboably my setup... But I don't understand what aspect of it! I have linked correctly etc. etc.... /cry!

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Fonts, don't we all love fonts...
« Reply #14 on: October 17, 2013, 10:47:59 am »
It's funny how things were so complicated and impossible to debug, and suddenly can be reduced to 2 lines of code :P

So... this is definitely an environment issue.

Can you:
- upload your Visual Studio project file
- tell us which version of VC++ you are using
- tell us which archive of SFML you downloaded
Laurent Gomila - SFML developer