Browse Source

android: Fix crash on shutdown if stdout/err is not redirected

rdb 1 year ago
parent
commit
46e90058b9
1 changed files with 7 additions and 2 deletions
  1. 7 2
      panda/src/android/android_main.cxx

+ 7 - 2
panda/src/android/android_main.cxx

@@ -75,6 +75,7 @@ void android_main(struct android_app* app) {
     << "New native activity started on " << *current_thread << "\n";
 
   // Were we given an optional location to write the stdout/stderr streams?
+  bool owns_stdout = false;
   methodID = env->GetMethodID(activity_class, "getIntentOutputUri", "()Ljava/lang/String;");
   jstring joutput_uri = (jstring) env->CallObjectMethod(activity->clazz, methodID);
   if (joutput_uri != nullptr) {
@@ -92,6 +93,7 @@ void android_main(struct android_app* app) {
 
           dup2(fd, 1);
           dup2(fd, 2);
+          owns_stdout = true;
         } else {
           android_cat.error()
             << "Failed to open output path " << path << "\n";
@@ -109,6 +111,7 @@ void android_main(struct android_app* app) {
             << spec.get_server_and_port() << "\n";
           dup2(fd, 1);
           dup2(fd, 2);
+          owns_stdout = true;
         } else {
           android_cat.error()
             << "Failed to open output socket "
@@ -299,8 +302,10 @@ void android_main(struct android_app* app) {
     env->ReleaseStringUTFChars(filename, filename_str);
   }
 
-  close(1);
-  close(2);
+  if (owns_stdout) {
+    close(1);
+    close(2);
+  }
 
   // Detach the thread before exiting.
   activity->vm->DetachCurrentThread();