withAimuxCError<T> function

T withAimuxCError<T>(
  1. T fn(
    1. Pointer<AimuxCError> err
    )
)

Allocate a cleared AimuxCError, run fn, free the engine-allocated message (if any) and the struct itself.

Typical use:

final handle = withAimuxCError((err) {
  final h = ffi.openaiNew(key, id, err);
  if (h == 0) throw AimuxException.fromC(err.ref);
  return h;
});

Implementation

T withAimuxCError<T>(T Function(Pointer<AimuxCError> err) fn) {
  final err = calloc<AimuxCError>();
  try {
    clearAimuxCError(err);
    return fn(err);
  } finally {
    final msg = err.ref.message;
    if (msg != nullptr) aimuxFreeString(msg);
    final ev = err.ref.errorValue;
    if (ev != nullptr) aimuxFreeString(ev);
    calloc.free(err);
  }
}