Scopedpointer lost in scope


#1

Dear All

I'm getting confused with scopedpointer. I have the following code:

MainContent.h

class MainContent  : public Component
{
public:
    //==============================================================================
    MainContent ();
    ~MainContent();
    //==============================================================================

    //[UserMethods]     -- You can add your own custom methods in this section.

    void enableDisableUIFromConnectedDevice();

    //[/UserMethods]

    void paint (Graphics& g);
    void resized();

private:
    //[UserVariables]   -- You can add your own custom variables in this section.
    //[/UserVariables]
    //==============================================================================

    ScopedPointer<TabbedComponent> tabbedComponent;

    //==============================================================================

    JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MainContent)
};

As shown above, tabbedComponent was declared as a scopedpointer by introjucer.
I'm trying to get the content of the tab at index 0 from one of my functions which is declared in the scope.

MainContent.cpp

#include "MainContent.h"
​#include "MidiController.h"

//[MiscUserDefs] You can add your own user definitions and misc code here...

//[/MiscUserDefs]

//==============================================================================

MainContent::MainContent ()
    : Component ("mainContent")
{

   addAndMakeVisible (tabbedComponent = new TabbedComponent (TabbedButtonBar::TabsAtTop));
   tabbedComponent->setTabBarDepth (29);
   tabbedComponent->addTab ("Xkey", Colours::lightgrey, new XkeyClass(), true);
   tabbedComponent->addTab ("Velocity Curves", Colours::lightgrey, new VelocityCurvesClass(), true);   
   tabbedComponent->addTab ("Sensitivity & Gain", Colours::lightgrey, new SensitivityClass(), true);
   tabbedComponent->setCurrentTabIndex (0);

    //[UserPreSize]
    //[/UserPreSize]

    setSize (800, 600);

    //[Constructor] You can add your own custom stuff here..

    MidiController *midiController = MidiController::getInstance();
    midiController->initMidiController();

    //[/Constructor]
}

MainContent::~MainContent()
{
   MidiController::deleteInstance();
   tabbedComponent = nullptr;
}

void MainContent::enableDisableUIFromConnectedDevice()
{

    // Grab a ref to MIDIcontroller
    MidiController *midiController = MidiController::getInstance();

    // get a ref to the first tab content -> PROBLEM
    Component* comp = tabbedComponent->getTabContentComponent(TAB_MYXEY);
...
}

The problem is above, tabbedComponent is no longer set (it is 0x000000) when entering into this function, which is in the scope.
So the getTabContentComponent crashes my app.

I do not understand why the tabbedComponent scopedpointer is lost here since my function is declared in the scope ?
What am I missing ?

Thanks for any help or tip.

J


#2

Is that all your code?  The mostly likely reason why your pointer is getting nulled somewhere is if you have constructs like:

 

ScopedPointer<TabbedComponent> t = tabbedComponent;
​​

// or
ScopedPointer<TabbedComponent> getTabbedComponent()

{

  return tabbedComponent;
}

ScopedPointer<TabbedComponent> t(getTabbedComponent());

 

 

somewhere else., I.E. if you r

 

 


#3

Thanks for your answer.

The pointer was constructed using introjucer.
I do not want to loose introjucer so I did not modify anything around this pointer.

In the .h, the pointer is declared in the private section as :

ScopedPointer<TabbedComponent> tabbedComponent;

and it is allocated in the cpp constructor as follow :


addAndMakeVisible (tabbedComponent = new TabbedComponent (TabbedButtonBar::TabsAtTop));

Anything I'm missing ?

Thanks


#4

What happens if the 4th parameter to addTab is false?

What’s the value of TAB_MYXEY ?

Is tabbedComponent nullptr or the return from getTabContentComponent() nullptr?

Rail


#5

The 4th param of addTab is : deleteComponentWhenNotNeeded (I do not think it is the cause of my issue)

The value of TAB_MYXEY is 0, I want to get the content of the first tab, I assume it starts at index 0

tabbedComponent is nullptr

When the app crashes, xcode shows juce_ScopedPointer.h class, and highlights the following line:

inline ObjectType* operator->() const noexcept                                  { return object; } --> EXC_BAD_ACCESS

You can see the screenshot, I set a breakpoint to the line that crashes, and you can see tabbedComponent is 0x00000

Again, thanks for your time.

 


#6

this is null

Rail


#7

Ohh ! thanks !

The caller was not correctly implemented !
Again, thanks