diff --git a/interface/ceed-fortran.c b/interface/ceed-fortran.c index d3ed061c3e..501d901c2e 100644 --- a/interface/ceed-fortran.c +++ b/interface/ceed-fortran.c @@ -845,6 +845,8 @@ CEED_EXTERN void fCeedQFunctionSetContext(int *qf, int *ctx, int *err) { if (*err) return; fctxdata->inner_ctx = ctx_; *err = CeedQFunctionContextRestoreData(fctx, (void **)&fctxdata); + if (*err) return; + *err = CeedQFunctionContextDestroy(&fctx); } #define fCeedQFunctionView FORTRAN_NAME(ceedqfunctionview, CEEDQFUNCTIONVIEW) diff --git a/interface/ceed-operator.c b/interface/ceed-operator.c index 3d6f6dd004..f77938e1e4 100644 --- a/interface/ceed-operator.c +++ b/interface/ceed-operator.c @@ -370,26 +370,22 @@ static int CeedOperatorContextSetGeneric(CeedOperator op, CeedContextFieldLabel "Composite operator modified after ContextFieldLabel created"); for (CeedInt i = 0; i < num_sub; i++) { - CeedQFunction qf; CeedQFunctionContext ctx; - CeedCall(CeedOperatorGetQFunction(sub_operators[i], &qf)); - CeedCall(CeedQFunctionGetContext(qf, &ctx)); - CeedCall(CeedQFunctionDestroy(&qf)); + CeedCall(CeedOperatorGetContext(sub_operators[i], &ctx)); // Try every sub-operator, ok if some sub-operators do not have field - if (field_label->sub_labels[i] && ctx) { + if (ctx && field_label->sub_labels[i]) { CeedCall(CeedQFunctionContextSetGeneric(ctx, field_label->sub_labels[i], field_type, values)); } + CeedCall(CeedQFunctionContextDestroy(&ctx)); } } else { - CeedQFunction qf; CeedQFunctionContext ctx; - CeedCall(CeedOperatorGetQFunction(op, &qf)); - CeedCall(CeedQFunctionGetContext(qf, &ctx)); - CeedCall(CeedQFunctionDestroy(&qf)); + CeedCall(CeedOperatorGetContext(op, &ctx)); CeedCheck(ctx, CeedOperatorReturnCeed(op), CEED_ERROR_UNSUPPORTED, "QFunction does not have context data"); CeedCall(CeedQFunctionContextSetGeneric(ctx, field_label, field_type, values)); + CeedCall(CeedQFunctionContextDestroy(&ctx)); } CeedCall(CeedOperatorSetQFunctionAssemblyDataUpdateNeeded(op, true)); return CEED_ERROR_SUCCESS; @@ -441,27 +437,24 @@ static int CeedOperatorContextGetGenericRead(CeedOperator op, CeedContextFieldLa "Composite operator modified after ContextFieldLabel created"); for (CeedInt i = 0; i < num_sub; i++) { - CeedQFunction qf; CeedQFunctionContext ctx; - CeedCall(CeedOperatorGetQFunction(sub_operators[i], &qf)); - CeedCall(CeedQFunctionGetContext(qf, &ctx)); - CeedCall(CeedQFunctionDestroy(&qf)); + CeedCall(CeedOperatorGetContext(sub_operators[i], &ctx)); // Try every sub-operator, ok if some sub-operators do not have field - if (field_label->sub_labels[i] && ctx) { + if (ctx && field_label->sub_labels[i]) { CeedCall(CeedQFunctionContextGetGenericRead(ctx, field_label->sub_labels[i], field_type, num_values, values)); + CeedCall(CeedQFunctionContextDestroy(&ctx)); return CEED_ERROR_SUCCESS; } + CeedCall(CeedQFunctionContextDestroy(&ctx)); } } else { - CeedQFunction qf; CeedQFunctionContext ctx; - CeedCall(CeedOperatorGetQFunction(op, &qf)); - CeedCall(CeedQFunctionGetContext(qf, &ctx)); - CeedCall(CeedQFunctionDestroy(&qf)); + CeedCall(CeedOperatorGetContext(op, &ctx)); CeedCheck(ctx, CeedOperatorReturnCeed(op), CEED_ERROR_UNSUPPORTED, "QFunction does not have context data"); CeedCall(CeedQFunctionContextGetGenericRead(ctx, field_label, field_type, num_values, values)); + CeedCall(CeedQFunctionContextDestroy(&ctx)); } return CEED_ERROR_SUCCESS; } @@ -507,27 +500,24 @@ static int CeedOperatorContextRestoreGenericRead(CeedOperator op, CeedContextFie "Composite operator modified after ContextFieldLabel created"); for (CeedInt i = 0; i < num_sub; i++) { - CeedQFunction qf; CeedQFunctionContext ctx; - CeedCall(CeedOperatorGetQFunction(sub_operators[i], &qf)); - CeedCall(CeedQFunctionGetContext(qf, &ctx)); - CeedCall(CeedQFunctionDestroy(&qf)); + CeedCall(CeedOperatorGetContext(sub_operators[i], &ctx)); // Try every sub-operator, ok if some sub-operators do not have field - if (field_label->sub_labels[i] && ctx) { + if (ctx && field_label->sub_labels[i]) { CeedCall(CeedQFunctionContextRestoreGenericRead(ctx, field_label->sub_labels[i], field_type, values)); + CeedCall(CeedQFunctionContextDestroy(&ctx)); return CEED_ERROR_SUCCESS; } + CeedCall(CeedQFunctionContextDestroy(&ctx)); } } else { - CeedQFunction qf; CeedQFunctionContext ctx; - CeedCall(CeedOperatorGetQFunction(op, &qf)); - CeedCall(CeedQFunctionGetContext(qf, &ctx)); - CeedCall(CeedQFunctionDestroy(&qf)); + CeedCall(CeedOperatorGetContext(op, &ctx)); CeedCheck(ctx, CeedOperatorReturnCeed(op), CEED_ERROR_UNSUPPORTED, "QFunction does not have context data"); CeedCall(CeedQFunctionContextRestoreGenericRead(ctx, field_label, field_type, values)); + CeedCall(CeedQFunctionContextDestroy(&ctx)); } return CEED_ERROR_SUCCESS; } diff --git a/interface/ceed-preconditioning.c b/interface/ceed-preconditioning.c index 5535b91661..1cc767cb08 100644 --- a/interface/ceed-preconditioning.c +++ b/interface/ceed-preconditioning.c @@ -71,6 +71,7 @@ static int CeedQFunctionCreateFallback(Ceed fallback_ceed, CeedQFunction qf, Cee CeedCall(CeedQFunctionGetContext(qf, &ctx)); CeedCall(CeedQFunctionSetContext(*qf_fallback, ctx)); + CeedCall(CeedQFunctionContextDestroy(&ctx)); } CeedCall(CeedQFunctionGetFields(qf, &num_input_fields, &input_fields, &num_output_fields, &output_fields)); for (CeedInt i = 0; i < num_input_fields; i++) { diff --git a/interface/ceed-qfunction.c b/interface/ceed-qfunction.c index 0daa4b98fc..6936974fa4 100644 --- a/interface/ceed-qfunction.c +++ b/interface/ceed-qfunction.c @@ -330,7 +330,8 @@ int CeedQFunctionGetUserFunction(CeedQFunction qf, CeedQFunctionUser *f) { @ref Backend **/ int CeedQFunctionGetContext(CeedQFunction qf, CeedQFunctionContext *ctx) { - *ctx = qf->ctx; + *ctx = NULL; + if (qf->ctx) CeedCall(CeedQFunctionContextReferenceCopy(qf->ctx, ctx)); return CEED_ERROR_SUCCESS; } @@ -361,6 +362,7 @@ int CeedQFunctionGetContextData(CeedQFunction qf, CeedMemType mem_type, void *da } else { *(void **)data = NULL; } + CeedCall(CeedQFunctionContextDestroy(&ctx)); return CEED_ERROR_SUCCESS; } @@ -387,7 +389,7 @@ int CeedQFunctionRestoreContextData(CeedQFunction qf, void *data) { CeedCall(CeedQFunctionContextRestoreDataRead(ctx, data)); } } - *(void **)data = NULL; + CeedCall(CeedQFunctionContextDestroy(&ctx)); return CEED_ERROR_SUCCESS; } @@ -415,6 +417,7 @@ int CeedQFunctionGetInnerContext(CeedQFunction qf, CeedQFunctionContext *ctx) { } else { *ctx = qf_ctx; } + CeedCall(CeedQFunctionContextDestroy(&qf_ctx)); return CEED_ERROR_SUCCESS; } @@ -730,6 +733,7 @@ int CeedQFunctionCreateIdentity(Ceed ceed, CeedInt size, CeedEvalMode in_mode, C CeedCall(CeedQFunctionGetContext(*qf, &ctx)); CeedCall(CeedQFunctionContextGetFieldLabel(ctx, "size", &size_label)); CeedCall(CeedQFunctionContextSetInt32(ctx, size_label, &size)); + CeedCall(CeedQFunctionContextDestroy(&ctx)); return CEED_ERROR_SUCCESS; }