Fix shutdown when the current thread has been manually detached.

Change-Id: I76990a88c31ff671c13e1e1a90710749911d185c
diff --git a/src/oatexec.cc b/src/oatexec.cc
index 4f7228b..4f068db 100644
--- a/src/oatexec.cc
+++ b/src/oatexec.cc
@@ -172,10 +172,14 @@
 
   int rc = InvokeMain(env, &argv[arg_idx]);
 
+#if defined(NDEBUG)
+  // The DestroyJavaVM call will detach this thread for us. In debug builds, we don't want to
+  // detach because detaching disables the CheckSafeToLockOrUnlock checking.
   if (vm->DetachCurrentThread() != JNI_OK) {
     fprintf(stderr, "Warning: unable to detach main thread\n");
     rc = EXIT_FAILURE;
   }
+#endif
 
   if (vm->DestroyJavaVM() != 0) {
     fprintf(stderr, "Warning: runtime did not shut down cleanly\n");