@@ -5745,9 +5745,7 @@ void InputDispatcher::doDispatchCycleFinishedCommand(nsecs_t finishTime,
57455745
57465746 bool restartEvent;
57475747 if (dispatchEntry->eventEntry ->type == EventEntry::Type::KEY) {
5748- KeyEntry& keyEntry = static_cast <KeyEntry&>(*(dispatchEntry->eventEntry ));
5749- restartEvent =
5750- afterKeyEventLockedInterruptable (connection, dispatchEntry, keyEntry, handled);
5748+ restartEvent = afterKeyEventLockedInterruptable (connection, dispatchEntry, handled);
57515749 } else if (dispatchEntry->eventEntry ->type == EventEntry::Type::MOTION) {
57525750 MotionEntry& motionEntry = static_cast <MotionEntry&>(*(dispatchEntry->eventEntry ));
57535751 restartEvent = afterMotionEventLockedInterruptable (connection, dispatchEntry, motionEntry,
@@ -5975,7 +5973,17 @@ void InputDispatcher::processConnectionResponsiveLocked(const Connection& connec
59755973
59765974bool InputDispatcher::afterKeyEventLockedInterruptable (const sp<Connection>& connection,
59775975 DispatchEntry* dispatchEntry,
5978- KeyEntry& keyEntry, bool handled) {
5976+ bool handled) {
5977+ // The dispatchEntry is currently valid, but it might point to a deleted object after we release
5978+ // the lock. For simplicity, make copies of the data of interest here and assume that
5979+ // 'dispatchEntry' is not valid after this section.
5980+ // Hold a strong reference to the EventEntry to ensure it's valid for the duration of this
5981+ // function, even if the DispatchEntry gets destroyed and releases its share of the ownership.
5982+ std::shared_ptr<EventEntry> eventEntry = dispatchEntry->eventEntry ;
5983+ const bool hasForegroundTarget = dispatchEntry->hasForegroundTarget ();
5984+ KeyEntry& keyEntry = static_cast <KeyEntry&>(*(eventEntry));
5985+ // To prevent misuse, ensure dispatchEntry is no longer valid.
5986+ dispatchEntry = nullptr ;
59795987 if (keyEntry.flags & AKEY_EVENT_FLAG_FALLBACK) {
59805988 if (!handled) {
59815989 // Report the key as unhandled, since the fallback was not handled.
@@ -5992,7 +6000,7 @@ bool InputDispatcher::afterKeyEventLockedInterruptable(const sp<Connection>& con
59926000 connection->inputState .removeFallbackKey (originalKeyCode);
59936001 }
59946002
5995- if (handled || !dispatchEntry-> hasForegroundTarget () ) {
6003+ if (handled || !hasForegroundTarget) {
59966004 // If the application handles the original key for which we previously
59976005 // generated a fallback or if the window is not a foreground window,
59986006 // then cancel the associated fallback key, if any.
0 commit comments