oven-sh/bun · error · Error

Memory lifetime is ambiguous when returning UTF8String

Error message

Memory lifetime is ambiguous when returning UTF8String

What it means

Guard in the bindgen out-parameter code path: the variant's return type is UTF8String, which the generator cannot emit because it is unclear who owns and frees the C-string after conversion to a JS value. It is a deliberate unsupported-combination sentinel, not a runtime failure — the .bind.ts declaration chose a return kind with no defined ownership transfer semantics.

Solutions

  1. Change the variant's return type to a kind with explicit ownership (e.g. a JS-owned string type the generator supports)
  2. Add a codegen arm for UTF8String out-params that copies the string into a JS value and defines the free strategy
  3. Return a wrapper object whose finalizer owns the memory instead of a raw UTF8String
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at src/codegen/bindgen.ts:391 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of oven-sh/bun@8c5296ac45 (2026-08-16). Data as JSON: /api/errors/85dfed09c1d63ed0. Report an issue: GitHub.

Appendix: source

Thrown at src/codegen/bindgen.ts:391

      cpp.line("}");
      cpp.line("return JSC::JSValue::encode(JSC::jsUndefined());");
      break;
    case "basic-out-param":
      addCommaAfterArgument();
      cpp.add("&out");
      cpp.line();
      cpp.dedent();
      cpp.line(")) {");
      cpp.line(`    return {};`);
      cpp.line("}");
      const simpleType = getSimpleIdlType(variant.ret);
      if (simpleType) {
        cpp.line(`return JSC::JSValue::encode(WebCore::toJS<${simpleType}>(*global, out));`);
        break;
      }
      switch (variant.ret.kind) {
        case "UTF8String":
          throw new Error("Memory lifetime is ambiguous when returning UTF8String");
        case "DOMString":
        case "USVString":
        case "ByteString":
          cpp.line(
            `return JSC::JSValue::encode(WebCore::toJS<WebCore::IDL${variant.ret.kind}>(*global, out.transferToWTFString()));`,
          );
          break;
      }
      break;
    default:
      throw new Error(`TODO: emitCppCallToVariant for ${inspect(returnStrategy)}`);
  }
}

/** If a simple IDL type mapping exists, it also currently means there is a direct C ABI mapping */
function getSimpleIdlType(type: TypeImpl): string | undefined {
  const map: { [K in TypeKind]?: string } = {
    boolean: "WebCore::IDLBoolean",

View on GitHub (pinned to 8c5296ac45)