Bug on pointer vector::push_back

Hello everyone,
Today I get a really weird error from this code. It compiles perfectly, but crashes at the push_back line :(
1
2
3
4
5
6
7
8
9
10
11
class MenuNav
{
public:
	MenuNav();
	~MenuNav();

	void addButton(MenuButton* button);

private:
	std::vector<MenuButton*> m_buttons;
};

That was the header and here is the implementation
1
2
3
4
5
6
7
void MenuNav::addButton(MenuButton* button)
{
	//std::cerr << "but=" << button << "  " << !!button; //works when I test
	m_buttons.push_back(button); //crash here
	//std::cerr << "Crashes b4" ; //doesn't display
}


When I do this:
 
m_nav->addButton(new MenuButton(the constructor parameters));//bug on this line boys 

The program just crashes when I ncomment it, for no obvious reason :(
If someone know what's going on the let me know :)
http://www.cplusplus.com/forum/general/112111/

¿why are you storing pointers instead of objects?
If you are planning to use polymorphism in the `MenuButton' then consider using an smart pointer to avoid leaking resources
i see that m_nav is a pointer. I suspect that it does not point to the valid MenuNav object. In that case you will ge symptoms you described.
The pointer m_nav has to be properly defined by defining the object with a valid initialized value and then call the method addButton() function.
Topic archived. No new replies allowed.