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

Author Topic: "Access violation" reading null (runtime error), main() does not execute  (Read 5499 times)

0 Members and 1 Guest are viewing this topic.

CoconutFred

  • Newbie
  • *
  • Posts: 1
    • View Profile
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?
« Last Edit: January 01, 2015, 11:09:49 pm by CoconutFred »

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: "Access violation" reading null (runtime error), main() does not execute
« Reply #1 on: January 01, 2015, 11:10:55 pm »
Crashes like this are usually an environment problem.  Reread the setup/configuration tutorials, try some small SFML and C++ examples to see if they crash as well, and if you can't figure it out then show us a complete and minimal example that crashes for you, with compiler/OS/build/version/etc details.

If it is indeed crashing "before main()" then that sounds like you have globals (which you shouldn't) or maybe something's wrong with the .dlls (ie an environment/config problem).  Again, not much we can do without more details.

Also, any suggestions for my Animation class? I feel like I should be using std::vector instead of a dynamic array of sprites.

You should always use the standard library containers instead of new/delete, unless you're doing something really low-level like implementing custom smart pointers or memory allocators.

As for the class itself, can't say much without knowing more, but taking an array of sprites is a very strange way to do animation.  The normal way is to have a single texture containing all the frames of the animation (aka, a "spritesheet") and use setTextureRect() to use the correct subregion of the texture.  Your "animation" would then be just a sprite and an array of IntRects.
« Last Edit: January 01, 2015, 11:12:27 pm by Ixrec »

Neil

  • Newbie
  • *
  • Posts: 27
    • View Profile
Re: "Access violation" reading null (runtime error), main() does not execute
« Reply #2 on: January 01, 2015, 11:27:07 pm »
Indeed if you use standard containers you will find many of your custom assignment operators and copy constructors disappear.

StormWingDelta

  • Sr. Member
  • ****
  • Posts: 365
    • View Profile
Re: "Access violation" reading null (runtime error), main() does not execute
« Reply #3 on: January 02, 2015, 03:05:21 am »
Not sure about the crashing before main starts thing but the only thing that comes to mind is if you aren't creating a global object correctly but without seeing the code there is no way to tell for certain.


Meanwhile another cause of access violations could be if you are going outside the array or list of sprites.  Like say your for loop counting one more than it should or starting one less than it should.


An Array is fine if the number of something isn't going to change, but if it is use something like vector since it'll give you less headaches for this.  Also keep in mind that if you don't have a default constructor it could cause other issues with arrays and other collections.  As some of my C++ classmates and myself found out rather fast.

Right now all we can do is guess at this point though.
I have many ideas but need the help of others to find way to make use of them.

grok

  • Jr. Member
  • **
  • Posts: 67
    • View Profile
    • Email
Re: "Access violation" reading null (runtime error), main() does not execute
« Reply #4 on: January 02, 2015, 06:21:02 pm »
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.
« Last Edit: January 02, 2015, 06:25:36 pm by grok »