xxMrPHDxx
xxMrPHDxx

Reputation: 667

Error when trying to push sf::Texture* into std::vector

I'm working on a C++ project that uses SFML and lua The thing is when I tried to push the texture pointer that I loaded it crashes.

Here's my code

    sf::Texture* tex = new sf::Texture;
    if(!tex->loadFromFile(lua_tostring(L, -1))) std::cout << "Failed to load texture!" << std::endl;
    std::cout << "Got here!" << std::endl;
    try{
        p.tex_auras.push_back(tex);
    }catch(std::invalid_argument& e){
        std::cout << "Error: " << e.what() << std::endl;
    }

If I comment out the push_back thingy, everything was fine. Also, the code spits out Got here! once and if tried checking the tex->loadFromFile() to see if it fails, it didn't. When I debug the code it seems to happen in the std::vector::push_back

        void push_back(const value_type &__x) {
            if (this->_M_impl._M_finish != this->_M_impl._M_end_of_storage) {          <<== HERE ==
                _Alloc_traits::construct(this->_M_impl, this->_M_impl._M_finish,
                        __x);
                ++this->_M_impl._M_finish;
            } else
                _M_realloc_insert(end(), __x);
        }

Edit

typedef std::vector<sf::Texture*> Textures;
// The textures was initialized in a class as such
class Player {
public:
     // Just like that
     Textures tex_auras;
}

I don't know what's going on

Upvotes: 0

Views: 155

Answers (2)

xxMrPHDxx
xxMrPHDxx

Reputation: 667

Apparently the player is NULL in the first place after I loaded the userdata from lua. I'm on the wrong stack index

Upvotes: 0

Noah K
Noah K

Reputation: 87

I'd rather post this as a comment, but I don't have enough rep.

While this won't completely solve the error, you might be able to avoid the crash if you changed the catch block to }catch(std::exception& e) { instead of }catch(std::invalid_argument& e){. The exception being thrown might not be an invalid_argument exception, and catching all exceptions works better as a catch-all. Assuming this works, you could also end up with a more informative error message and thus a better idea of what is going wrong.

Upvotes: 1

Related Questions