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

Author Topic: [SFML 2] Crashing at random time  (Read 4548 times)

0 Members and 1 Guest are viewing this topic.

Metapyziks

  • Newbie
  • *
  • Posts: 33
    • View Profile
[SFML 2] Crashing at random time
« on: February 09, 2010, 07:32:34 pm »
I have a load of small sprites (16*16, >64 of them) drawn onto my window each frame. After about one or two minutes, the app crashes with an access violation error:


The variable I use to work out how much the sprites should move (I use the time since they were last drawn) looks like this in the debugger:
Code: [Select]
4.316e-039#DEN

Could this be part of the problem?

Edit: It crashes on a line where I pass the variable mentioned to a function.

panithadrum

  • Sr. Member
  • ****
  • Posts: 304
    • View Profile
    • Skyrpex@Github
    • Email
[SFML 2] Crashing at random time
« Reply #1 on: February 09, 2010, 08:01:44 pm »
Hmm My little experience about dll's tells me that if I update SFML from SVN and don't compile the dll's (and put it to my project) it will crash at any moment, so try to compile the dlls and make sure your project loads them.

Metapyziks

  • Newbie
  • *
  • Posts: 33
    • View Profile
[SFML 2] Crashing at random time
« Reply #2 on: February 09, 2010, 08:08:16 pm »
Quote from: "panithadrum"
Hmm My little experience about dll's tells me that if I update SFML from SVN and don't compile the dll's (and put it to my project) it will crash at any moment, so try to compile the dlls and make sure your project loads them.


Thanks, that sounds likely since I haven't updated the dlls in a while.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
[SFML 2] Crashing at random time
« Reply #3 on: February 09, 2010, 08:12:08 pm »
If I download a new SVN version, I will always recompile the whole SFML project solution and the dependent projects. This saves me from tedious errors. ;)
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Metapyziks

  • Newbie
  • *
  • Posts: 33
    • View Profile
[SFML 2] Crashing at random time
« Reply #4 on: February 09, 2010, 08:32:17 pm »
Quote from: "Nexus"
If I download a new SVN version, I will always recompile the whole SFML project solution and the dependent projects. This saves me from tedious errors. ;)

At least I know now :D

But new problem: I have a vector containing the entities which have a sprite each, which is constantly being added to and entities are removed every so often too.

The problem is that the images set to the sprite of each entity seems to stick based on position in the vector, because when a new entity is removed from the vector some of the entities change their sprite.

It's hard to describe, so I'll upload an example:
http://www.robertandsherman.co.uk/downloads/BugThingExample.zip

This is just a guess at what is causing them to change image. They should all stay the same after they are created.

I can fix this by using Sprite.SetImage() every time the sprite is drawn, but I doubt that is the best method.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
[SFML 2] Crashing at random time
« Reply #5 on: February 09, 2010, 09:38:17 pm »
Quote from: "Metapyziks"
But new problem: I have a vector containing the entities which have a sprite each, which is constantly being added to and entities are removed every so often too.
Can you post the definition of the entity class and the code where you insert/remove elements into/from the container?

Maybe your class doesn't support full value semantics, which is required for STL containers. Could you show the implementation of the entity class's copy constructor, copy assignment operator and destructor, too, if you have defined these functions yourself?
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Metapyziks

  • Newbie
  • *
  • Posts: 33
    • View Profile
[SFML 2] Crashing at random time
« Reply #6 on: February 09, 2010, 10:33:14 pm »
Quote from: "Nexus"
Quote from: "Metapyziks"
But new problem: I have a vector containing the entities which have a sprite each, which is constantly being added to and entities are removed every so often too.
Can you post the definition of the entity class and the code where you insert/remove elements into/from the container?

Maybe your class doesn't support full value semantics, which is required for STL containers. Could you show the implementation of the entity class's copy constructor, copy assignment operator and destructor, too, if you have defined these functions yourself?


*Goes and writes a copy constructor / assignment operator / destructor*

 :roll: I am quite new to C++ anyway. I'll tell you if adding them fixes it.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
[SFML 2] Crashing at random time
« Reply #7 on: February 09, 2010, 10:45:48 pm »
Quote from: "Metapyziks"
*Goes and writes a copy constructor / assignment operator / destructor*
You only need them if you want specific copy semantics, for example because you allocate members dynamically. However, the better way is mostly to use automatic variables where possible. For those, the compiler generated Big Three (copy-ctor, op= and dtor) work just fine.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Metapyziks

  • Newbie
  • *
  • Posts: 33
    • View Profile
[SFML 2] Crashing at random time
« Reply #8 on: February 09, 2010, 10:56:37 pm »
I made a copy constructor (my first), and it works! I think I understand why, because it didn't allocate memory for the sf::Image in the entity.

