MouseEvent has no default constructor (i.e. parameter-less) which means that a Component cannot save a MouseEvent in a member variable and use it later:
This won’t compile:
class MyComponent : public AsyncUpdater
{
public:
void mouseEnter (MouseEvent& e)
{
m_mouseEvent = e; // Good luck with this one
triggerAsyncUpdate ();
}
private:
MouseEvent m_mouseEvent; // Class can't be instantiated because of this!
};
That was deliberate - I didn’t want people hanging onto copies of mouseevents, which only have a valid meaning during the actual event itself. Putting one into a member variable is the sort of thing I didn’t want to happen to them!
It’s part of detecting a mouse-enter for a group of components. I save the MouseEvent, trigger an AsyncUpdater and then later on call the virtual function with the event. Here is the relevant function:
But the event is no longer valid when the async updater fires… By that point, the components that are referenced by the event may have been deleted.
That’s why I tried to stop people copying these things around - it’s only safe to use them during the actual event callback. At a later point, calling its methods could cause crashes or return incorrect values.
True in the general case, but impossible the way my class is designed. I see your point though…I can just stick with my reinterpret_cast and char array hack.
Damn Jules, it seems you thought of everything. I see what you mean. If, during the processing of the mouse movements, a component referenced in my hierarchy changes its position it could make the coordinates become invalid.
Did you think of this as you were writing the MouseEvent class? Or did it later come up as a bug?
Did you think of this as you were writing the MouseEvent class?
Yes - all the code that surrounds component event callbacks has always had to be paranoid about components being deleted, because it’s so common to do so during a callback. Hence all the WeakReference/ComponentDeletionChecker stuff that’s scattered through the component classes.