Stack Overflow! >_<


#1

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:

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]


#2

Just to add it occurs in juce_MessageManager.cpp at

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

return instance;

}[/code]


#3

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

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

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

Chris


#4

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?


#5

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:

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

private:
  JucerComponent juceWindow;
  
}
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


#6

Excellent advice from Chris there.

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


#7

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.


#8

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


#9

[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.


#10

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


#11

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.


#12

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

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

[quote=“jules”]

Yays! :smiley:


#13

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 :slight_smile: