unlinkToDeath only when the listener has no more callbacks

Bug: N/A
Test: build, boot, SurfaceFlinger_test
Change-Id: I2bbca0f781897967a18731ade3a3b4724f16d361
diff --git a/services/surfaceflinger/TransactionCompletedThread.cpp b/services/surfaceflinger/TransactionCompletedThread.cpp
index 4c1baaf..1475889 100644
--- a/services/surfaceflinger/TransactionCompletedThread.cpp
+++ b/services/surfaceflinger/TransactionCompletedThread.cpp
@@ -310,8 +310,8 @@
                     // we get pointers that compare unequal in the SF process.
                     interface_cast<ITransactionCompletedListener>(listenerStats.listener)
                             ->onTransactionCompleted(listenerStats);
-                    listener->unlinkToDeath(mDeathRecipient);
                     if (transactionStatsDeque.empty()) {
+                        listener->unlinkToDeath(mDeathRecipient);
                         completedTransactionsItr =
                                 mCompletedTransactions.erase(completedTransactionsItr);
                     } else {