@@ -5730,9 +5730,7 @@ void InputDispatcher::doDispatchCycleFinishedCommand(nsecs_t finishTime,
57305730
57315731 bool restartEvent;
57325732 if (dispatchEntry->eventEntry ->type == EventEntry::Type::KEY) {
5733- KeyEntry& keyEntry = static_cast <KeyEntry&>(*(dispatchEntry->eventEntry ));
5734- restartEvent =
5735- afterKeyEventLockedInterruptable (connection, dispatchEntry, keyEntry, handled);
5733+ restartEvent = afterKeyEventLockedInterruptable (connection, dispatchEntry, handled);
57365734 } else if (dispatchEntry->eventEntry ->type == EventEntry::Type::MOTION) {
57375735 MotionEntry& motionEntry = static_cast <MotionEntry&>(*(dispatchEntry->eventEntry ));
57385736 restartEvent = afterMotionEventLockedInterruptable (connection, dispatchEntry, motionEntry,
@@ -5960,7 +5958,17 @@ void InputDispatcher::processConnectionResponsiveLocked(const Connection& connec
59605958
59615959bool InputDispatcher::afterKeyEventLockedInterruptable (const sp<Connection>& connection,
59625960 DispatchEntry* dispatchEntry,
5963- KeyEntry& keyEntry, bool handled) {
5961+ bool handled) {
5962+ // The dispatchEntry is currently valid, but it might point to a deleted object after we release
5963+ // the lock. For simplicity, make copies of the data of interest here and assume that
5964+ // 'dispatchEntry' is not valid after this section.
5965+ // Hold a strong reference to the EventEntry to ensure it's valid for the duration of this
5966+ // function, even if the DispatchEntry gets destroyed and releases its share of the ownership.
5967+ std::shared_ptr<EventEntry> eventEntry = dispatchEntry->eventEntry ;
5968+ const bool hasForegroundTarget = dispatchEntry->hasForegroundTarget ();
5969+ KeyEntry& keyEntry = static_cast <KeyEntry&>(*(eventEntry));
5970+ // To prevent misuse, ensure dispatchEntry is no longer valid.
5971+ dispatchEntry = nullptr ;
59645972 if (keyEntry.flags & AKEY_EVENT_FLAG_FALLBACK) {
59655973 if (!handled) {
59665974 // Report the key as unhandled, since the fallback was not handled.
@@ -5977,7 +5985,7 @@ bool InputDispatcher::afterKeyEventLockedInterruptable(const sp<Connection>& con
59775985 connection->inputState .removeFallbackKey (originalKeyCode);
59785986 }
59795987
5980- if (handled || !dispatchEntry-> hasForegroundTarget () ) {
5988+ if (handled || !hasForegroundTarget) {
59815989 // If the application handles the original key for which we previously
59825990 // generated a fallback or if the window is not a foreground window,
59835991 // then cancel the associated fallback key, if any.
0 commit comments