Seg Fault When Adding Gui Component as ScopedPointer to MainConent


#1

First of all I'm VERY new to JUCE!

 

As the title says, I created my first GUI component, I tried to add it to MainContentComponent like this:

 

[1] In header file

private:

    ScopedPointer<MetronomeGUI> metronomeGUI;


[2] in source file.


MainContentComponent::MainContentComponent () 

{

    setSize (400, 200);

    addAndMakeVisible (metronomeGUI = new MetronomeGUI ());

}

 

And it gives me segmentation fault when I run the program...

 

Now if I use the MetronomeGUI component as the MainComponent, meaning directly into the window class it works...

If I remove the ScopedPointer from MetronomeGUI it works... 

 

If I made any mistakes in writing the code, don't mind them because the program compiles just fine.

 

Why does this happen?

 


#2

I imagine the probem here is with your MetronomeGUI class. Step through your code with the debugger. That's what they are there for. And if you've never used one before, please take the time to learn. In the long run it will save your hours and hours of head scratching! 


#3

I am not sure if this changed in recent compiler languages, but when I learned C++ the return value of an assignment was "true". So in your case you add a "true" as a child, which will not work...

If you do this in two lines I *think* it should work:


metronomeGUI = new MetronomeGUI ();
addAndMakeVisible (metronomeGUI);

But there might be other problems, which you may find in your debugger, as Rory said...

HTH, daniel


#4

Don't use the ScopedPointer, just do it like this:


class MainContentComponent : public Component
{
public:
    MainContentComponent()
    {
        setSize (400, 200);
        addAndMakeVisible (metronomeGUI)
    }
private:
    MetronomeGUI metronomeGUI;
}

Besides, MetronomeGUI sounds like an odd class name to me, why don't you call it simply MetronomeComponent if that's what it is?

 


#5

when I learned C++ the return value of an assignment was "true"

Sorry but that's definitely not true. In C++, the assignment operator typically returns a reference to the object on the left hand side after assignment. Otherwise you couldn't chain assignments like this: a = b = c = d;

Of course, people can overload the assignment operator in their classes, and theoretically they could make it return whatever they want to, but C++ best practice is that you should always do the above.


#6

Then I got that wrong, sorry. I might have messid it up with something else, can't remember.

So simply take my idea to improve readability ;-)

Thanks for correcting me.


#7

I found what the problem was... scopped pointers are fine, I don't get why you suggest not using one when Jules says that using them is encouraged (or at least I think I read that somewhere)


#8

Took a while but I found the problem... 

I was calling Component::setSize in the first line of the class constructor, which from what I read in the documentation calls synchronously the Component::resized() method... in that method I had metronomeGUI->setBounds(0, 0, getWidth(), getHeight()); But the metronomeGUI variable  was not yet initialized.... hence the seg fault. 


#9

But in this case there is no need to declare your object on the heap. I think that's what Timur is getting at?  


#10

This kind of issue is found with a debugger within seconds so as Rory already suggested, you should get to know how to debug.

In Visual Studio it is advisable to also break on exceptions (set “Win32 Exceptions” in Debug->Exceptions). You then see where in your program a crash occurs and can step backwards through the stack trace to corner the crash’s cause.

It’s easier to use objects instead of ScopedPointer<> as the object’s initialisation is also automatic. You only need to use a pointer if the object’s constructor relies on the holder’s this pointer as this isn’t necessarily valid within the initialisation list.


#11

Using them is fine, but only if you need to! Much much better to avoid heap allocation if possible.


#12

Yeah, visual studio... I'm on linux ... Of course I tried iwth a debugger but I don't know how to use them well yet in linux, I'm still figuring out my tools. In visual studio debugging is kids play, was doing that since visual studio 6... Thanks. 


#13

That's not really an excuse in this day and age Chris!! Linux has plenty of IDEs that let you step through your code. CodeLite, CodeBlocks, Eclipse, KDeveloper, NetBeans, Anjuata, etc. Ok, there are not as developed as VS, but CodeLite integrates its debugger very nicely, and was clearly influenced by MS tools. 

 

[edit] I hope this doesn't come across as dismissive. I just wanted to point out, in friendly way, that things aren't so bad on Linux as some people think!  


#14

...and in case you go for Code::Blocks, the Introjucer can even directly export a Code::Blocks project for you.

(although it's been a while since I've tested the Code::Blocks export, hope it still works ;-)