Crash on using MouseListener


#1

First the crash:


mouse enter
Program received signal SIGSEGV, Segmentation fault.
0x00000000004bc722 in juce::WeakReference<juce::Component, juce::ReferenceCountedObject>::get() const ()
(gdb) bt
#0  0x00000000004bc722 in juce::WeakReference<juce::Component, juce::ReferenceCountedObject>::get() const ()
#1  0x000000000046797e in juce::Component::BailOutChecker::shouldBailOut() const ()
#2  0x00000000004c48d7 in juce::Component::MouseListenerList::sendMouseEvent(juce::Component&, juce::Component::BailOutChecker&, void (juce::MouseListener::*)(juce::MouseEvent const&), juce::MouseEvent const&) ()
#3  0x000000000048197e in juce::Component::internalMouseEnter(juce::MouseInputSource, juce::Point<int>, juce::Time) ()

The "mouse enter" is from my own code, which illustrates that it's getting called.

So: I created a subclass of MouseListener:


class MyMouseListener : public MouseListener

And implemented all the virtual members.  Inside the mouse enter listener I call into my code, which is the "mouse enter" you saw.

 

The initialization is after I create the Component, I do addMouseListener( mylistener, false);  where 'mylistener' is a singleton pointer to a MyMouseListener.   

My problem is I have no clue why JUCE is complaining here.  The identical handling code was working fine when I subclassed the Component... so it's something to do with the listener...

Any clues?


#2

It's basic C++ object lifetime management. Most likely you've registered your mouselistener, then deleted it without first deregistering it.


#3

Nope.  The listener is at global scope, it's a pointer, created with new.  I definitely have not deleted it in the meantime

 


#4

Originally I had made it a simple object, without allocating dynamically, but at global scope.  I figured that would be just fine; but it also crashed.


#5

Then you must be doing something in your callback that's trashing the stack.


#6

Yes, it must be something like that; I'm trying to figure it out now...


#7

Figured it out.  

I needed to preserve a register context across calls; it wasn't a problem until I did the listener, which is somewhat interesting.