]> gitweb.factorcode.org Git - factor.git/commitdiff
VM: add destructor to vm_parameters so that executable and image_path is
authorBjörn Lindqvist <bjourne@gmail.com>
Fri, 13 May 2016 21:12:16 +0000 (23:12 +0200)
committerBjörn Lindqvist <bjourne@gmail.com>
Sat, 14 May 2016 16:21:49 +0000 (18:21 +0200)
free'd

Valgrind complained that those were leaking

vm/factor.cpp
vm/image.cpp
vm/image.hpp
vm/os-genunix.cpp

index f554292570196aa3c96808d5f0c2d06aaaf1f599..7a2200f6db0cebe8c56e042aaf3bfcc41c11b7cc 100644 (file)
@@ -56,15 +56,12 @@ void factor_vm::init_factor(vm_parameters* p) {
   /* OS-specific initialization */
   early_init();
 
-  const vm_char* executable_path = vm_executable_path();
-
-  if (executable_path)
-    p->executable_path = executable_path;
+  p->executable_path = vm_executable_path();
 
   if (p->image_path == NULL) {
     if (embedded_image_p()) {
       p->embedded_image = true;
-      p->image_path = p->executable_path;
+      p->image_path = safe_strdup(p->executable_path);
     } else
       p->image_path = default_image_path();
   }
@@ -82,7 +79,8 @@ void factor_vm::init_factor(vm_parameters* p) {
 
   cell aliens[][2] = {
     {OBJ_CPU,             (cell)FACTOR_CPU_STRING},
-    {OBJ_EXECUTABLE,      (cell)p->executable_path},
+    {OBJ_EXECUTABLE,      (cell)safe_strdup(p->executable_path)},
+    {OBJ_IMAGE,           (cell)safe_strdup(p->image_path)},
     {OBJ_OS,              (cell)FACTOR_OS_STRING},
     {OBJ_VM_COMPILE_TIME, (cell)FACTOR_COMPILE_TIME},
     {OBJ_VM_COMPILER,     (cell)FACTOR_COMPILER_VERSION},
index f4b23110970e6bbdc3e4a5784eae4d4fbef1547e..4e408df73aab61e998f6f10bbf95c721e7256a1d 100644 (file)
@@ -43,9 +43,12 @@ vm_parameters::vm_parameters() {
   callback_size = 256;
 }
 
-void vm_parameters::init_from_args(int argc, vm_char** argv) {
-  executable_path = argv[0];
+vm_parameters::~vm_parameters() {
+  free((vm_char *)image_path);
+  free((vm_char *)executable_path);
+}
 
+void vm_parameters::init_from_args(int argc, vm_char** argv) {
   int i = 0;
 
   for (i = 1; i < argc; i++) {
@@ -73,8 +76,13 @@ void vm_parameters::init_from_args(int argc, vm_char** argv) {
       ;
     else if (factor_arg(arg, STRING_LITERAL("-callbacks=%d"), &callback_size))
       ;
-    else if (STRNCMP(arg, STRING_LITERAL("-i="), 3) == 0)
-      image_path = arg + 3;
+    else if (STRNCMP(arg, STRING_LITERAL("-i="), 3) == 0) {
+      /* In case you specify -i more than once. */
+      if (image_path) {
+        free((vm_char *)image_path);
+      }
+      image_path = safe_strdup(arg + 3);
+    }
     else if (STRCMP(arg, STRING_LITERAL("-fep")) == 0)
       fep = true;
     else if (STRCMP(arg, STRING_LITERAL("-nosignals")) == 0)
@@ -255,9 +263,6 @@ void factor_vm::load_image(vm_parameters* p) {
   cell data_offset = data->tenured->start - h.data_relocation_base;
   cell code_offset = code->allocator->start - h.code_relocation_base;
   fixup_heaps(data_offset, code_offset);
-
-  /* Store image path name */
-  special_objects[OBJ_IMAGE] = allot_alien(false_object, (cell)p->image_path);
 }
 
 /* Save the current image to disk. We don't throw any exceptions here
index 36709b2ec152c9ce4849e29edc178712e0030ff8..a4e9e7d0c5a87d009f61648bcfa293ddada1824e 100644 (file)
@@ -45,6 +45,7 @@ struct vm_parameters {
   cell callback_size;
 
   vm_parameters();
+  ~vm_parameters();
   void init_from_args(int argc, vm_char** argv);
 };
 
index 21b9304e40d25753a6b4cfd6c78f028a9419ea10..4fb857b296ee7c5c6fab10a3e82c55af9d047b0f 100644 (file)
@@ -12,15 +12,15 @@ void early_init() {}
 #define SUFFIX ".image"
 #define SUFFIX_LEN 6
 
-/* You must delete[] the result yourself. */
+/* You must free() the result yourself. */
 const char* default_image_path() {
   const char* path = vm_executable_path();
 
   if (!path)
-    return "factor.image";
+    return strdup("factor.image");
 
   int len = strlen(path);
-  char* new_path = new char[len + SUFFIX_LEN + 1];
+  char* new_path = (char *)malloc(len + SUFFIX_LEN + 1);
   memcpy(new_path, path, len);
   memcpy(new_path + len, SUFFIX, SUFFIX_LEN + 1);
   free(const_cast<char*>(path));