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 1125 times)

0 Members and 1 Guest are viewing this topic.

makerimages

  • Newbie
  • *
  • Posts: 24
  • Yay!
    • View Profile
    • Email
https://bitbucket.org/makerimages/chtt3r

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:
 spriteCursor=*chat->getSpriteManager()->getSprite(0);

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

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10801
    • 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
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/