soundtrigger: Modify conditional check in detach

- All the required SoundTrigger models of a module client
are not getting unloaded and removed as part of detach.
- mModels.size() is evaluated for every iteration which
gives current size of vector which results in exit of
loop without checking for all models in case elements
are removed in loop.
removeItemsAt would modify the vector indices for each
iteration and this would result in skipping access of
some models.
- Iterate from end of models to check and unload.

Bug: 35910617
Test: mmm frameworks/av/services/soundtrigger/
Author: Chaithanya Krishna Bacharaju <chaithan@codeaurora.org>
Change-Id: I72415e53d2e6f366f8ba6e6eeb0cd8323364a231
diff --git a/services/soundtrigger/SoundTriggerHwService.cpp b/services/soundtrigger/SoundTriggerHwService.cpp
index 78845b7..5b8d990 100644
--- a/services/soundtrigger/SoundTriggerHwService.cpp
+++ b/services/soundtrigger/SoundTriggerHwService.cpp
@@ -544,10 +544,11 @@
     ALOGV("remove client %p", moduleClient.get());
     mModuleClients.removeAt(index);
 
-    for (size_t i = 0; i < mModels.size(); i++) {
-        sp<Model> model = mModels.valueAt(i);
+    // Iterate in reverse order as models are removed from list inside the loop.
+    for (size_t i = mModels.size(); i > 0; i--) {
+        sp<Model> model = mModels.valueAt(i - 1);
         if (moduleClient == model->mModuleClient) {
-            mModels.removeItemsAt(i);
+            mModels.removeItemsAt(i - 1);
             ALOGV("detach() unloading model %d", model->mHandle);
             if (mHalInterface != 0) {
                 if (model->mState == Model::STATE_ACTIVE) {