Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
sanitize the OOP of kernel::profile
[simgrid.git] / src / bindings / java / jmsg_task.cpp
index f146abe..b017a48 100644 (file)
@@ -127,9 +127,7 @@ JNIEXPORT void JNICALL Java_org_simgrid_msg_Task_execute(JNIEnv * env, jobject j
     return;
   }
   msg_error_t rv;
-  try {
-    rv = MSG_task_execute(task);
-  } catch (simgrid::kernel::context::Context::StopRequest& e) {
+  if (not simgrid::kernel::context::StopRequest::try_n_catch([&rv, &task]() { rv = MSG_task_execute(task); })) {
     jxbt_throw_by_name(env, "org/simgrid/msg/ProcessKilledError", "Process killed");
   }
 
@@ -288,9 +286,9 @@ JNIEXPORT jobject JNICALL Java_org_simgrid_msg_Task_receive(JNIEnv* env, jclass
 
   const char *alias = env->GetStringUTFChars(jalias, 0);
   msg_error_t rv;
-  try {
-    rv = MSG_task_receive_ext(&task, alias, (double)jtimeout, /*host*/ nullptr);
-  } catch (simgrid::kernel::context::Context::StopRequest& e) {
+  if (not simgrid::kernel::context::StopRequest::try_n_catch([&rv, &task, &alias, &jtimeout]() {
+        rv = MSG_task_receive_ext(&task, alias, (double)jtimeout, /*host*/ nullptr);
+      })) {
     jxbt_throw_by_name(env, "org/simgrid/msg/ProcessKilledError", "Process killed");
   }
   env->ReleaseStringUTFChars(jalias, alias);
@@ -478,7 +476,10 @@ static void msg_task_cancel_on_failed_dsend(void*t) {
   /* Destroy the global ref so that the JVM can free the stuff */
   env->DeleteGlobalRef(jtask_global);
   MSG_task_set_data(task, nullptr);
-  MSG_task_destroy(task);
+  /* Don't free the C data here, to avoid a race condition with the GC also sometimes doing so.
+   * A rare memleak is seen as preferable to a rare "free(): invalid pointer" failure that
+   * proves really hard to debug.
+   */
 }
 
 JNIEXPORT void JNICALL Java_org_simgrid_msg_Task_dsend(JNIEnv * env, jobject jtask, jstring jalias)