forked from ground/ground
Fix segfaults returning objects from extlibs
This commit is contained in:
24
src/types.c
24
src/types.c
@@ -128,7 +128,6 @@ GroundValue copyGroundValue(const GroundValue* gv) {
|
||||
break;
|
||||
}
|
||||
case CUSTOM: {
|
||||
newGv.customType = gv->customType;
|
||||
newGv.data.customVal = malloc(sizeof(GroundObject));
|
||||
if (newGv.data.customVal == NULL) {
|
||||
printf("Couldn't allocate memory for GroundObject copy\n");
|
||||
@@ -137,6 +136,15 @@ GroundValue copyGroundValue(const GroundValue* gv) {
|
||||
|
||||
newGv.data.customVal->fields = NULL;
|
||||
|
||||
// Deep copy the struct definition as well
|
||||
newGv.customType = malloc(sizeof(GroundStruct));
|
||||
newGv.customType->size = gv->customType->size;
|
||||
newGv.customType->fields = malloc(gv->customType->size * sizeof(GroundStructField));
|
||||
for (size_t i = 0; i < gv->customType->size; i++) {
|
||||
strncpy(newGv.customType->fields[i].id, gv->customType->fields[i].id, 64);
|
||||
newGv.customType->fields[i].value = copyGroundValue(&gv->customType->fields[i].value);
|
||||
}
|
||||
|
||||
GroundObjectField *field, *tmp;
|
||||
HASH_ITER(hh, gv->data.customVal->fields, field, tmp) {
|
||||
GroundObjectField* newField = malloc(sizeof(GroundObjectField));
|
||||
@@ -267,6 +275,20 @@ void freeGroundValue(GroundValue* gv) {
|
||||
freeGroundStruct(gstruct);
|
||||
free(gstruct);
|
||||
}
|
||||
if (gv->type == CUSTOM && gv->data.customVal != NULL) {
|
||||
freeGroundObject(gv->data.customVal);
|
||||
free(gv->data.customVal);
|
||||
gv->data.customVal = NULL;
|
||||
|
||||
if (gv->customType != NULL) {
|
||||
for (size_t i = 0; i < gv->customType->size; i++) {
|
||||
freeGroundValue(&gv->customType->fields[i].value);
|
||||
}
|
||||
free(gv->customType->fields);
|
||||
free(gv->customType);
|
||||
gv->customType = NULL;
|
||||
}
|
||||
}
|
||||
if (gv->type == ERROR) {
|
||||
GroundError* error = &gv->data.errorVal;
|
||||
if (error->type != NULL) {
|
||||
|
||||
Reference in New Issue
Block a user