VM: add destructor to vm_parameters so that executable and image_path is

free'd

Valgrind complained that those were leaking
locals-and-roots
Björn Lindqvist 2016-05-13 23:12:16 +02:00
parent 9555915e7b
commit 7342d97e6d
4 changed files with 20 additions and 16 deletions

View File

@ -56,15 +56,12 @@ void factor_vm::init_factor(vm_parameters* p) {
/* OS-specific initialization */ /* OS-specific initialization */
early_init(); early_init();
const vm_char* executable_path = vm_executable_path(); p->executable_path = vm_executable_path();
if (executable_path)
p->executable_path = executable_path;
if (p->image_path == NULL) { if (p->image_path == NULL) {
if (embedded_image_p()) { if (embedded_image_p()) {
p->embedded_image = true; p->embedded_image = true;
p->image_path = p->executable_path; p->image_path = safe_strdup(p->executable_path);
} else } else
p->image_path = default_image_path(); p->image_path = default_image_path();
} }
@ -82,7 +79,8 @@ void factor_vm::init_factor(vm_parameters* p) {
cell aliens[][2] = { cell aliens[][2] = {
{OBJ_CPU, (cell)FACTOR_CPU_STRING}, {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_OS, (cell)FACTOR_OS_STRING},
{OBJ_VM_COMPILE_TIME, (cell)FACTOR_COMPILE_TIME}, {OBJ_VM_COMPILE_TIME, (cell)FACTOR_COMPILE_TIME},
{OBJ_VM_COMPILER, (cell)FACTOR_COMPILER_VERSION}, {OBJ_VM_COMPILER, (cell)FACTOR_COMPILER_VERSION},

View File

@ -43,9 +43,12 @@ vm_parameters::vm_parameters() {
callback_size = 256; callback_size = 256;
} }
void vm_parameters::init_from_args(int argc, vm_char** argv) { vm_parameters::~vm_parameters() {
executable_path = argv[0]; free((vm_char *)image_path);
free((vm_char *)executable_path);
}
void vm_parameters::init_from_args(int argc, vm_char** argv) {
int i = 0; int i = 0;
for (i = 1; i < argc; i++) { 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 (factor_arg(arg, STRING_LITERAL("-callbacks=%d"), &callback_size))
; ;
else if (STRNCMP(arg, STRING_LITERAL("-i="), 3) == 0) else if (STRNCMP(arg, STRING_LITERAL("-i="), 3) == 0) {
image_path = arg + 3; /* 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) else if (STRCMP(arg, STRING_LITERAL("-fep")) == 0)
fep = true; fep = true;
else if (STRCMP(arg, STRING_LITERAL("-nosignals")) == 0) 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 data_offset = data->tenured->start - h.data_relocation_base;
cell code_offset = code->allocator->start - h.code_relocation_base; cell code_offset = code->allocator->start - h.code_relocation_base;
fixup_heaps(data_offset, code_offset); 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 /* Save the current image to disk. We don't throw any exceptions here

View File

@ -45,6 +45,7 @@ struct vm_parameters {
cell callback_size; cell callback_size;
vm_parameters(); vm_parameters();
~vm_parameters();
void init_from_args(int argc, vm_char** argv); void init_from_args(int argc, vm_char** argv);
}; };

View File

@ -12,15 +12,15 @@ void early_init() {}
#define SUFFIX ".image" #define SUFFIX ".image"
#define SUFFIX_LEN 6 #define SUFFIX_LEN 6
/* You must delete[] the result yourself. */ /* You must free() the result yourself. */
const char* default_image_path() { const char* default_image_path() {
const char* path = vm_executable_path(); const char* path = vm_executable_path();
if (!path) if (!path)
return "factor.image"; return strdup("factor.image");
int len = strlen(path); 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, path, len);
memcpy(new_path + len, SUFFIX, SUFFIX_LEN + 1); memcpy(new_path + len, SUFFIX, SUFFIX_LEN + 1);
free(const_cast<char*>(path)); free(const_cast<char*>(path));