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

Author Topic: My program crashes on exit because of window.draw(text)  (Read 4498 times)

0 Members and 1 Guest are viewing this topic.

ivicaSD

  • Newbie
  • *
  • Posts: 5
    • View Profile
    • Email
My program crashes on exit because of window.draw(text)
« on: January 31, 2016, 11:45:33 pm »
I am in a process of making a game (Breakout clone).
I have solved several problems concerning sf::text (loading font, and passing font AND text by reference), and it works just fine, but there is still one problem left: when I close my window, it crashes with a system message "program.exe has stopped working" and "Windows is checking for a solution".
Build log shows only one message: "Process terminated with status -1073741819" (this is not a random number, every time it ends with the same code, no matter what stage of the game I exit from).
When I remove the line window.draw(text), the program runs OK and ends OK.

I use Code::Blocks 13.12 with the latest version of SFML, everything is perfectly configured (I have done A LOT of reading on that subject), it seems like some bug on this function?

My code is too big and complex to post it here (10 files, 4 classes), but if it is needed to see what is going on, I'll take the effort to copy/paste it.

When I run the program in DEBUG mode, i get 12 lines:
#0 77B4DFD4     ntdll!TpWaitForWork() (C:\Windows\system32\ntdll.dll:??)
#1 41A00000     ?? () (??:??)
#2 0028F83C     ?? () (??:??)
#3 775998CD     msvcrt!free() (C:\Windows\syswow64\msvcrt.dll:??)
#4 00AD0000     ?? () (??:??)
#5 004E0FD0     __gnu_cxx::new_allocator<sf::Vertex>::deallocate(this=0x28fb10, __p=0x41a00000) (c:/program files (x86)/codeblocks/mingw/bin/../lib/gcc/mingw32/4.7.1/include/c++/ext/new_allocator.h:100)
#6 00517572     std::_Vector_base<sf::Vertex, std::allocator<sf::Vertex> >::_M_deallocate(this=0x28fb10, __p=0x41a00000, __n=2575617234) (c:/program files (x86)/codeblocks/mingw/bin/../lib/gcc/mingw32/4.7.1/include/c++/bits/stl_vector.h:175)
#7 0051772E     std::_Vector_base<sf::Vertex, std::allocator<sf::Vertex> >::~_Vector_base(this=0x28fb10, __in_chrg=<optimized out>) (c:/program files (x86)/codeblocks/mingw/bin/../lib/gcc/mingw32/4.7.1/include/c++/bits/stl_vector.h:161)
#8 0053752C     std::vector<sf::Vertex, std::allocator<sf::Vertex> >::~vector(this=0x28fb10, __in_chrg=<optimized out>) (c:/program files (x86)/codeblocks/mingw/bin/../lib/gcc/mingw32/4.7.1/include/c++/bits/stl_vector.h:404)
#9 004DE0DF     sf::VertexArray::~VertexArray(this=0x28fb0c, __in_chrg=<optimized out>) (D:/Diplomski/CodeBlocks/SFML-2.3.2/include/SFML/Graphics/VertexArray.hpp:45)
#10 004DF6C2    sf::Text::~Text(this=0x28fa30, __in_chrg=<optimized out>) (D:/Diplomski/CodeBlocks/SFML-2.3.2/include/SFML/Graphics/Text.hpp:48)
#11 004E0812    Rezultat::~Rezultat(this=0x28f924, __in_chrg=<optimized out>) (include/Rezultat.h:6)
#12 00401D9A    main() (D:\Diplomski\CodeBlocks\Diplomski\main.cpp:152)
 

I have found THIS thread with the same error:
http://en.sfml-dev.org/forums/index.php?topic=6130.0

BUT, I am not using default font, I load "verdana.ttf" and it works fine!


To prevent the crash, the default font must never be instanciated, so you have to pass your own font to the constructor of all your sf::Text instances.
Doing it with the SetFont function is already too late.
 

Good hint is to send the font to the constructor, so I am going to try this.

EDIT: I am not able to use the constructor with the font, I will post my code to show why.
« Last Edit: February 01, 2016, 01:23:40 am by ivicaSD »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: My program crashes on exit because of window.draw(text)
« Reply #1 on: February 01, 2016, 12:53:53 am »
Please use the edit function next time.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: My program crashes on exit because of window.draw(text)
« Reply #2 on: February 01, 2016, 01:03:59 am »
It looks like your application is not exiting properly by reaching its end. (I just googled your error message)
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

ivicaSD

  • Newbie
  • *
  • Posts: 5
    • View Profile
    • Email
Re: My program crashes on exit because of window.draw(text)
« Reply #3 on: February 01, 2016, 01:28:15 am »
It looks like your application is not exiting properly by reaching its end. (I just googled your error message)

Yes, I got it.
I have 3 conditions to exit:
- return 0 at the end of the program (normal exit)
- return 101 when you press ESC key
- return 202 when you click on X (close the game window)

 while (window.pollEvent(action))
        {
            if (action.type == sf::Event::Closed)
            {
                window.close();
                return 101;
            }
            if (action.type == sf::Event::KeyPressed)
            {
                if (action.key.code == sf::Keyboard::Escape)
                {
                    window.close();
                    return 202;
                }
            }
        }
 

