add a flag 'type of the union' to safely free model->param[i]->element,

if it contain a malloced string
This commit is contained in:
Holger Vogt 2018-08-28 21:29:05 +02:00
parent f6cbaea604
commit 800c9711f2
4 changed files with 22 additions and 2 deletions

View File

@ -9,6 +9,8 @@ option trtol=1
tran 100p 20n
*plot i(Vs) i(Vs2)
plot v(in1)
tran 100p 20n
plot v(in1)
rusage
.endc
*

View File

@ -328,6 +328,7 @@ typedef struct Mif_Param_Data_s {
Mif_Boolean_t is_null; /* True if no value given on .model card */
int size; /* Size of array (1 if scalar) */
Mif_Value_t *element; /* Value of parameter(s) */
int eltype; /* type of the element */
} Mif_Param_Data_t;

View File

@ -64,12 +64,19 @@ int
MIFmDelete(GENmodel *gen_model)
{
MIFmodel *model = (MIFmodel *) gen_model;
int i;
int i, j;
/* Free the model params stuff allocated in MIFget_mod */
for (i = 0; i < model->num_param; i++) {
if (model->param[i]->element)
/* delete content of union 'element' if it contains a string */
if (model->param[i]->element) {
if (model->param[i]->eltype == IF_STRING)
FREE(model->param[i]->element[0].svalue);
else if (model->param[i]->eltype == IF_STRINGVEC)
for (j = 0; j < model->param[i]->size; j++)
FREE(model->param[i]->element[j].svalue);
FREE(model->param[i]->element);
}
FREE(model->param[i]);
}
FREE(model->param);

View File

@ -129,14 +129,17 @@ int MIFmParam(
case IF_FLAG:
model->param[param_index]->element[0].bvalue = value->iValue;
model->param[param_index]->eltype = IF_FLAG;
break;
case IF_INTEGER:
model->param[param_index]->element[0].ivalue = value->iValue;
model->param[param_index]->eltype = IF_INTEGER;
break;
case IF_REAL:
model->param[param_index]->element[0].rvalue = value->rValue;
model->param[param_index]->eltype = IF_REAL;
break;
case IF_STRING:
@ -144,6 +147,7 @@ int MIFmParam(
model->param[param_index]->element[0].svalue =
TMALLOC(char, 1 + strlen(value->sValue));
strcpy(model->param[param_index]->element[0].svalue, value->sValue);
model->param[param_index]->eltype = IF_STRING;
break;
case IF_COMPLEX:
@ -151,6 +155,7 @@ int MIFmParam(
/* so copy the real and imaginary parts explicitly */
model->param[param_index]->element[0].cvalue.real = value->cValue.real;
model->param[param_index]->element[0].cvalue.imag = value->cValue.imag;
model->param[param_index]->eltype = IF_COMPLEX;
break;
default:
@ -166,14 +171,17 @@ int MIFmParam(
case IF_FLAGVEC:
model->param[param_index]->element[i].bvalue = value->v.vec.iVec[i];
model->param[param_index]->eltype = IF_FLAGVEC;
break;
case IF_INTVEC:
model->param[param_index]->element[i].ivalue = value->v.vec.iVec[i];
model->param[param_index]->eltype = IF_INTVEC;
break;
case IF_REALVEC:
model->param[param_index]->element[i].rvalue = value->v.vec.rVec[i];
model->param[param_index]->eltype = IF_REALVEC;
break;
case IF_STRINGVEC:
@ -181,6 +189,7 @@ int MIFmParam(
model->param[param_index]->element[i].svalue =
TMALLOC(char, 1 + strlen(value->v.vec.sVec[i]));
strcpy(model->param[param_index]->element[i].svalue, value->v.vec.sVec[i]);
model->param[param_index]->eltype = IF_STRINGVEC;
break;
case IF_CPLXVEC:
@ -188,6 +197,7 @@ int MIFmParam(
/* so copy the real and imaginary parts explicitly */
model->param[param_index]->element[i].cvalue.real = value->v.vec.cVec[i].real;
model->param[param_index]->element[i].cvalue.imag = value->v.vec.cVec[i].imag;
model->param[param_index]->eltype = IF_CPLXVEC;
break;
default: