From b02b58fb1969f8209957fcce31172296529e5b26 Mon Sep 17 00:00:00 2001 From: jangko Date: Mon, 6 May 2024 17:14:08 +0700 Subject: [PATCH] nim-bindings fix: prevent Nim GC call free_trusted_setup if it already called manually --- bindings/nim/kzg.nim | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/bindings/nim/kzg.nim b/bindings/nim/kzg.nim index 41709d9f6..eb892b6c5 100644 --- a/bindings/nim/kzg.nim +++ b/bindings/nim/kzg.nim @@ -13,6 +13,7 @@ export type KzgCtx* = ref object + valFreed: bool val: KzgSettings KzgProofAndY* = object @@ -32,7 +33,12 @@ else: ############################################################## proc destroy*(x: KzgCtx) = - free_trusted_setup(x.val) + # Prevent Nim GC to call free_trusted_setup + # if user already done it before. + # Otherwise, the program will crash with segfault. + if not x.valFreed: + free_trusted_setup(x.val) + x.valFreed = true proc newKzgCtx(): KzgCtx = # Nim finalizer is still broken(v1.6) @@ -209,8 +215,8 @@ template loadTrustedSetupFile*(input: File | string): untyped = loadTrustedSetup(input) template freeTrustedSetup*(ctx: KzgCtx) = - free_trusted_setup(ctx.val) - + destroy(ctx) + template blobToKzgCommitment*(ctx: KzgCtx, blob: KzgBlob): untyped = toCommitment(ctx, blob)