Hey all, whenever I run my game, the application crashes immediately without entering main(). On debugging, I get an error:
Unhandled exception at 0x55E76001 (sfml-graphics-d-2.dll) in DungeonRaider.exe:
0xC0000005: Access violation reading location 0x00000000.
The offending code is in my Animation class's assignment operator.
The Animation class consists of a dynamic array of sf::Sprite objects. When the assignment operator executes, the sf::Sprite objects from the source Animation object are copied by value to the new Animation object, like such:
Animation& operator=(const Animation& srcAnimation) const
{
//sprite_ is the dynamic array of sf::Sprite objects
/*...code to delete the elements of the old array and make space for the new one...*/
sprites_ = new sf::Sprite[/*...the new number of sprites...*/];
for(int i=0;i</*...the new number of sprites...*/;i++)
{
sprites_[i].setTexture(*(srcAnimation.sprites_[i].getTexture())); //<----This is the line that debugging stops at
}
}
I've checked that I'm linking debug libraries for debug mode.
This is the call stack:
sfml-graphics-d-2.dll!55e76001() Unknown
[Frames below may be incorrect and/or missing, no symbols loaded for sfml-graphics-d-2.dll]
sfml-graphics-d-2.dll!55e8155d() Unknown
> DungeonRaider.exe!Animation::operator=(const Animation & srcAnimation) Line 110 C++
DungeonRaider.exe!AnimState::AnimState() Line 24 C++
DungeonRaider.exe!AnimStateController::AnimStateController() Line 27 C++
DungeonRaider.exe!`dynamic initializer for 'AnimStateComponent::s_defaultController''() Line 55 C++
[External Code]
Any ideas? I'm not quite sure where to start... :-\
Also, any suggestions for my Animation class? I feel like I should be using std::vector instead of a dynamic array of sprites.
EDIT: Maybe this should've been in the graphics section. Can someone move this?
EDIT 2: Arrghh! It's a null pointer. But why is it crashing before main() starts?
are you sure your Animation& srcAnimation
has enough sprites?
I mean, you recreate your
sprites_ = new sf::Sprite[/*...the new number of sprites...*/];
and then you might fall into trap if your
srcAnimation.sprites_
has less amount of items.
Think this way:
you have 10 sprites initially, then you delete them and reallocate, say, 15 sprites.
now you loop through them (15 times), asking your srcAnimation for textures for each of them, but srcAnimation still contains 10 sprites (and textures for them).
As a result if you ask srcAnimation sprite having index 11 your program crashes.
I think here:
sprites_ = new sf::Sprite[/*...the new number of sprites...*/];
you should allocate an array having the length equal to your srcAnimation's sprites_'s one.