Merge "drm_hwcomposer: ground work for squashing" into mnc-dr-dev
diff --git a/nvimporter.cpp b/nvimporter.cpp
index de3ed55..71b3b7f 100644
--- a/nvimporter.cpp
+++ b/nvimporter.cpp
@@ -157,10 +157,15 @@
 
     gem_close.handle = bo->gem_handles[i];
     int ret = drmIoctl(drm_->fd(), DRM_IOCTL_GEM_CLOSE, &gem_close);
-    if (ret)
+    if (ret) {
       ALOGE("Failed to close gem handle %d %d", i, ret);
-    else
+    } else {
+      /* Clear any duplicate gem handle as well but don't close again */
+      for(int j = i + 1; j < num_gem_handles; j++)
+        if(bo->gem_handles[j] == bo->gem_handles[i])
+          bo->gem_handles[j] = 0;
       bo->gem_handles[i] = 0;
+    }
   }
 }