Index: trunk/zoo-project/zoo-kernel/service_internal_js.c
===================================================================
--- trunk/zoo-project/zoo-kernel/service_internal_js.c	(revision 453)
+++ trunk/zoo-project/zoo-kernel/service_internal_js.c	(revision 471)
@@ -39,5 +39,7 @@
   for(i=0;i<argc;i++){
     JSString* jsmsg = JS_ValueToString(cx,argv[i]);
-    fprintf(stderr,"[ZOO-API:JS] %s\n",JS_EncodeString(cx,jsmsg));
+    char *tmp=JS_EncodeString(cx,jsmsg);
+    fprintf(stderr,"[ZOO-API:JS] %s\n",tmp);
+    free(tmp);
   }
   JS_MaybeGC(cx);
@@ -69,4 +71,5 @@
 #endif
     JSObject *api_script1=loadZooApiFile(cx,JS_GetGlobalObject(cx),api0);
+    free(api0);
   }
   JS_MaybeGC(cx);
@@ -158,4 +161,5 @@
 #endif
   JSObject *api_script1=loadZooApiFile(cx,global,api0);
+  free(api0);
   fflush(stderr);
 
@@ -166,4 +170,5 @@
 #endif
   JSObject *api_script2=loadZooApiFile(cx,global,api1);
+  free(api1);
   fflush(stderr);
 
@@ -183,5 +188,5 @@
   struct stat file_status;
   stat(filename, &file_status);
-  char *source=(char*)malloc(file_status.st_size);
+  //char *source=(char*)malloc(file_status.st_size);
   uint16 lineno;
   jsval rval;
@@ -194,12 +199,17 @@
     char tmp1[1024];
     sprintf(tmp1,"Unable to load JavaScript file %s",filename);
+    free(filename);
     map* err=createMap("text",tmp1);
-    addMapToMap(&err,createMap("code","NoApplicableCode"));
+    addToMap(err,"code","NoApplicableCode");
     printExceptionReportResponse(mc,err);
+    freeMap(&err);
+    free(err);
+    JS_MaybeGC(cx);
     JS_DestroyContext(cx);
     JS_DestroyRuntime(rt);
     JS_ShutDown();
-    exit(-1);
-  }
+    return -1;
+  }
+  
 
   /* Call a function in obj's scope. */
@@ -243,9 +253,11 @@
     freeMap(&err);
     free(err);
+    free(filename);
+    JS_MaybeGC(cx);
     JS_DestroyContext(cx);
     JS_DestroyRuntime(rt);
     JS_ShutDown();
     // Should return -1 here but the unallocation won't work from zoo_service_loader.c line 1847
-    exit(-1);
+    return -1;
   }
 
@@ -344,4 +356,5 @@
   JS_DestroyRuntime(rt);
   JS_ShutDown();
+  free(filename);
 #ifdef JS_DEBUG
   fprintf(stderr,"Returned value %d\n",res);
@@ -481,17 +494,18 @@
 	jsmsg = JS_ValueToString(cx,vp);
 	len1 = JS_GetStringLength(jsmsg);
-
+	
+	tmp=JS_EncodeString(cx,jsmsg);
 	tres=(maps*)malloc(MAPS_SIZE);
-	tres->name=zStrdup(JS_EncodeString(cx,jsmsg));
+	tres->name=zStrdup(tmp);
 	tres->content=NULL;
 	tres->next=NULL;
 
 	jsval nvp=JSVAL_NULL;
-	if((JS_GetProperty(cx, tt, JS_EncodeString(cx,jsmsg), &nvp)==JS_FALSE)){
-#ifdef JS_DEBUG
-	  fprintf(stderr,"Enumerate id : %d => %s => No more value\n",oi,JS_EncodeString(cx,jsmsg));
+	if((JS_GetProperty(cx, tt, tmp, &nvp)==JS_FALSE)){
+#ifdef JS_DEBUG
+	  fprintf(stderr,"Enumerate id : %d => %s => No more value\n",oi,tmp);
 #endif
 	}
-	
+	free(tmp);
 	JSObject *nvp1=JSVAL_TO_OBJECT(JSVAL_NULL);
 	JS_ValueToObject(cx,nvp,&nvp1);
@@ -510,4 +524,5 @@
 		
       }
+      JS_DestroyIdArray(cx,idp);
     }
   }
@@ -550,14 +565,15 @@
 	jsmsg = JS_ValueToString(cx,vp);
 	len1 = JS_GetStringLength(jsmsg);
-#ifdef JS_DEBUG
-	fprintf(stderr,"Enumerate id : %d => %s\n",oi,JS_EncodeString(cx,jsmsg));
+	tmp=JS_EncodeString(cx,jsmsg);
+#ifdef JS_DEBUG
+	fprintf(stderr,"Enumerate id : %d => %s\n",oi,tmp);
 #endif
 	jsval nvp=JSVAL_NULL;