I will repeat the core of my problem:

When I remove the line window.draw(text), the program runs OK and ends OK.


Good hint is to send the font to the constructor, so I am going to try this.

EDIT: I am not able to use the constructor with the font, I will post my code to show why.

This is my class for holding results (number of points, number of lives):

Results.H:
#include "Parametri.h"

class Rezultat
{
public:
    Rezultat(sf::Font &font);

    //  (getters)
    sf::Text& KojiJeRezultat() { return _tekstRezultat; }
    sf::Text& KolikoZivota()   { return _tekstBrojZiv;  }
    int BrojZivota()           { return _brojZivota;    }
    int BrojPoena()            { return _rezultat;      }

    //  (setters)
    void PovecajRez(int x) { _rezultat += x; }
    void SmanjiZivot() { _brojZivota--; }

    // update
    void Promena();

private:

    int _rezultat;
    int _brojZivota;
    sf::Text _tekstRezultat;     //  <-- This one should be the constructor? I can't force a font to it. Or can I?
    sf::Text _tekstBrojZiv;
};
 

and this is Results.cpp:

#include "Rezultat.h"

Rezultat::Rezultat(sf::Font &font)
{
    // initial values
    _rezultat = 0;
    _brojZivota = 3;

    // score and number of lives
    _tekstRezultat.setFont(font);
    _tekstRezultat.setString("Broj poena: 0");
    _tekstRezultat.setCharacterSize(20);
    _tekstRezultat.setStyle(sf::Text::Bold);
    _tekstRezultat.setColor(sf::Color::Black);
    _tekstRezultat.setPosition(duzinaBloka + 2 * razmak, 3 * razmak);

    _tekstBrojZiv.setFont(font);
    _tekstBrojZiv.setString("Broj zivota: 3");
    _tekstBrojZiv.setCharacterSize(20);
    _tekstBrojZiv.setStyle(sf::Text::Bold);
    _tekstBrojZiv.setColor(sf::Color::Blue);
    _tekstBrojZiv.setPosition(sirinaProzora - 4 * (razmak + duzinaBloka), 3 * razmak);
}

void Rezultat::Promena()
{
    _tekstRezultat.setString("Broj poena: " + std::to_string(_rezultat));
    _tekstBrojZiv.setString("Broj zivota: " + std::to_string(_brojZivota));
}
 
« Last Edit: February 01, 2016, 02:06:21 am by eXpl0it3r »

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: My program crashes on exit because of window.draw(text)
« Reply #4 on: February 01, 2016, 04:36:32 pm »
What does your operating system make of the return values 101 and 202?
Why are you exiting the program from there?
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

ivicaSD

  • Newbie
  • *
  • Posts: 5
    • View Profile
    • Email
Re: My program crashes on exit because of window.draw(text)
« Reply #5 on: February 01, 2016, 06:22:00 pm »
What does your operating system make of the return values 101 and 202?
Why are you exiting the program from there?

OS does nothing.
I exit from the program because user have chosen to exit (whether by clicking on close, or by hitting ESC)

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: My program crashes on exit because of window.draw(text)
« Reply #6 on: February 01, 2016, 07:03:48 pm »
Why, then, are you returning those values?

Can you not just exit the actual loop when people choose to quit, like this or this?
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

ivicaSD

  • Newbie
  • *
  • Posts: 5
    • View Profile
    • Email
Re: My program crashes on exit because of window.draw(text)
« Reply #7 on: February 01, 2016, 08:40:35 pm »
Why, then, are you returning those values?

Can you not just exit the actual loop when people choose to quit, like this or this?

I am returning values as a signal in which way the program ended. No real need for it.
I removed those lines. That fixed the problem with program crashing.

Thanks a lot! :)



But, now when i close the game window (or hit ESC), command prompt remains open with no message. After I close it, the build log gives me the message "Process terminated with status -1073741510".

I will read the resources you linked for me, thanks for them, too!

ivicaSD

  • Newbie
  • *
  • Posts: 5
    • View Profile
    • Email
Re: My program crashes on exit because of window.draw(text)
« Reply #8 on: February 15, 2016, 02:41:18 pm »

I will read the resources you linked for me, thanks for them, too!

I have finished my project and I still have THE SAME problem. Everything works flawless, until I put this line:
window.draw(text)

Without it, my program exits normally.
When I put it, everything works fine, until the program finishes.
Then I have a message: "Process terminated with status -1073741819".

I will have to use SPRITES with TEXTURES to show my result, because TEXT is causing me this problem.
I suspect there is some kind of bug in the sf::TEXT class?


If there is a need to look into the source, it has 368 lines in main + 15 files with classes (8 .h and 7 .cpp), this is the Dropbox link: https://www.dropbox.com/s/uty67a165fel4qn/Diplomski.zip?dl=0

Lines 269 and 270 are used to show the score and number of lives. When I remove them, everything works!



----------------------------------------------------------------------------
edit:  I have replaced sf::Text objects with sprites and textures. Everything works fine now.
Here is the Dropbox link to the project: https://www.dropbox.com/s/esyl14ri6w4j6on/DiplomskiX.zip?dl=0
« Last Edit: February 16, 2016, 03:18:54 pm by ivicaSD »