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

Author Topic: sf::Text causes application crash after being deleted with 'delete' instruction  (Read 4839 times)

0 Members and 1 Guest are viewing this topic.

Christ00pher

  • Newbie
  • *
  • Posts: 10
    • View Profile
    • Email
Hello everyone,
as it is written it the title, I have some issues with sf::Text destructor.
A quick glance at my project:
I have an app with MENU with 4 buttons. My app is divided into 4 states - StateMenu (first state, shows the whole MENU), stateStart, StateManage and StateOptions. Every state is a class which derives from the basic abstract class IState. In main.cpp I created a loop with polymorphic pointer. Firstly it is assigned to the StateMenu class and it changes if needed. During the change, I call:
delete state; // state is IState* state;
state = new SomeState();
But when I call the 'delete' instruction, the program crushes. Also when I call delete after loop before return 0;, I get the same problem. I started looking for the problem and I found out that sf::Text object messes everything up. I have a class Caption which contains sf::Text, and in debugger when sf::Text~Text is called the problem occurs.
I even commented every line in my class except sf::Text declaration, the classes' constructor and destructor (both empty) and the problem still occured.
So my question is - why delete instruction causes problems? When I don't use this instruction, the program exits without any problems. What is going on?

I run Ubuntu 16.04, working on CodeLite 9.2.0 and compiling with Cross GCC (x86_64-linux-gnu)

Link to the project on github: https://github.com/Christ00pher/Flashcards-SFML
« Last Edit: November 05, 2016, 09:16:40 pm by Christ00pher »

DarkRoku12

  • Full Member
  • ***
  • Posts: 203
  • Lua coder.
    • View Profile
    • Email
Whitout code or debug info we cannot point the problem itself.

You i would want to make a bet, the problem is not sf::Text, is another part of your code is messing the memory.

I remember one week ago having a problem with the lua_close() function, that frees the resources used by the lua state.

And there was a error triggered in the lua.dll, but i was overwriting the memory in other part of my program, probably messing up the memory allocated by Lua.

And that caused the crash.

Debuggers tend to say where te crash was found, but not what was the origin of the crash.
I would like a spanish/latin community...
Problems building for Android? Look here

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11016
    • View Profile
    • development blog
    • Email
Make sure the font object gets destroyed after the text objects destructions. Then again, this shouldn't cause a crash.

But as has been said, without a stack trace and some code, we can't help much further.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Christ00pher

  • Newbie
  • *
  • Posts: 10
    • View Profile
    • Email
Level/Address/Function/File/Line

0  0x00007ffff6c71a92  __GI___libc_free  /build/glibc-GKVZIf/glibc-2.23/malloc/malloc.c  2952
1  0x000000000040b2bc  __gnu_cxx::new_allocator<sf::Vertex>::deallocate  /usr/include/c++/5/ext/new_allocator.h  110
2  0x000000000040b28a  __gnu_cxx::__alloc_traits<std::allocator<sf::Vertex> >::deallocate  /usr/include/c++/5/ext/alloc_traits.h  185
3  0x000000000040b22a  std::_Vector_base<sf::Vertex, std::allocator<sf::Vertex> >::_M_deallocate  /usr/include/c++/5/bits/stl_vector.h  178
4  0x000000000040b175  std::_Vector_base<sf::Vertex, std::allocator<sf::Vertex> >::~_Vector_base  /usr/include/c++/5/bits/stl_vector.h  160
5  0x000000000040b0e2  std::vector<sf::Vertex, std::allocator<sf::Vertex> >::~vector  /usr/include/c++/5/bits/stl_vector.h  425
6  0x000000000040af0d  sf::VertexArray::~VertexArray  /home/chris/dev/sfml/SFML/include/SFML/Graphics/VertexArray.hpp  45
7  0x000000000040afb0  sf::Text::~Text  /home/chris/dev/sfml/SFML/include/SFML/Graphics/Text.hpp  48
8  0x000000000040a4f6  Caption::~Caption  /home/chris/CodeLiteProjects/Flashcards-SFML/Caption.cpp  23
9  0x000000000040c67f  Button::~Button  /home/chris/CodeLiteProjects/Flashcards-SFML/Button.cpp  32
10  0x0000000000408191  StateMenu::~StateMenu  /home/chris/CodeLiteProjects/Flashcards-SFML/StateMenu.h  21
11  0x00000000004082b4  StateMenu::~StateMenu  /home/chris/CodeLiteProjects/Flashcards-SFML/StateMenu.h  21
12  0x0000000000403db3  main  /home/chris/CodeLiteProjects/Flashcards-SFML/main.cpp  48


