AddAndMakeVisible and pointers

Discussion and support for general JUCE issues

AddAndMakeVisible and pointers

Postby StrangeMan » Fri May 11, 2012 3:50 pm

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:
Code: Select all
std::shared_ptr<Component> Addon::getGuiPage();


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:
Code: Select all
addAndMakeVisible(someAddon->getGuiPage().get());


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
StrangeMan
JUCE Obsessive
 
Posts: 55
Joined: Sat Apr 21, 2012 12:21 pm

Re: AddAndMakeVisible and pointers

Postby jules » Fri May 11, 2012 4:13 pm

Well yes.. Your shared_ptr will immediately delete the component when it goes out of scope!
User avatar
jules
Fearless Leader
 
Posts: 17352
Joined: Mon Sep 06, 2004 9:03 am
Location: London, UK

Re: AddAndMakeVisible and pointers

Postby StrangeMan » Fri May 11, 2012 4:27 pm

ah, sorry, forgot to tell: The method stores a copy of the pointer internally like this
Code: Select all
class Addon
{
...
private:
   std::shared_ptr<Component> m_gui_page;
};

std::shared_ptr<Component> addon::getGuiPage()
{
   if (m_gui_page == 0)
   {
      m_gui_page = std::shared_ptr<Component>(new Gui());
   }
   return m_gui_page;
}
StrangeMan
JUCE Obsessive
 
Posts: 55
Joined: Sat Apr 21, 2012 12:21 pm

Re: AddAndMakeVisible and pointers

Postby jules » Fri May 11, 2012 4:36 pm

Ah, well you're probably just not setting its bounds or something then.
User avatar
jules
Fearless Leader
 
Posts: 17352
Joined: Mon Sep 06, 2004 9:03 am
Location: London, UK

Re: AddAndMakeVisible and pointers

Postby StrangeMan » Fri May 11, 2012 4:44 pm

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...
StrangeMan
JUCE Obsessive
 
Posts: 55
Joined: Sat Apr 21, 2012 12:21 pm

Re: AddAndMakeVisible and pointers

Postby X-Ryl669 » Fri May 18, 2012 2:46 pm

StrangeMan wrote:ah, sorry, forgot to tell: The method stores a copy of the pointer internally like this

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.
X-Ryl669
X-Ryl669
JUCE UberWeenie
 
Posts: 1125
Joined: Sun Apr 24, 2005 5:30 pm


Return to General JUCE discussion

Who is online

Users browsing this forum: Google Feedfetcher and 2 guests