I think I know when I'll need a copy constructor and destructor now, when I have members in the function that can take up varying amounts of memory (like an array).

Thanks again, I have learnt something new :D

Edit: I still get memory access violations after about 2 minutes :( I'll post my constructor / copy constructor / where I add to the vector like you said anyway.

Code: [Select]
// Constructor
Bug::Bug( unsigned int s, unsigned int b, unsigned int g, sf::Vector2f p )
{
if( s < 1 )
s = 1;
if( s > 180 )
s = 180;

if( b < 1 )
b = 1;
if( b > 180 )
b = 180;

if( g < 1 )
g = 1;
if( g > 180 )
g = 180;

info.shell = s;
info.body  = b;
info.gut   = g;

health = pref_start_health;
food = 0;

pos = p;
vel = sf::Vector2f( sf::Randomizer::Random( -( int )pref_speed_max, pref_speed_max ) * 1.f, sf::Randomizer::Random( -( int )pref_speed_max, pref_speed_max ) * 1.f );

sf::Image tm_image;
my_image.Create( 16, 16, sf::Color( 0, 0, 0, 0 ));

tm_image.Create( 16, 16, sf::Color( 0, 0, 0, 0 ));

tm_image.Copy( img_shell, 0, 0, sf::IntRect( 0, 0, 16, 16 ),  true );
ShadeImage   ( tm_image,  GetPartShade( info.shell ));
my_image.Copy( tm_image,  0, 0, sf::IntRect( 0, 0, 16, 16 ), true );

tm_image.Create( 16, 16, sf::Color( 0, 0, 0, 0 ));

tm_image.Copy( img_body,  0, 0, sf::IntRect( 0, 0, 16, 16 ), true );
ShadeImage   ( tm_image,  GetPartShade( info.body  ));
my_image.Copy( tm_image,  0, 0, sf::IntRect( 0, 0, 16, 16 ), true );

tm_image.Create( 16, 16, sf::Color( 0, 0, 0, 0 ));

tm_image.Copy( img_gut,   0, 0, sf::IntRect( 0, 0, 16, 16 ), true );
ShadeImage   ( tm_image,  GetPartShade( info.gut   ));
my_image.Copy( tm_image,  0, 0, sf::IntRect( 0, 0, 16, 16 ), true );

my_sprite.SetImage( my_image );
my_sprite.SetPosition( pos.x - 8, pos.y - 8 );

alive = true;
age = 0.f;

last_acid = 0.f;
id = next_id ++;
}


Code: [Select]
// Copy Constructor
Bug::Bug( const Bug &bug )
{
info.shell = bug.info.shell;
info.body  = bug.info.body;
info.gut   = bug.info.gut;

health = bug.health;
food = bug.food;

pos = bug.pos;
vel = bug.vel;

my_image = bug.my_image;
my_sprite.SetImage( my_image );

my_sprite.SetPosition( pos.x - 8, pos.y - 8 );

alive = bug.alive;
age = bug.age;

last_acid = bug.last_acid;
id = bug.id;
}


Code: [Select]
// Adding to vector (This is a static function)
void Bug::AddBug( unsigned int s, unsigned int b, unsigned int g, sf::Vector2f p )
{
if( !images_loaded )
{
images_loaded = true;
img_shell.LoadFromFile( "shell.png" );
img_body.LoadFromFile( "body.png" );
img_gut.LoadFromFile( "gut.png" );

img_acid.LoadFromFile( "acid.png" );
img_food.LoadFromFile( "food.png" );
}
bug_list.push_back( Bug( s, b, g, p )); // bug_list is the vector
}


Code: [Select]
// Removing from vector
std::vector<Bug>::iterator b_i = Bug::bug_list.begin();
while( b_i != Bug::bug_list.end())
{
if( !b_i->Think( inc, app_window ))
b_i = Bug::bug_list.erase( b_i );
else
++ b_i;
}


"Bug::Think" returns true if the bug entity is still active, or false if it should be removed.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
[SFML 2] Crashing at random time
« Reply #9 on: February 09, 2010, 11:50:05 pm »
Hm, so far I don't see any error... Can I see the class definition with the members?

If you don't allocate dynamic memory, do rather not implement The Big Three on your own. As said, the compiler-generated ones should be enough. Your problem is probably somewhere else. Have you ever used a debugger? With a debugger, you can track the code, watch variables and place breakpoints. Like this, you are able to observe the copy and compare the variables of the origin and the newly created instance.

A further tip: To initialize variables in a constructor (also copy constructor), use the initializer list:
Code: [Select]
MyClass::MyClass(int arg1, double arg2, bool arg3) : member1(arg1), member2(arg2), member3(arg3)
{
   // in the constructor body, the listed members are already initialized
}
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development: