diff --git a/templates/sys.ts b/templates/sys.ts index cc0ca1a..3423966 100644 --- a/templates/sys.ts +++ b/templates/sys.ts @@ -356,12 +356,22 @@ export class UniffiRustBufferValue { throw new Error(`Error converting rust buffer to uint8array - rust buffer length is ${this.struct.len}, which cannot be represented as a Number safely.`) } - const [contents] = restorePointer({ - retType: [arrayConstructor({ type: DataType.U8Array, length: Number(this.struct.len) })], - paramsValue: wrapPointer([this.struct.data]), - }); - - return new Uint8Array(contents); + const length = Number(this.struct.len); + const wrapped = wrapPointer([this.struct.data]); + try { + const [contents] = restorePointer({ + retType: [arrayConstructor({ type: DataType.U8Array, length })], + paramsValue: wrapped, + }); + + return new Uint8Array(contents); + } finally { + freePointer({ + paramsType: [arrayConstructor({ type: DataType.U8Array, length })], + paramsValue: wrapped, + pointerType: PointerType.RsPointer, + }); + } } consumeIntoUint8Array() {