Here's what I see in debugger after I exit program while being in StateMenu state.

@DarkRoku
I don't know what else could mess the program. But even if I comment every object that has something to do with sf::Text in StateMenu class and just declare sf::Text variable directly into StateMenu, then press window's X and exit the program, the 'delete StateMenu' instruction is called and the program crashes again. (With just sf::Text variable in class). Here's the debugger info when I don't have any buttons captions etc in my class, just one uninitialized sf::Text variable.

0  0x00007ffff6c71a92  __GI___libc_free  /build/glibc-GKVZIf/glibc-2.23/malloc/malloc.c  2952
1  0x0000000000407acc  __gnu_cxx::new_allocator<sf::Vertex>::deallocate  /usr/include/c++/5/ext/new_allocator.h  110
2  0x0000000000407a9a  __gnu_cxx::__alloc_traits<std::allocator<sf::Vertex> >::deallocate  /usr/include/c++/5/ext/alloc_traits.h  185
3  0x0000000000407a3a  std::_Vector_base<sf::Vertex, std::allocator<sf::Vertex> >::_M_deallocate  /usr/include/c++/5/bits/stl_vector.h  178
4  0x0000000000407985  std::_Vector_base<sf::Vertex, std::allocator<sf::Vertex> >::~_Vector_base  /usr/include/c++/5/bits/stl_vector.h  160
5  0x00000000004078f2  std::vector<sf::Vertex, std::allocator<sf::Vertex> >::~vector  /usr/include/c++/5/bits/stl_vector.h  425
6  0x000000000040771d  sf::VertexArray::~VertexArray  /home/chris/dev/sfml/SFML/include/SFML/Graphics/VertexArray.hpp  45
7  0x00000000004077c0  sf::Text::~Text  /home/chris/dev/sfml/SFML/include/SFML/Graphics/Text.hpp  48
8  0x0000000000407637  StateMenu::~StateMenu  /home/chris/CodeLiteProjects/Flashcards-SFML/StateMenu.h  22
9  0x00000000004076c2  StateMenu::~StateMenu  /home/chris/CodeLiteProjects/Flashcards-SFML/StateMenu.h  22
10  0x0000000000403db3  main  /home/chris/CodeLiteProjects/Flashcards-SFML/main.cpp  48


And according to sending a code, I don't know what to post here, the problem is just too complex and I would have to send you few classes to show you what's going on.
« Last Edit: November 02, 2016, 09:49:57 pm by Christ00pher »

DarkRoku12

  • Full Member
  • ***
  • Posts: 203
  • Lua coder.
    • View Profile
    • Email
And according to sending a code, I don't know what to post here, the problem is just too complex and I would have to send you few classes to show you what's going on.

Sure i want to help you.
But i will be limited because i run Windows 8 and you
Quote
I run Ubuntu 16.04, working on CodeLite 9.2.0 and compiling with Cross GCC (x86_64-linux-gnu)

