heap_close bug fixed
[cacao.git] / main.c
diff --git a/main.c b/main.c
index e49362c9d337979bfc7453c654a773ab43d90237..9d5c2eae768992b1f115be02959cb9f93ddc02cc 100644 (file)
--- a/main.c
+++ b/main.c
@@ -33,7 +33,7 @@
 #include "builtin.h"
 #include "native.h"
 
-#include "threads/thread.h"            /* schani */
+#include "threads/thread.h"
 
 bool compileall = false;
 int  newcompiler = true;               
@@ -42,6 +42,11 @@ bool verbose =  false;
 bool new_gc = false;
 #endif
 
+static bool showmethods = false;
+static bool showconstantpool = false;
+static bool showunicode = false;
+static classinfo *topclass;
+
 #ifndef USE_THREADS
 void **stackbottom = 0;
 #endif
@@ -436,6 +441,47 @@ void class_compile_methods ()
                }
 }
 
+/*
+ * void exit_handler(void)
+ * -----------------------
+ * The exit_handler function is called upon program termination to shutdown
+ * the various subsystems and release the resources allocated to the VM.
+ */
+
+void exit_handler(void)
+{
+       /********************* Debug-Tabellen ausgeben ************************/
+                               
+       if (showmethods) class_showmethods (topclass);
+       if (showconstantpool)  class_showconstantpool (topclass);
+       if (showunicode)       unicode_show ();
+
+#ifdef USE_THREADS
+       clear_thread_flags();           /* restores standard file descriptor
+                                                                  flags */
+#endif
+
+       /************************ Freigeben aller Resourcen *******************/
+
+       heap_close ();                          /* must be called before compiler_close and
+                                                                  loader_close because finalization occurs
+                                                                  here */
+
+#ifdef OLD_COMPILER
+       compiler_close ();
+#endif
+       loader_close ();
+       unicode_close ( literalstring_free );
+
+       if (verbose || getcompilingtime || statistics) {
+               log_text ("CACAO terminated");
+               if (statistics)
+                       print_stats ();
+               if (getcompilingtime)
+                       print_times ();
+               mem_usagelog(1);
+       }
+}
 
 /************************** Funktion: main *******************************
 
@@ -444,12 +490,10 @@ void class_compile_methods ()
    
 **************************************************************************/
 
-
 int main(int argc, char **argv)
 {
        s4 i,j;
        char *cp;
-       classinfo *topclass;
        java_objectheader *exceptionptr;
        void *dummy;
        
@@ -459,9 +503,6 @@ int main(int argc, char **argv)
        u4 heapsize = 16000000;
        u4 heapstartsize = 200000;
        char classpath[500] = ".:/usr/local/lib/java/classes";
-       bool showmethods = false;
-       bool showconstantpool = false;
-       bool showunicode = false;
        bool startit = true;
        char *specificmethodname = NULL;
        char *specificsignature = NULL;
@@ -469,11 +510,9 @@ int main(int argc, char **argv)
 #ifndef USE_THREADS
        stackbottom = &dummy;
 #endif
-
-
-#ifdef USE_THREADS
-       atexit(clear_thread_flags);
-#endif
+       
+       if (0 != atexit(exit_handler))
+               panic("unable to register exit_handler");
 
        /************ Infos aus der Environment lesen ************************/
 
@@ -718,8 +757,8 @@ int main(int argc, char **argv)
                        printf ("\n");
                }
 
-               /*              killThread(currentThread); */
-
+               killThread(currentThread);
+               fprintf(stderr, "still here\n");
        }
 
        /************* Auf Wunsch alle Methode "ubersetzen ********************/
@@ -751,39 +790,7 @@ int main(int argc, char **argv)
 #endif
        }
 
-       /********************* Debug-Tabellen ausgeben ************************/
-                               
-       if (showmethods) class_showmethods (topclass);
-       if (showconstantpool)  class_showconstantpool (topclass);
-       if (showunicode)       unicode_show ();
-
-   
-
-       /************************ Freigeben aller Resourcen *******************/
-
-       heap_close ();                          /* must be called before compiler_close and
-                                                                  loader_close because finalization occurs
-                                                                  here */
-#ifdef OLD_COMPILER
-       compiler_close ();
-#endif
-       loader_close ();
-       unicode_close ( literalstring_free );
-
-
-       /* Endemeldung ausgeben und mit entsprechendem exit-Status terminieren */
-
-       if (verbose || getcompilingtime || statistics) {
-               log_text ("CACAO terminated");
-               if (statistics)
-                       print_stats ();
-               if (getcompilingtime)
-                       print_times ();
-               mem_usagelog(1);
-       }
-               
        exit(0);
-       return 1;
 }
 
 
@@ -808,7 +815,7 @@ void cacao_shutdown(s4 status)
                sprintf (logtext, "Exit status: %d\n", (int) status);
                dolog();
                }
-               
+
        exit(status);
 }