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

Author Topic: [SFML 2] Why this fail?  (Read 5634 times)

0 Members and 1 Guest are viewing this topic.

exafi

  • Newbie
  • *
  • Posts: 41
    • View Profile
[SFML 2] Why this fail?
« on: August 21, 2012, 01:35:06 am »
Hi i have next problem with the number line 12 the file: text.hpp

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;
}
 

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: [SFML 2] Why this fail?
« Reply #1 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++. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

kaB00M

  • Full Member
  • ***
  • Posts: 101
    • View Profile
    • Caffeware
    • Email
Re: [SFML 2] Why this fail?
« Reply #2 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.



exafi

  • Newbie
  • *
  • Posts: 41
    • View Profile
Re: [SFML 2] Why this fail?
« Reply #3 on: August 21, 2012, 11:16:19 am »
Maybe some help please?
I am not expert, but i am learning always.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: [SFML 2] Why this fail?
« Reply #4 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;
}
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

exafi

  • Newbie
  • *
  • Posts: 41
    • View Profile
Re: [SFML 2] Why this fail?
« Reply #5 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;
« Last Edit: August 21, 2012, 12:07:15 pm by exafi »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: [SFML 2] Why this fail?
« Reply #6 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.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

exafi

  • Newbie
  • *
  • Posts: 41
    • View Profile
Re: [SFML 2] Why this fail?
« Reply #7 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

Roose Bolton of the Dreadfort

  • Full Member
  • ***
  • Posts: 113
  • Full-time Procrastinator, Part-time programmer.
    • View Profile
    • Personal Portfolio/Website/Blog
Re: [SFML 2] Why this fail?
« Reply #8 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.
Trying so very hard to finish at-least one project.

Watch out for the RAII police, they will get you.

www.bantersaurus-games.com

exafi

  • Newbie
  • *
  • Posts: 41
    • View Profile
Re: [SFML 2] Why this fail?
« Reply #9 on: August 21, 2012, 03:24:14 pm »
I did that, but the error continues

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: [SFML 2] Why this fail?
« Reply #10 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?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

exafi

  • Newbie
  • *
  • Posts: 41
    • View Profile
Re: [SFML 2] Why this fail?
« Reply #11 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
« Last Edit: August 21, 2012, 05:17:42 pm by exafi »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: [SFML 2] Why this fail?
« Reply #12 on: August 21, 2012, 05:29:23 pm »
This also error.
What kind of error?

And:
Do you mix release and debug libraries?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

exafi

  • Newbie
  • *
  • Posts: 41
    • View Profile
Re: [SFML 2] Why this fail?
« Reply #13 on: August 24, 2012, 04:42:26 pm »
how i do that?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: [SFML 2] Why this fail?
« Reply #14 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). ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/