Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Actually allows the GC to reclaim tasks (fix #18874)
authorMartin Quinson <martin.quinson@loria.fr>
Sat, 25 Apr 2015 23:39:37 +0000 (01:39 +0200)
committerMartin Quinson <martin.quinson@loria.fr>
Sat, 25 Apr 2015 23:39:50 +0000 (01:39 +0200)
ChangeLog
src/bindings/java/jmsg_task.c
src/bindings/java/jmsg_task.h
src/bindings/java/org/simgrid/msg/Task.java

index dc3e85d..af38a7e 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -16,6 +16,8 @@ SimGrid (3.12) NOT RELEASED; urgency=low
    - Rename Process.currentProcess() to Process.getCurrentProcess()
    - Rename Task.setDataSize() to Task.setBytesAmount()
    - Merge Task.getRemainingDuration() and Task.getComputeDuration() into Task.getFlopsAmount()
+ * Bug fixes:
+   - #18874: Actually allows the GC to reclaim tasks
    
  SIMIX:
  * New functions
index 850a999..40cffed 100644 (file)
@@ -651,7 +651,17 @@ MSG_task_set_data(task, (void *) (*env)->NewGlobalRef(env, jtask));
   return jcomm;
 }
 
+JNIEXPORT void JNICALL
+Java_org_simgrid_msg_Task_doFinalize(JNIEnv * env, jobject jtask) {
+         msg_task_t task = jtask_to_native_task(jtask, env);
+
+         if (!task) {
+           jxbt_throw_notbound(env, "task", jtask);
+           return;
+         }
 
+         MSG_task_destroy(task);
+}
 
 static void msg_task_cancel_on_failed_dsend(void*t) {
   msg_task_t task = t;
index 77daa9f..2edb2ca 100644 (file)
@@ -71,6 +71,8 @@ Java_org_simgrid_msg_Task_nativeInit(JNIEnv *env, jclass cls);
 JNIEXPORT void JNICALL
 Java_org_simgrid_msg_Task_create
                (JNIEnv * env, jobject jtask, jstring jname, jdouble jcomputeDuration, jdouble jmessageSize);
+JNIEXPORT void JNICALL
+Java_org_simgrid_msg_Task_doFinalize(JNIEnv * env, jobject jtask);
 /*
  * Class               org_simgrid_msg_Task
  * Method              parallelCreate
index 95893ec..16e54ef 100644 (file)
@@ -161,17 +161,19 @@ public class Task {
        /** Cancels a task. */ 
        public native void cancel();
 
-       /** Deletes a task.
-        *
-        * @exception                   NativeException if the destruction failed.
-        */ 
-       protected void finalize() throws NativeException {
-               destroy();
+       /** Deletes a task once the garbage collector reclaims it */
+       @Override
+       protected void finalize() {
+               try {
+                       // Exceptions in finalizers lead to bad situations:
+                       // http://stackoverflow.com/questions/7644556/troubleshooting-a-java-memory-leak-finalization
+                       doFinalize();
+                       bind=0; // to avoid segfaults if the impossible happens yet again making this task surviving its finalize()
+               } catch (Exception e) {
+                       e.printStackTrace();
+               }
        }
-       /**
-        * The natively implemented method to destroy a MSG task.
-        */
-       protected native void destroy();
+       protected native void doFinalize();
        /* *                       * *
         * * Communication-related * *
         * *                       * */