Listener not getting message

Hello,

I’m trying to broadcast a change message to a subComponent, but its never getting there. I’ve traced the methods using DBG, and the sendChangeMessage() is definitely being called.

In one part of my code I have a Data Model extending ChangeBrodcaster, and on my View side of things I have a MainWindow->MainComponent->ChildComponent(0) that I’m trying to make a ChangeListener for changes to my Data Model.

I’m creating my MainWindow inside my Main.cpp->initialise(const String& commandLine), and then creating my Data Model and View Components inside my MainWindow() constructor. I then try to make a Listener out of the subComponent by calling…

The code compiles fine, and runs fine,… but when sendChangeMessage() is called, it never calls the ChangeListenerCallBack ?

Any Thoughts?

did you try to register the receiving component directly (without the (ChangeListener*) cast)?, does this work?

Yeah, I actually gave that a shot initially, but it returned an error saying

error: no matching function for call to ‘CSOMMain::addChangeListener(juce::Component*)’
/Developer/juce/juce_amalgamated.h:13206: note: candidates are: void juce::ChangeBroadcaster::addChangeListener(juce::ChangeListener*)

My friend codes using JUCE and he was pretty confused as well. He was saying he never had to cast before when registering ChangeListeners. ??? Not really sure what’s missing?

did you inherit a ChangeListener to the view?

[code]class myView : public ChangeListener
{
public:

	void changeListenerCallback (void* objectThatHasChanged);

[/code]

Yep :slight_smile:

I commented out everything but a DBG statement in the callback for testing right now.

mmh if you inherit the ChangeListener and add the add the object directly, the compiler should not produce this error, then there must be another problem.

You’d have to use dynamic_cast. If you don’t understand why, then it’d probably be a good career move to dig out your old c++ books and re-read the chapter about casts!

This is a perfect example of why you should never use old-fashioned C-style casts. If you’d been following that advice, you’d have written a static_cast instead, which wouldn’t have compiled, and you’d hopefully have realised your mistake. The C-style cast compiles, but is actually just reinterpreting the raw memory as a different type of object, so your callback will be dispatched via the wrong vtable (or via a random bit of memory that isn’t a vtable at all!)

figured it had to do with the casting making it call back to the wrong object, but I didn’t know why or how else to do it. I’ll read up on C++ casting (didn’t realize it was any different). I’ve only worked with JAVA before my recent attempts to Learn JUCE… C++ and JAVA are similar enough for me to write code thinking I might know what I’m doing, but more often then not it seems I have a lot to learn :slight_smile:

Thanks for the help

You are right, but I don’t think the reason is good. Such C cast does still produce an error as it’s not the expected type for the method signature. dynamic_cast wouldn’t have worked either here.
Also dynamic_cast only work if RTTI is enabled in the compiler.
I wonder why people don’t try to understand compiler’s error before trying to hack around type-less unsafe code.

Wait, now I’m confused?

[quote]dynamic_cast wouldn’t have worked either here.
Also dynamic_cast only work if RTTI is enabled in the compiler.[/quote]
The Dynamic_Cast did work, and when I return the Object in the Listener callback, a Static_Cast works fine. I just needed to read up on how polymorphism worked in C++. In the Java code I used to write, you would just cast using (ObjectToCastTo)ObjectReturnedFromCollection. These ideas of Dynamic/Static/ or the other one (which I haven’t used yet so it slips my mind), are all new to me.