-	if((JS_GetProperty(cx, JSVAL_TO_OBJECT(tmp1), JS_EncodeString(cx,jsmsg), &nvp)==JS_FALSE)){
-#ifdef JS_DEBUG
-	  fprintf(stderr,"Enumerate id : %d => %s => No more value\n",oi,JS_EncodeString(cx,jsmsg));
+	if((JS_GetProperty(cx, JSVAL_TO_OBJECT(tmp1), tmp, &nvp)==JS_FALSE)){
+#ifdef JS_DEBUG
+	  fprintf(stderr,"Enumerate id : %d => %s => No more value\n",oi,tmp);
 #endif
 	}
-	
+	free(tmp);
 	if(JSVAL_IS_OBJECT(nvp)){
 #ifdef JS_DEBUG
@@ -571,24 +587,29 @@
 	if(JSVAL_IS_OBJECT(nvp1j)){
 	  JSString *jsmsg1;
+	  char *tmp1, *tmp2;
 	  JSObject *nvp2=JSVAL_TO_OBJECT(JSVAL_NULL);
 	  jsmsg1 = JS_ValueToString(cx,nvp1j);
 	  len1 = JS_GetStringLength(jsmsg1);
-#ifdef JS_DEBUG
-	  fprintf(stderr,"JSVAL NVP1J IS OBJECT %s = %s\n",JS_EncodeString(cx,jsmsg),JS_EncodeString(cx,jsmsg1));
-#endif
-	  if(strcasecmp(JS_EncodeString(cx,jsmsg1),"[object Object]")==0){
-	    tres->name=zStrdup(JS_EncodeString(cx,jsmsg));
+	  tmp1=JS_EncodeString(cx,jsmsg1);
+	  tmp2=JS_EncodeString(cx,jsmsg);
+#ifdef JS_DEBUG
+	  fprintf(stderr,"JSVAL NVP1J IS OBJECT %s = %s\n",JS_EncodeString(cx,jsmsg),tmp1);
+#endif
+	  if(strcasecmp(tmp1,"[object Object]")==0){
+	    tres->name=zStrdup(tmp2);
 	    tres->content=mapFromJSObject(cx,nvp1j);
 	  }
 	  else
-	    if(strcasecmp(JS_EncodeString(cx,jsmsg),"name")==0){
-	      tres->name=zStrdup(JS_EncodeString(cx,jsmsg1));
+	    if(strcasecmp(tmp2,"name")==0){
+	      tres->name=zStrdup(tmp1);
 	    }
 	    else{
 	      if(tres->content==NULL)
-		tres->content=createMap(JS_EncodeString(cx,jsmsg),JS_EncodeString(cx,jsmsg1));
+		tres->content=createMap(tmp2,tmp1);
 	      else
-		addToMap(tres->content,JS_EncodeString(cx,jsmsg),JS_EncodeString(cx,jsmsg1));
+		addToMap(tres->content,tmp2,tmp1);
 	    }
+	  free(tmp1);
+	  free(tmp2);
 	}
 #ifdef JS_DEBUG
@@ -607,5 +628,5 @@
       free(tres);
       tres=NULL;
-
+      JS_DestroyIdArray(cx,idp);
     }
   }
@@ -633,5 +654,5 @@
       JSString* str; 
       JS_IdToValue(cx,id,&vp);
-      char *c, *tmp;
+      char *c, *tmp, *tmp1;
       JSString *jsmsg,*jsmsg1;
       size_t len,len1;
@@ -639,24 +660,29 @@
       len = JS_GetStringLength(jsmsg);
       jsval nvp;
-      JS_GetProperty(cx, JSVAL_TO_OBJECT(t), JS_EncodeString(cx,jsmsg), &nvp);
+      tmp=JS_EncodeString(cx,jsmsg);
+      JS_GetProperty(cx, JSVAL_TO_OBJECT(t), tmp, &nvp);
       jsmsg1 = JS_ValueToString(cx,nvp);
       len1 = JS_GetStringLength(jsmsg1);
-#ifdef JS_DEBUG
-      fprintf(stderr,"Enumerate id : %d [ %s => %s ]\n",index,JS_EncodeString(cx,jsmsg),JS_EncodeString(cx,jsmsg1));
+      tmp1=JS_EncodeString(cx,jsmsg1);
+#ifdef JS_DEBUG
+      fprintf(stderr,"Enumerate id : %d [ %s => %s ]\n",index,tmp,tmp1);
 #endif
       if(res!=NULL){
 #ifdef JS_DEBUG
-	fprintf(stderr,"%s - %s\n",JS_EncodeString(cx,jsmsg),JS_EncodeString(cx,jsmsg1));
-#endif
-	addToMap(res,JS_EncodeString(cx,jsmsg),JS_EncodeString(cx,jsmsg1));
+	fprintf(stderr,"%s - %s\n",tmp,tmp1);
+#endif
+	addToMap(res,tmp,tmp1);
       }
       else{
-	res=createMap(JS_EncodeString(cx,jsmsg),JS_EncodeString(cx,jsmsg1));
+	res=createMap(tmp,tmp1);
 	res->next=NULL;
       }
+      free(tmp);
+      free(tmp1);
 #ifdef JS_DEBUG
       dumpMap(res);
 #endif
     }
+    JS_DestroyIdArray(cx,idp);
   }
 #ifdef JS_DEBUG
