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

Author Topic: Program crashes on exit (but only after moving characters)  (Read 5062 times)

0 Members and 1 Guest are viewing this topic.

Mlikaon

  • Newbie
  • *
  • Posts: 17
    • View Profile
Program crashes on exit (but only after moving characters)
« on: October 20, 2013, 01:19:47 pm »
Hello everybody,

I've got some trouble in my program : when i call the window.close() function, it crashes with an "Access Violation" type error

If i try to close the program just after i started it, it works properly.
But a problem occurs if i close the program after moving some of my characters.

When i move a character, i use pointers and dynamic arrays like Vector or Deque.
Dynamic arrays are to create a dynamic path (with an A* algorithm) my characters will follow.

I'm quite new in C++ and SFML, so i don't really succeed in finding a way to debug this, please give me some help ^_


PS : I'm using Windows 7, latest SFML version (must be 2.1), and Visual Studio 11

Thank you!

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
AW: Program crashes on exit (but only after moving characters)
« Reply #1 on: October 20, 2013, 01:24:32 pm »
With a chance of 99.9999% it's an issue in your code or setup thus without providing a minimal and complete example, we can't help you further. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Mlikaon

  • Newbie
  • *
  • Posts: 17
    • View Profile
Re: Program crashes on exit (but only after moving characters)
« Reply #2 on: October 20, 2013, 02:04:53 pm »
Well, i'll try to put some lines to sum it up.

