SFML community forums

Help => General => Topic started by: exafi on August 21, 2012, 01:35:06 am

Title: [SFML 2] Why this fail?
Post by: exafi on August 21, 2012, 01:35:06 am
Hi i have next problem with the number line 12 the file: text.hpp
(http://img832.imageshack.us/img832/7138/sinttulo2vg.png) (http://imageshack.us/photo/my-images/832/sinttulo2vg.png/)
Here the code
main.cpp
#include <iostream>
using namespace std;
#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>
#include "object.hpp"
#include "text.hpp"
int main(int argc,char *argv[]){
    // Definition
    sf::RenderWindow myWindow(sf::VideoMode(800,400,32),"class Text");
    Text *Texto1 = new Text;
    Texto1->Display(myWindow);
    Texto1->SetText("hola");

    cout << "\n# Game Over"<< endl;
    myWindow.display();
    return 0;
}
 
object.cpp
#include "object.hpp"
Object::Object(){}
Object::~Object(){}

void Object::Display(sf::RenderWindow &t){
    cout << "Invoked class Object"<< endl;
}
 
object.hpp
#ifndef _objecthpp_
#define _objecthpp_
#include <iostream>
using namespace std;
#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>
class Object {
    public:
        Object();
        ~Object();
        virtual void Display(sf::RenderWindow &);
    private:
};
#endif
 
text.hpp
#ifndef _texthpp_
#define _texthpp_
#include <iostream>
using namespace std;
#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>
#include "object.hpp"
class Text : public Object {
    public:
        Text();
        ~Text();
        void Display(sf::RenderWindow &);
        void SetText(char *);
    private:
        sf::Text myText;
};
#endif

 
text.cpp
#include "Text.hpp"
Text::Text(){
}
Text::~Text(){
}

void Text::Display(sf::RenderWindow &t){

    cout << "Invoked class Text"<< endl;
}
void Text::SetText(char *t){
    myText.setString(sf::String(t));
    //cout << "Text: "<< string(myText->getString()) << endl;
}
 
Title: Re: [SFML 2] Why this fail?
Post by: eXpl0it3r on August 21, 2012, 02:24:03 am
I guess the problem is, that you're allocating your class on the heap with new and you never delete it (= memory leak!!!) and then SFML destroies the contexts etc but the text still exists which can lead to problems...
Then again you should maybe take out your C++ book again and learn a bit more how to program in C++. ;)
Title: Re: [SFML 2] Why this fail?
Post by: kaB00M on August 21, 2012, 08:42:12 am
xploiter, in this case, when will the memory be released.

Also, are all stack objects properly destroyed when one hits the X close button on the window.
Title: Re: [SFML 2] Why this fail?
Post by: exafi on August 21, 2012, 11:16:19 am
Maybe some help please?
I am not expert, but i am learning always.
Title: Re: [SFML 2] Why this fail?
Post by: eXpl0it3r on August 21, 2012, 11:20:46 am
xploiter, in this case, when will the memory be released.
Never actually, that's why it's called memory leaks. But I guess the development enviroment takes care of that a bit more and releases it anyways which then again is after the opengl context are already destroyed and thus could lead to such problems.

Maybe some help please?
How about this:
int main(){
    // Definition
    sf::RenderWindow myWindow(sf::VideoMode(800,400,32),"class Text");
    Text Texto1;
    Texto1.Display(myWindow);
    Texto1.SetText("hola");

    cout << "\n# Game Over"<< endl;
    myWindow.display();
    return 0;
}
Title: Re: [SFML 2] Why this fail?
Post by: exafi on August 21, 2012, 12:05:27 pm
How about this:
int main(){
    // Definition
    sf::RenderWindow myWindow(sf::VideoMode(800,400,32),"class Text");
    Text Texto1;
    Texto1.Display(myWindow);
    Texto1.SetText("hola");

    cout << "\n# Game Over"<< endl;
    myWindow.display();
    return 0;
}

Error
I tried that and continues the problems, i want is pointer for the polimorfisms.

Example:

Object *text = new text;
Object *button= new Button;
Title: Re: [SFML 2] Why this fail?
Post by: eXpl0it3r on August 21, 2012, 12:25:56 pm
Does this minimal example also crash?
#include <SFML/Graphics.hpp>
int main()
{
    sf::RenderWindow window(sf::VideoMode(800, 600), "Test");
}

i want is pointer for the polimorfisms.

Object *text = new text;
Object *button= new Button;
You have to know C++ is not equal to Java. So whenever you call new you have to call delete! ;)
It's generally not the best design to have one object from which every other class derives. C++ is not a fully object oriented language and one should not try to force such a thing up on it. So one should use polymorphism and other OOP principles only in places where the make sense and are not artificial products.
Title: Re: [SFML 2] Why this fail?
Post by: exafi on August 21, 2012, 12:49:41 pm
Yes, i call to delete, but the error is when invoke the line "myText.setString(sf::String(myString));"
I don't understand the error sorry
Title: Re: [SFML 2] Why this fail?
Post by: Roose Bolton of the Dreadfort on August 21, 2012, 02:41:05 pm
Why in your set text function are you passing in a char* then casting it too a std::string? You may aswel just pass a string into the function then avoiding casting.
Title: Re: [SFML 2] Why this fail?
Post by: exafi on August 21, 2012, 03:24:14 pm
I did that, but the error continues
Title: Re: [SFML 2] Why this fail?
Post by: eXpl0it3r on August 21, 2012, 03:30:47 pm
Yes, i call to delete, but the error is when invoke the line "myText.setString(sf::String(myString));"
Well then you've informed us wrongly in your first post, because you said Text.hpp instead of Text.cpp...  :-\

Do you mix release and debug libraries?
Title: Re: [SFML 2] Why this fail?
Post by: exafi on August 21, 2012, 05:14:46 pm
This also error.

using namespace std;
#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>
int main(int argc,char *argv[]){
    sf::Text t1;
    t1.setString("hola");
    return 0;
}

sorry i am moron xDD
Title: Re: [SFML 2] Why this fail?
Post by: eXpl0it3r on August 21, 2012, 05:29:23 pm
This also error.
What kind of error?

And:
Do you mix release and debug libraries?
Title: Re: [SFML 2] Why this fail?
Post by: exafi on August 24, 2012, 04:42:26 pm
how i do that?
Title: Re: [SFML 2] Why this fail?
Post by: eXpl0it3r on August 24, 2012, 10:20:43 pm
how i do that?
By having set release mode in your IDE and linking against the libraries with the '-d' postfix (e.g. sfml-graphics-d.lib) or having set debug mode and linking against the libraries without postfix (e.g. sfml-graphics.lib). ;)