If its a memory problem inside your classes sure i can help.
But if it something with the OS/Compiler/SFML (for Linux) i can't do much.
I would like a spanish/latin community...
Problems building for Android? Look here

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Quote
And according to sending a code, I don't know what to post here, the problem is just too complex and I would have to send you few classes to show you what's going on.
Debugging doesn't mean to just stare at your code until you find what's wrong. When things become too complex, you must actively isolate the part that is causing the problem. Either by disabling parts of your code until it becomes minimal (ie. if you disable anything else the problem doesn't show up anymore), or reproduce the problem in a new, complete and minimal code.

You've somehow managed to already reduce the code in your second attempt to debug it, so go on and reduce it even more, then post a complete and minimal code and for sure we'll be able to tell what's wrong ;)
Laurent Gomila - SFML developer

Christ00pher

  • Newbie
  • *
  • Posts: 10
    • View Profile
    • Email
Okay let's give it a shot.

I don't think there's any sense in posting all classes' code so let's focus on StateMenu.
What's more, I found strange thing when playing with the code, but let's post it firstly:

main.cpp

#include "Window.h"
//...

void changeState(IState *(&state))
{
        if ( state->getStateMenu() )
        {
                delete state;
                state = new StateMenu();
        }
       
        else if ( state->getStateOptions() )
        {
                delete state;
                state = new StateOptions();
        }
               
        else if ( state->getStateManage() )
        {
                delete state;
                state = new StateManage();
        }
       
        else if ( state->getStateStart() )
        {
                delete state;
                state = new StateStart();
        }
}

int main()
{
        Sound::instance().playSoundtrack();
       
        IState* state = new StateMenu();
        while(!state->toExit())
        {
                state->update();
                state->render();
                changeState(state);
        }
       
        delete state;
       
        return 0;
}

IState.h

#pragma once
#include "Flashcards.h"
#include "Window.h"
#include "Button.h"
#include "Sound.h"
#include <SFML/Graphics.hpp>

class IState
{
protected:
        //...
        sf::Texture t_background;
        sf::Sprite background;
public:
        IState();
        virtual ~IState() {};
        virtual void update() = 0;
        virtual void render() = 0;
        //...
};
 

StateMenu.h

class StateMenu : public IState
{
private:
        const short TOTAL_BUTTONS;
        sf::Vector2f buttonSize;
        Button start;
        Button manage;
        Button options;
        Button exit;
        Button speaker;
        sf::Text text;
        sf::String string;
public:
        StateMenu();
        ~StateMenu() {};
        //...
};
 

So, the anomaly I found is when I comment all buttons from StateMenu.h (class Button contains Caption object, and Caption class contains sf::Text and sf::Font), declare sf::Text variable into StateMenu.h and comment following two lines from IState.h class:
        sf::Texture t_background;
   sf::Sprite background;
... there is no crash. However, if I decalre buttons and comment background lines from IState, the crash still happens. What the hell? How is this even connected? Maybe there's something wrong with IDE I use? I'm super confused right now.
« Last Edit: November 03, 2016, 10:23:12 am by Christ00pher »

DarkRoku12

  • Full Member
  • ***
  • Posts: 203
  • Lua coder.
    • View Profile
    • Email
Main.cpp

void changeState(IState *(&state))
{
    if ( state->getStateMenu() )
    {
        delete state;
        state = new StateMenu();
    }
   
    else if ( state->getStateOptions() )
    {
        delete state;
        state = new StateOptions();
    }
       
    else if ( state->getStateManage() )
    {
        delete state;
        state = new StateManage();
    }
   
    else if ( state->getStateStart() )
    {
        delete state;
        state = new StateStart();
    }
}
 

StateMenu.h

class StateMenu : public IState
{
private:
    //...
public:
    StateMenu();
    ~StateMenu() {};
    //...
};

class IState
{
protected:
    //...
    sf::Texture t_background;
    sf::Sprite background;
public:
    IState();
    virtual ~IState() {};
    virtual void update() = 0;
    virtual void render() = 0;
        //...
};
 

I see that IState is an abstract class.

in the line after the while loop 
 delete state
did you know that you only are deleting
 IState
and not the full
 StateMenu
. ?

Are you sure you're correctly deallocating resources on those if/else if on the main.cpp?
« Last Edit: November 03, 2016, 07:23:01 pm by DarkRoku »
I would like a spanish/latin community...
Problems building for Android? Look here

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Quote
did you know that you only are deleting
 IState
and not the full
 StateMenu
. ?
The destructor is virtual, so that's fine.
Laurent Gomila - SFML developer

DarkRoku12

  • Full Member
  • ***
  • Posts: 203
  • Lua coder.
    • View Profile
    • Email
Quote
did you know that you only are deleting
 IState
and not the full
 StateMenu
. ?
The destructor is virtual, so that's fine.

Yes, i didn't note this.
But we still don't know if inside those virtual destructors the memory is well-released, we need to be sure of all before blame sf::Text.

A single bad deallocating can mess a lot of things. (Thats why new languages want to bring memory safety).
« Last Edit: November 04, 2016, 04:29:33 am by DarkRoku »
I would like a spanish/latin community...
Problems building for Android? Look here

Christ00pher

  • Newbie
  • *
  • Posts: 10
    • View Profile
    • Email
Every destructor in my program is empty, the virtual one too. If anybody comes up with an idea that could explain why the program is crashing, I would appreciate sharing it here. Recently I have had no time to search for the bug, but I will try again this evening :)

@EDIT: If you want to check the whole project, here it is on github: https://github.com/Christ00pher/Flashcards-SFML
« Last Edit: November 05, 2016, 09:16:13 pm by Christ00pher »

 

anything