AddAndMakeVisible and pointers


#1

Hello!

I have the following problem: A Component is used to display a list of “addons” in a custom list box. When the user clicks an item in the list, I want to display some addon-specific options next to the list. The list holds references to these addons anyway so i thought, it might be a good idea to implement a member function for each addon like this:

So now, when the user clicks the list, i’d like to let the addon create it’s gui component so I can add it to my window using “addAndMakeVisible”. I call it like this:

However, nothing happens. I guess, it’s got to do with the whole shared_ptr stuff, but I have no idea, whats going on here. Please, can you help me?

Thanks a lot,
StrangeMan


#2

Well yes… Your shared_ptr will immediately delete the component when it goes out of scope!


#3

ah, sorry, forgot to tell: The method stores a copy of the pointer internally like this[code]
class Addon
{

private:
std::shared_ptr m_gui_page;
};

std::shared_ptr addon::getGuiPage()
{
if (m_gui_page == 0)
{
m_gui_page = std::shared_ptr(new Gui());
}
return m_gui_page;
}[/code]


#4

Ah, well you’re probably just not setting its bounds or something then.


#5

oh my god, that’s so awkward :oops: Sometimes you won’t find the problem due to it’s simplicity… Of course you’re right… Well thanks then…


#6

That’s why I usually don’t tell people to use shared_ptr…
You only need a member here, not a pointer. Use a reference and you’re done, you’ll avoid useless referenced counting, the mind-twisting weakptr issues and so on.