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

Author Topic: Renderedwindow.draw( sf::Text ) is crashing  (Read 3253 times)

0 Members and 2 Guests are viewing this topic.

kmilo9999

  • Newbie
  • *
  • Posts: 5
    • View Profile
Renderedwindow.draw( sf::Text ) is crashing
« on: November 16, 2014, 08:19:39 pm »
Hello Everyone!!

I'm having troubles rendering text on the screen. When my renderedwindow instance call draw method using  a sf::Text instance as a parameter, i got a memory access violation message in MS VS 2012 c++


I'm using sfml 2.1 and I have triple checked that I'm not using the debug libraries.


here is my code:

class MyClass{

void Initialize(){
   mWindow = new new sf::RenderWindow();
   sf::Font myFont ;
   if (!myFont.loadFromFile("arial.ttf"))
   {
            //Throw error
   }

       text = new sf::Text();

   text->setFont(myFont);
   text->setCharacterSize(50);
       text->setPosition(250,250);
         text->setColor(sf::Color::Red);

   // set the text style
   text->setStyle(sf::Text::Bold | sf::Text::Underlined);

   text->setString("Hello world");
}

void draw(){
              mWindow->clear();
      
      mWindow->draw(*text);   // this line throw the memory access violation message
      mWindow->display();

}

}

private:
sf::RenderWindow* mWindow;
sf::Text* text;
}


void main(){
   MyClass* mclass = new MyClass();
  mclass->Initialize();

 //game loop
  while (......)  {
     mclass->draw();
  }

}

the error is :

Unhandled exception at 0x51C12E59 (sfml-graphics-2.dll) in CDIAZ_2DENGINE.exe: 0xC0000005: Access violation reading location 0x3F800004.

I'll appreciate the help!
« Last Edit: November 16, 2014, 08:46:21 pm by kmilo9999 »

Gambit

  • Sr. Member
  • ****
  • Posts: 283
    • View Profile
Re: Renderedwindow.draw( sf::Text ) is crashing
« Reply #1 on: November 16, 2014, 08:25:56 pm »
Care to share the rest of your
Hello Everyone!!

I'm having troubles rendering text in the screen. When my renderedwindow instance call draw method and i send a sf::Text instance as a parameter, i got a memory access violation message in MS VS c++

here is my code:

class MyClass{


}

Care to share the rest of your code?

kmilo9999

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: Renderedwindow.draw( sf::Text ) is crashing
« Reply #2 on: November 16, 2014, 08:46:35 pm »
Hello Everyone!!

I'm having troubles rendering text on the screen. When my renderedwindow instance call draw method using  a sf::Text instance as a parameter, i got a memory access violation message in MS VS 2012 c++


I'm using sfml 2.1 and I have triple checked that I'm not using the debug libraries.


here is my code:

class MyClass{

void Initialize(){
   mWindow = new new sf::RenderWindow();
   sf::Font myFont ;
        if (!myFont.loadFromFile("arial.ttf"))
        {
            //Throw error
        }

       text = new sf::Text();

        text->setFont(myFont);
        text->setCharacterSize(50);
       text->setPosition(250,250);
        text->setColor(sf::Color::Red);

        // set the text style
        text->setStyle(sf::Text::Bold | sf::Text::Underlined);

        text->setString("Hello world");
}

void draw(){
              mWindow->clear();
               
                mWindow->draw(*text);   // this line throw the memory access violation message
                mWindow->display();

}

}

private:
sf::RenderWindow* mWindow;
sf::Text* text;
}


void main(){
   MyClass* mclass = new MyClass();
  mclass->Initialize();

 //game loop
  while (......)  {
     mclass->draw();
  }

}

the error is :

Unhandled exception at 0x51C12E59 (sfml-graphics-2.dll) in CDIAZ_2DENGINE.exe: 0xC0000005: Access violation reading location 0x3F800004.

I'll appreciate the help!
« Last Edit: November 16, 2014, 08:55:25 pm by eXpl0it3r »

kmilo9999

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: Renderedwindow.draw( sf::Text ) is crashing
« Reply #3 on: November 16, 2014, 08:52:48 pm »
ohhhh I solved!!! LOL...

When I'm initializing the font :

sf::Font myFont ;
   if (!myFont.loadFromFile("arial.ttf"))
   {
            //Throw error
   }

I changed by :

sf::Font* myFont = new sf::Font();
        if (!myFont->loadFromFile("arial.ttf"))
        {
                 //Throw error
        }

text->setFont(*myFont);
 
« Last Edit: November 16, 2014, 08:55:06 pm by eXpl0it3r »

Gambit

  • Sr. Member
  • ****
  • Posts: 283
    • View Profile
Re: Renderedwindow.draw( sf::Text ) is crashing
« Reply #4 on: November 16, 2014, 08:56:01 pm »
Im not sure why you copy/pasted your original post as a response but you should consider using code tags for code. Further more, classes have private access specifiers by default, so unless I have missed something, you are trying to call private methods publically. Also, unless you have a good reason, you probably shouldnt be allocating things on the heap, it creates unnecessary complexity in code. ALSO, you should change your MyClass::Initialize to a proper constructor and initialize your members in that.

The code you have provided is either not complete or you are getting a lot of compile errors. your text variable is assigned but never defined? And also this:
Code: [Select]
mWindow = new new sf::RenderWindow(); is incorrect.

kmilo9999

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: Renderedwindow.draw( sf::Text ) is crashing
« Reply #5 on: November 16, 2014, 09:07:02 pm »
Sorry for copy/paste my message twice, while i was writing my question, the session timed out and restarted everything :(


Is this line ok ?


sf::Font* myFont = new sf::Font();
 

so.. i shouldn't use pointers to any sfml type?

Thank u for answering so quick !

Gambit

  • Sr. Member
  • ****
  • Posts: 283
    • View Profile
Re: Renderedwindow.draw( sf::Text ) is crashing
« Reply #6 on: November 16, 2014, 09:11:15 pm »
Pointers are not the same thing as dynamic memory allocation. I suspect that you are not deleting any of these pointers anywhere which means that you will be getting memory leaks. Store it on the stack or use smart pointers (I advise using the stack though). No offence but you seem pretty inexperienced with OOP and dynamic memory allocation so for the sake of simplicity and safety (and as advised by almost everyone), dont use new unless you know what you are doing.

Code: [Select]
sf::Font myFont; will work fine for you.

kmilo9999

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: Renderedwindow.draw( sf::Text ) is crashing
« Reply #7 on: November 16, 2014, 09:35:19 pm »
Thank u so much !!

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Renderedwindow.draw( sf::Text ) is crashing
« Reply #8 on: November 16, 2014, 09:46:21 pm »
What Gambit was talking about also applies to RenderWindow and Text objects  ;)
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Gambit

  • Sr. Member
  • ****
  • Posts: 283
    • View Profile
Re: Renderedwindow.draw( sf::Text ) is crashing
« Reply #9 on: November 16, 2014, 09:48:34 pm »
What Gambit was talking about also applies to RenderWindow and Text objects  ;)

Thank you. I was in 2 minds about whether to explain that or not.