# Stack Overflow! \>\_\<

**URL:** <https://forum.juce.com/t/stack-overflow--/8226>\
**Category:** The Projucer\
**Created:** [February 28, 2012, 6:17am UTC](https://forum.juce.com/t/stack-overflow--/8226 "2012-02-28T06:17:14Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![couch.jalapeno](https://avatars.discourse-cdn.com/v4/letter/c/90ced4/32.png) [@couch.jalapeno](https://forum.juce.com/u/couch.jalapeno)\
**Post date:** [February 28, 2012, 6:17am UTC](https://forum.juce.com/t/stack-overflow--/8226/1 "2012-02-28T06:17:14Z")

</div>

Hi pros, I’m new to OOP and JUCE.  
Anyone with the patience, I’ll dearly appreciate your help.

JuceDemo compiles & runs well  
& so do JuceAmalgated & the Jucer.

Introjucer builds me a blank template, theJucer builds me a simple Component (I’ve named this JucerComponent)

I try to call the Component built by Jucer using:

` mainWindow = new JucerComponent();
		mainWindow->addAndMakeVisible(mainWindow);`  
& in shutdown() & systemRequestedQuit() I put:

```auto

```

Builds without Warnings or Errors, but when running, the window doesn’t come on, & I get:

[quote]JUCE v1.54.27  
’testJuceGUI.exe’: Loaded ‘C:\Windows\System32\cryptbase.dll’  
‘testJuceGUI.exe’: Loaded 'C:\Windows\System32\uxtheme.dll’  
First-chance exception at 0x005fb197 in testJuceGUI.exe: 0xC00000FD: Stack overflow.  
Unhandled exception at 0x005fb197 in testJuceGUI.exe: 0xC00000FD: Stack overflow.[/quote]

My main.cpp is:

# [code]/\*

```
This file was auto-generated by the Jucer!

It contains the basic startup code for a Juce application.

```

==============================================================================  
\*/

#include “…/JuceLibraryCode/JuceHeader.h”  
#include “…/JuceLibraryCode/AppConfig.h”  
//#include “MainWindow.h”  
#include “…/…/…/juce\_amalgamated.h”  
#include “…/…/…/juce.h”  
#include “…/…/…/juce\_Config.h”  
#include “JucerComponent.h”

//==============================================================================  
class testJuceGUIApplication : public JUCEApplication  
{  
public:  
//==============================================================================  
testJuceGUIApplication()  
{  
}

```
~testJuceGUIApplication()
{
}

//==============================================================================
void initialise (const String& commandLine)
{
    // Do your application's initialisation code here..
    //mainWindow = new MainAppWindow();
	mainWindow = new JucerComponent();
	mainWindow->addAndMakeVisible(mainWindow);
}

void shutdown()
{
    // Do your application's shutdown code here..
	mainWindow->deleteAllChildren();
    mainWindow = 0;
}

//==============================================================================
void systemRequestedQuit()
{
	mainWindow->deleteAllChildren();
    quit();
}

//==============================================================================
const String getApplicationName()
{
    return "testJuceGUI";
}

const String getApplicationVersion()
{
    return ProjectInfo::versionString;
}

bool moreThanOneInstanceAllowed()
{
    return true;
}

void anotherInstanceStarted (const String& commandLine)
{
    
}

```

private:  
//ScopedPointer mainWindow;  
ScopedPointer mainWindow;  
};

//==============================================================================  
// This macro generates the main() routine that starts the app.  
START\_JUCE\_APPLICATION(testJuceGUIApplication)  
[/code]

---

<div class="post-metadata">

**Author:** ![couch.jalapeno](https://avatars.discourse-cdn.com/v4/letter/c/90ced4/32.png) [@couch.jalapeno](https://forum.juce.com/u/couch.jalapeno)\
**Post date:** [February 28, 2012, 6:28am UTC](https://forum.juce.com/t/stack-overflow--/8226/2 "2012-02-28T06:28:09Z")

</div>

Just to add it occurs in juce\_MessageManager.cpp at

[code]MessageManager\* MessageManager::getInstance()  
{  
if (instance == nullptr)  
{  
instance = new MessageManager();  
doPlatformSpecificInitialisation();  
}

```
return instance;

```

}[/code]

---

<div class="post-metadata">

**Author:** ![ckk](https://avatars.discourse-cdn.com/v4/letter/c/d26b3c/32.png) [@ckk](https://forum.juce.com/u/ckk)\
**Post date:** [February 28, 2012, 6:40am UTC](https://forum.juce.com/t/stack-overflow--/8226/3 "2012-02-28T06:40:10Z")

</div>

You have to add the mainWindow object to your window, not to itself as a childcomponent:

```auto
mainWindow = new JucerComponent();
addAndMakeVisible(mainWindow);
```

Also, instead of using deleteAllChildren better use Scopedpointers or normal objects.

Chris

---

<div class="post-metadata">

**Author:** ![couch.jalapeno](https://avatars.discourse-cdn.com/v4/letter/c/90ced4/32.png) [@couch.jalapeno](https://forum.juce.com/u/couch.jalapeno)\
**Post date:** [February 28, 2012, 7:21am UTC](https://forum.juce.com/t/stack-overflow--/8226/4 "2012-02-28T07:21:56Z")

</div>

> [@ckk](#):
>
> You have to add the mainWindow object to your window, not to itself as a childcomponent

Thanks Chris, for your atention on this.  
It must look too stupid for anyone else to want to bother.

I changed it to call the default window conjured by the introJucer,  
In which, I call the main juce component conjured by the Jucer like this:

MainWindow.cpp

# [code]/\*

```
This file was auto-generated by the Jucer!

It contains the basic outline for a simple desktop window.

```

==============================================================================  
\*/

#include “MainWindow.h”

```
ScopedPointer <JucerComponent> juceWindow;

```

//==============================================================================  
MainAppWindow::MainAppWindow()  
: DocumentWindow (JUCEApplication::getInstance()-\>getApplicationName(),  
Colours::lightgrey,  
DocumentWindow::allButtons)  
{  
centreWithSize (900, 900);  
setVisible (true);

```
juceWindow = new JucerComponent();
juceWindow->addAndMakeVisible(juceWindow);

```

}

MainAppWindow::~MainAppWindow()  
{  
juceWindow-\>deleteAllChildren();  
}

void MainAppWindow::closeButtonPressed()  
{  
JUCEApplication::getInstance()-\>systemRequestedQuit();  
}

[/code]

& again I get a Stack Overflow at the same location as before, which is:

juce\_win32\_Threads.cpp

`Thread::ThreadID Thread::getCurrentThreadId()
{
    return (ThreadID) (pointer_sized_int) GetCurrentThreadId();
}`

What am I doing wrong here?

---

<div class="post-metadata">

**Author:** ![ckk](https://avatars.discourse-cdn.com/v4/letter/c/d26b3c/32.png) [@ckk](https://forum.juce.com/u/ckk)\
**Post date:** [February 28, 2012, 8:57am UTC](https://forum.juce.com/t/stack-overflow--/8226/5 "2012-02-28T08:57:38Z")

</div>

> [@](#):
>
> It must look too stupid for anyone else to want to bother.

Well, it’s been about three hours since your first post and this forum isn’t that crowded as to expect answers within minutes.

Adding a component as its own child isn’t such a good idea (and quite likely caused the stack overflow). Also avoid using global objects (juceWindow in your posted code).  
Try:

```auto
// MainAppWindow header file:
class MainAppWindow : public ...
{
[...]

private:
  JucerComponent juceWindow;
  
}
```

```auto
MainAppWindow::MainAppWindow()
    : DocumentWindow (JUCEApplication::getInstance()->getApplicationName(),
                      Colours::lightgrey,
                      DocumentWindow::allButtons)
{
    setContentNonOwned(&juceWindow, true);
    centreWithSize (900, 900);
    setVisible (true);

}

MainAppWindow::~MainAppWindow()
{
  clearContentComponent();

  // Don't use deleteAllChildren. Either use ScopedPointers (which delete itselves) or local objects (like juceWindow here).
  // juceWindow->deleteAllChildren();
}

```

Chris

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [February 28, 2012, 9:03am UTC](https://forum.juce.com/t/stack-overflow--/8226/6 "2012-02-28T09:03:02Z")

</div>

Excellent advice from Chris there.

> [@](#):
>
> What am I doing wrong here?

Not using your debugger! It’ll give you answers a lot more quickly and reliably than a forum!

---

<div class="post-metadata">

**Author:** ![couch.jalapeno](https://avatars.discourse-cdn.com/v4/letter/c/90ced4/32.png) [@couch.jalapeno](https://forum.juce.com/u/couch.jalapeno)\
**Post date:** [February 28, 2012, 1:40pm UTC](https://forum.juce.com/t/stack-overflow--/8226/7 "2012-02-28T13:40:13Z")

</div>

> [@jules](#):
>
> Not using your debugger! It’ll give you answers a lot more quickly and reliably than a forum!

Sorry Jules, I’m very new with OOP & swear I’d spent several weeks bashing it out on the compiler, going thru the tutorials (based on the outdated version), flipping through the forum, & looking up the compiler error messages both on google & in the forums before resorting to this.

Thanx for this amazing toolbox, I just wish I can make sense of it soon.

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [February 28, 2012, 1:45pm UTC](https://forum.juce.com/t/stack-overflow--/8226/8 "2012-02-28T13:45:56Z")

</div>

Glad you’re enjoying the learning experience, I’m sure you’ll get the hang of it soon enough!

---

<div class="post-metadata">

**Author:** ![couch.jalapeno](https://avatars.discourse-cdn.com/v4/letter/c/90ced4/32.png) [@couch.jalapeno](https://forum.juce.com/u/couch.jalapeno)\
**Post date:** [February 28, 2012, 2:30pm UTC](https://forum.juce.com/t/stack-overflow--/8226/9 "2012-02-28T14:30:42Z")

</div>

[quote=“ckk”]Adding a component as its own child isn’t such a good idea (and quite likely caused the stack overflow). Also avoid using global objects (juceWindow in your posted code).  
Try:  
[/quote]  
Omig :shock: sh, u solved it & didnt even see my header file

- it runs & it looks beaaauuuutiful.  
Thank you ever so much, Godwin (& Jules)

Only, I’m still not freeing up memory properly, so it crashes whenever I quit.  
I’ve seen deleteAndZero(Component); , deleteAllChildren(Compponent); , & componentWindow = 0; used everywhere & I’ve tried them without success.

---

<div class="post-metadata">

**Author:** ![couch.jalapeno](https://avatars.discourse-cdn.com/v4/letter/c/90ced4/32.png) [@couch.jalapeno](https://forum.juce.com/u/couch.jalapeno)\
**Post date:** [February 28, 2012, 2:34pm UTC](https://forum.juce.com/t/stack-overflow--/8226/10 "2012-02-28T14:34:32Z")

</div>

> [@jules](#):
>
> I’m sure you’ll get the hang of it soon enough!

Thank u very much, Jules. - I do hope so

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [February 28, 2012, 2:46pm UTC](https://forum.juce.com/t/stack-overflow--/8226/11 "2012-02-28T14:46:31Z")

</div>

> [@](#):
>
> I’ve seen deleteAndZero(Component); , deleteAllChildren(Compponent); , & componentWindow = 0; used everywhere & I’ve tried them without success.

I can only apologise for having used those things in the past (a long time ago, but some of the old code is still knocking around).

Never, ever use delete or deleteAndZero. And you should almost never use deleteAllChildren() (ok, there are a couple of obscure situations where I’ve actually found it essential, and yes, I know the Jucer generates code that uses it… but really, in your own code it should never be used).

I’ll be doing a new demo app soon which will get rid of any vestiges of old-fashioned programming like that.

---

<div class="post-metadata">

**Author:** ![couch.jalapeno](https://avatars.discourse-cdn.com/v4/letter/c/90ced4/32.png) [@couch.jalapeno](https://forum.juce.com/u/couch.jalapeno)\
**Post date:** [February 29, 2012, 4:31am UTC](https://forum.juce.com/t/stack-overflow--/8226/12 "2012-02-29T04:31:51Z")

</div>

> [@jules](#):
>
> I can only apologise for having used those things in the past (a long time ago, but some of the old code is still knocking around).

Please don’t - it’s amazing, all u’ve done for the programming community

> [@jules](#):
>
> Never, ever use delete or deleteAndZero. And you should almost never use deleteAllChildren() (ok, there are a couple of obscure situations where I’ve actually found it essential, and yes, I know the Jucer generates code that uses it… but really, in your own code it should never be used).

ah, I see - thank u. someone suggested I try clearContentComponent(), which worked.

[quote=“jules”]

> [@jules](#):
>
> I’ll be doing a new demo app soon which will get rid of any vestiges of old-fashioned programming like that.[/quote]

Yays! 😃

---

<div class="post-metadata">

**Author:** ![dinaiz](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/dinaiz/32/182_2.png) [@dinaiz](https://forum.juce.com/u/dinaiz)\
**Post date:** [April 2, 2012, 8:36am UTC](https://forum.juce.com/t/stack-overflow--/8226/13 "2012-04-02T08:36:36Z")

</div>

> [@jules](#):
>
> Never, ever use delete or deleteAndZero. And you should almost never use deleteAllChildren() (ok, there are a couple of obscure situations where I’ve actually found it essential, and yes, I know the Jucer generates code that uses it… but really, in your own code it should never be used).

ah, I see - thank u. someone suggested I try clearContentComponent(), which worked.

Just to clarify, I think what Jules meant wasn’t using clearContentComponent instead of deleteAndzero . He meant (correct me if I’m wrong) that you should use RAII techniques instead of manually deleting stuff (see ScopedPointer, ReferenceCountedObjectPtr, and the likes …)

As a general guideline also, stack overflow is often a symptom of infinite recusrion, i.e. a function calling itself infinitely, which is what probably happens when you add a window to itself 🙂
