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

Author Topic: Having a Sprite manager. Trying drawing from it causes access violation  (Read 879 times)

0 Members and 1 Guest are viewing this topic.


  • Newbie
  • *
  • Posts: 24
  • Yay!
    • View Profile
    • Email

I draw in TextBox.cpp, it causes an access violation, the call stack looks like this
        sfml-graphics-d-2.dll!5492a253()        Unknown
        [Frames below may be incorrect and/or missing, no symbols loaded for sfml-graphics-d-2.dll]    
        sfml-graphics-d-2.dll!54932951()        Unknown
>       Chtt3r.exe!TextBox::TextBox(float x, float y, float w, float h, ChatObject * cO) Line 16        C++
        Chtt3r.exe!NotSignedInStage::NotSignedInStage(sf::RenderWindow * rW) Line 23    C++
        Chtt3r.exe!main() Line 16       C++
        Chtt3r.exe!__tmainCRTStartup() Line 536 C
        Chtt3r.exe!mainCRTStartup() Line 377    C
        kernel32.dll!75821174() Unknown
        ntdll.dll!778fb3f5()    Unknown
        ntdll.dll!778fb3c8()    Unknown

After I get the violation, VS leads me to    this line:

What may the cause be and how to fix?
Makerimages-It`s in the pixel


  • SFML Team
  • Hero Member
  • *****
  • Posts: 10191
    • View Profile
    • development blog
    • Email
Re: Having a Sprite manager. Trying drawing from it causes access violation
« Reply #1 on: January 03, 2014, 02:44:53 pm »
A sprite manager is not really good. Sprites are light weight objects and should be part of the object that uses it. I'd suggest to make the sprite a member variable of the TextBox.

For the current state, you always need to make sure that the object in the array exists. Instead of blindly accessing it "because I know it exists!!!1".
That's also the reason why it crashes. No sprites exist in that array and you try to access the element with index 0, which doesn't exist -> crash.

The reason why it doesn't exist is, because you've declared an empty default constructor for SpriteManager, which doesn't call the init part, thus never creating the sprites.
Remove the default constructor and init the SpriteManager via the other constructor by passing a TextureManager in ChatObject's initialization list.
Official FAQ: https://www.sfml-dev.org/faq.php
Nightly Builds: https://www.nightlybuilds.ch/
Dev Blog: https://dev.my-gate.net/
Thor: http://www.bromeon.ch/libraries/thor/