if(event.type == Event::KeyPressed)
{
        if(Keyboard::isKeyPressed(Keyboard::Escape))
        {
                window.close();
        }
        else if(//blabla
        {
                entity[i]->path.clear();
                entity[i]->collision_sprite_large.setPosition(entity[i]->getPosition());
       
                search(//Some parameters including entity[i]);
                //This function fill the path of the entity, which is a deque type.
        }
}

//Back in the game loop

for(int i=0; i<NB_CHAR; i++)
        if(entity[i]->in_movement)
                move(*entity, entity[i], i, buildings);

//This function move the entity->sprite, step by step, following the path


window.clear();
window.draw(entity->sprite)
etc.
 

My entity class :

class Character : public Transformable
{
public :

        bool in_movement;
        Vector2i aim_position;
        Texture char_texture, collision_texture, collision_texture_large;
        Sprite char_sprite, collision_sprite, collision_sprite_large;
        Vector2f temp_aim_position;
        deque<Vector2f> path;

        void Character()
        {
                char_texture.loadFromFile("character.png");
                char_sprite.setTexture(char_texture);
                CreateTextureAndBitmask(collision_texture, "collision_circle.png");
                collision_sprite.setTexture(collision_texture);
                //Etc.
        }
}
 

I hope it's enough.
There's a lot of lines in my program, so ask me for other classes/functions if you suspect them to cause the crash.

The Hatchet

  • Full Member
  • ***
  • Posts: 135
    • View Profile
    • Email
Re: Program crashes on exit (but only after moving characters)
« Reply #3 on: October 20, 2013, 03:46:14 pm »
Please post a MINIMAL and COMPLETE code example that exhibits the problem if you can.  From the code you posted I don't even see how that compiled.

else if(//blabla
    {
        entity[i]->path.clear();
        entity[i]->collision_sprite_large.setPosition(entity[i]->getPosition());
   
        search(//Some parameters including entity[i]);
        //This function fill the path of the entity, which is a deque type.
    }
 
this shouldn't even compile since 'search()' isn't even properly closed.  you don't have a delcaration to 'i' for accessing entity[ i ] so if you program thread ever makes it into the 'else if()' part it could be trying to access entity[-3273537] for all you know, which could be your 'Access Violation' error.

window.draw(entity->sprite)
 
is both missing a semicolon and you are trying to draw the array itself, not any entity in it.  Should be window.draw(entity[ i ]->sprite); inside of a for loop at least.

Please clean up the code, cut it down so it runs from a single main.cpp that can be posted/copy/pasted so that we can try and find out your problem.  Most of the time by cutting code down to work in a minimal way you will find the error yourself
« Last Edit: October 20, 2013, 04:21:27 pm by The Hatchet »

Mlikaon

  • Newbie
  • *
  • Posts: 17
    • View Profile
Re: Program crashes on exit (but only after moving characters)
« Reply #4 on: October 20, 2013, 04:15:58 pm »
I don't have any errors while compiling, i wrote an example code to "sum up" what my program does, it is not the real code, which is thousands lines long.

Sorry for the ambiguities about that!

So i don't have any errors, only an "Access Violation" when i try to call "window.close()".
My program is running well, i can even call the "window.close()" function properly if i don't move any characters.
But if i do move a character, then the window.close() call make it crash.

Have you ever had this error before?
Do i have to clean some ressources before closing the window, like iterators, arrays or something else?

I do have a working program, the only problem occurs on "window.close()" (i marked it with a breakpoint and the instruction fails).
« Last Edit: October 20, 2013, 04:29:23 pm by Mlikaon »

The Hatchet

  • Full Member
  • ***
  • Posts: 135
    • View Profile
    • Email
Re: Program crashes on exit (but only after moving characters)
« Reply #5 on: October 20, 2013, 04:28:54 pm »
Access Violation means that a chunk of memory for something is trying to be accessed but that object is no longer in memory, or at that location or is currently being accessed by another thread/process.

Since this only happens after you move a sprite around then the error should be somewhere in your move code.  Look at what you are creating or copying around that may lead to problems when the exiting program tries to clean up all its entities. 
« Last Edit: October 20, 2013, 04:42:22 pm by The Hatchet »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: Program crashes on exit (but only after moving characters)
« Reply #6 on: October 20, 2013, 04:30:02 pm »
As said before, it's a bug somewhere in your code. We can't just work with some pseudo code and wishful thinking to figure out what's wrong.
At best you start your debugger yourself and hunt down the bug, as every other programmer is doing, or you create a SSCCE, so we can actually help you.

And if you're wondering whether it's a "common" issue, then the answer is no. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Mlikaon

  • Newbie
  • *
  • Posts: 17
    • View Profile
Re: Program crashes on exit (but only after moving characters)
« Reply #7 on: October 20, 2013, 05:17:38 pm »
Thank you for your answers,

I'd like to find the bug myself, but this is quite hard for me.

I tested much things and already found something anyway, but i don't understand why it happens :

Here are my two main classes, character and zombie (zombie extending the first one) :

class Character : public Transformable
{
public :

        int speed;
        int width;
        bool stuck;
        e_direction direction;
        bool deplacement;
        bool selected;
        bool hovered;
        Vector2i aim_position;
        Texture char_texture, collision_texture, collision_texture_large;
        Sprite char_sprite, collision_sprite, collision_sprite_large;
        CircleShape circle,circle_hover;
        bool draw_circle;
        int move_delay;
        Vector2f temp_aim_position;
        deque<Vector2f> path;

        Character()
        {
                setPosition(Vector2f(rand()%300+400, rand()%300+300));
                //setPosition(Vector2f(30,620));
                char_texture.loadFromFile("character.png");
                char_sprite.setTexture(char_texture);
                char_sprite.setOrigin(CHARWIDTH,CHARHEIGHT);
                char_sprite.setPosition(getPosition());
                circle.setPosition(getPosition());
                circle.setOrigin(CHARWIDTH, CHARHEIGHT);
                circle.setFillColor(Color(0,255,255,0));
                circle.setRadius(15);
                circle.setOutlineColor(Color(0,255,24,255));
                circle.setOutlineThickness(3);
                circle_hover=circle;
                circle_hover.setOutlineColor(Color(200,255,24,255));
                circle_hover.setOrigin(CHARWIDTH, CHARHEIGHT);
                CreateTextureAndBitmask(collision_texture, "collision_circle.png");
                collision_sprite.setTexture(collision_texture);
                collision_sprite.setOrigin(10, 10);
                collision_sprite.setPosition(getPosition());
                CreateTextureAndBitmask(collision_texture_large, "collision_circle_large.png");
                collision_sprite_large.setTexture(collision_texture_large);
                collision_sprite_large.setOrigin(CHARWIDTH, CHARHEIGHT);
                collision_sprite_large.setPosition(getPosition());
                speed=1;
                deplacement=false;
                selected=false;
                draw_circle=false;
                hovered=false;
                direction=Droite;
                move_delay=0;
                stuck=false;
        }
};

class Zombie : public Character
{
public:

        vector<int> cibles;
        bool chasing;
        float range;
        int chasing_timer;

        Zombie()
        {
                Zombie::Character();
                char_texture.loadFromFile("ennemy.png");
                char_sprite.setTexture(char_texture);
                range=100;
                setPosition(Vector2f(rand()%300+1000, rand()%300+300));
                //setPosition(Vector2f(700,420));
                char_sprite.setPosition(getPosition());
                collision_sprite.setPosition(getPosition());
                chasing=false;
                chasing_timer=0;
        }
};
 

My program is crashing on exit if i use the searching function for a character type (and works properly if i use it for a zombie type, which extends character:

Here is the prototype of the function, and its call:

//Prototype
void search(node*** matrix, deque<node>* n, deque<node>* m, Sprite collision_map, Sprite collision_sprite_large, Vector2f position, Vector2f aim,
Character* entite, int index, Character* tab_entite, vector<Building*>* batiments, grid_node** grid_bg)

//Call for character type (make the program crashing at exit)
search(matrix, tree, tree_to_sort, collision_map, entite[i]->collision_sprite_large, entite[i]->getPosition(),
Vector2f(entite[i]->aim_position.x, entite[i]->aim_position.y), entite[i], i, *entite, batiments, grid_bg);

//Call for zombie type (make the program exiting properly)
search(matrix, tree, tree_to_sort, collision_map, zombies[i]->collision_sprite_large, zombies[i]->getPosition(),
Vector2f(zombies[i]->aim_position.x, zombies[i]->aim_position.y), zombies[i], i, *zombies, batiments, grid_bg);

 

I really don't get it :D
« Last Edit: October 20, 2013, 05:19:10 pm by Mlikaon »

The Hatchet

  • Full Member
  • ***
  • Posts: 135
    • View Profile
    • Email
Re: Program crashes on exit (but only after moving characters)
« Reply #8 on: October 20, 2013, 05:27:25 pm »
what does the 'search()' function do?  Cuz never in my day have I ever seen a function need to take a dozen arguments.  Like holy cow that function just looks way too busy. 

Mlikaon

  • Newbie
  • *
  • Posts: 17
    • View Profile
Re: Program crashes on exit (but only after moving characters)
« Reply #9 on: October 20, 2013, 05:37:51 pm »
It finds a proper way to get to an aim from a starting point.

It creates a node matrix around the character, and tests each 8 directions, first from the character, and then from the child nodes that have been tested (it's a PathFinder).

I test the collision on each tested node, so i need to put the sprites as arguments, and the array containing the characters not to collide with them neither.

But the strange point is : why does it work for the child class, and not for the parent class?
Well, when i say it doesn't work, i mean it works, but it make a crash occuring at exit.
« Last Edit: October 20, 2013, 05:41:50 pm by Mlikaon »

The Hatchet

  • Full Member
  • ***
  • Posts: 135
    • View Profile
    • Email
Re: Program crashes on exit (but only after moving characters)
« Reply #10 on: October 20, 2013, 06:23:25 pm »
if your 'search() function makes use of:
vector<int> cibles;
    bool chasing;
    float range;
    int chasing_timer;
 
at all it could be why, since the parent class doesn't have those.  Or since you are passing a pointer to the entity arrays of each maybe some value is getting changed that should be changed.  hard to say without knowing what the function is doing in terms of code.

Mlikaon

  • Newbie
  • *
  • Posts: 17
    • View Profile
Re: Program crashes on exit (but only after moving characters)
« Reply #11 on: October 20, 2013, 06:43:42 pm »
It only uses attributes from the Character class.

It fills the "deque<Vector2f> path" from Character.
entite->path.push_back(node);

It moves the "Sprite collision_sprite_large" from Character.
collision_sprite_large.setPosition(entite->getPosition());

The Hatchet

  • Full Member
  • ***
  • Posts: 135
    • View Profile
    • Email
Re: Program crashes on exit (but only after moving characters)
« Reply #12 on: October 20, 2013, 07:08:28 pm »
You should post your search function since so far that seems to be where the error is popping up.

Mlikaon

  • Newbie
  • *
  • Posts: 17
    • View Profile
Re: Program crashes on exit (but only after moving characters)
« Reply #13 on: October 20, 2013, 07:19:01 pm »
Well it seems that i have a bunch of errors that i hadn't notice yet, due to my allocations.
I have to clear my code before coming back to you.
Thank you anyway!

 

anything