# Scopedpointer lost in scope

**URL:** <https://forum.juce.com/t/scopedpointer-lost-in-scope/11420>\
**Category:** General JUCE discussion\
**Created:** [September 6, 2013, 3:33pm UTC](https://forum.juce.com/t/scopedpointer-lost-in-scope/11420 "2013-09-06T15:33:43Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![joeman](https://avatars.discourse-cdn.com/v4/letter/j/6f9a4e/32.png) [@joeman](https://forum.juce.com/u/joeman)\
**Post date:** [September 6, 2013, 3:33pm UTC](https://forum.juce.com/t/scopedpointer-lost-in-scope/11420/1 "2013-09-06T15:33:43Z")

</div>

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,&nbsp;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&nbsp;getTabContentComponent crashes my app.  
  
I do not understand why the&nbsp;tabbedComponent scopedpointer is lost here since my function is declared in the scope ?  
What am I missing ?  
  
Thanks for any help or tip.

J

---

<div class="post-metadata">

**Author:** ![valley](https://avatars.discourse-cdn.com/v4/letter/v/67e7ee/32.png) [@valley](https://forum.juce.com/u/valley)\
**Post date:** [September 6, 2013, 3:47pm UTC](https://forum.juce.com/t/scopedpointer-lost-in-scope/11420/2 "2013-09-06T15:47:36Z")

</div>

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

&nbsp;

```
ScopedPointer<TabbedComponent> t = tabbedComponent;
​​

// or
ScopedPointer<TabbedComponent> getTabbedComponent()

{

  return tabbedComponent;
}

ScopedPointer<TabbedComponent> t(getTabbedComponent());
```

&nbsp;

&nbsp;

somewhere else., I.E. if you r

&nbsp;

&nbsp;

---

<div class="post-metadata">

**Author:** ![joeman](https://avatars.discourse-cdn.com/v4/letter/j/6f9a4e/32.png) [@joeman](https://forum.juce.com/u/joeman)\
**Post date:** [September 9, 2013, 7:10am UTC](https://forum.juce.com/t/scopedpointer-lost-in-scope/11420/3 "2013-09-09T07:10:34Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![railjonrogut](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/railjonrogut/32/505_2.png) [@railjonrogut](https://forum.juce.com/u/railjonrogut)\
**Post date:** [September 9, 2013, 8:53am UTC](https://forum.juce.com/t/scopedpointer-lost-in-scope/11420/4 "2013-09-09T08:53:00Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![joeman](https://avatars.discourse-cdn.com/v4/letter/j/6f9a4e/32.png) [@joeman](https://forum.juce.com/u/joeman)\
**Post date:** [September 9, 2013, 9:24am UTC](https://forum.juce.com/t/scopedpointer-lost-in-scope/11420/5 "2013-09-09T09:24:38Z")

</div>

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

The value of&nbsp;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&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { 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.

&nbsp;

---

<div class="post-metadata">

**Author:** ![railjonrogut](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/railjonrogut/32/505_2.png) [@railjonrogut](https://forum.juce.com/u/railjonrogut)\
**Post date:** [September 9, 2013, 9:39am UTC](https://forum.juce.com/t/scopedpointer-lost-in-scope/11420/6 "2013-09-09T09:39:49Z")

</div>

this is null

Rail

---

<div class="post-metadata">

**Author:** ![joeman](https://avatars.discourse-cdn.com/v4/letter/j/6f9a4e/32.png) [@joeman](https://forum.juce.com/u/joeman)\
**Post date:** [September 9, 2013, 12:09pm UTC](https://forum.juce.com/t/scopedpointer-lost-in-scope/11420/7 "2013-09-09T12:09:30Z")

</div>

Ohh ! thanks !

The caller was not correctly implemented !  
Again, thanks
