diff --git a/vlib/builtin/option.v b/vlib/builtin/option.v index 753314ff61..1c301ac848 100644 --- a/vlib/builtin/option.v +++ b/vlib/builtin/option.v @@ -62,3 +62,27 @@ fn opt_ok(data voidptr, mut option Option, size int) { C.memcpy(byteptr(&option.err) + sizeof(IError), data, size) } } + +[unsafe] +pub fn (e &Error) free() { + unsafe { e.msg.free() } +} + +[unsafe] +pub fn (n &None__) free() { + unsafe { n.msg.free() } +} + +[typedef] +struct C.IError { + _object voidptr +} + +[unsafe] +pub fn (ie &IError) free() { + unsafe { + ie.msg.free() + cie := &C.IError(ie) + C.free(cie._object) + } +} diff --git a/vlib/v/gen/c/cgen.v b/vlib/v/gen/c/cgen.v index 1801883acf..f9d8076273 100644 --- a/vlib/v/gen/c/cgen.v +++ b/vlib/v/gen/c/cgen.v @@ -975,8 +975,9 @@ fn (mut g Gen) stmts_with_tmp_var(stmts []ast.Stmt, tmp_var string) { stmt_pos = stmt.expr.position() } if stmt_pos.pos == 0 { - print('autofree: first stmt pos = 0. ') - println(stmt.type_name()) + $if trace_autofree ? { + println('autofree: first stmt pos = 0. $stmt.type_name()') + } return } } @@ -4849,6 +4850,8 @@ fn (mut g Gen) const_decl_init_later(mod string, name string, val string, typ ta g.cleanups[mod].writeln('\tstring_free(&$cname);') } else if sym.kind == .map { g.cleanups[mod].writeln('\tmap_free(&$cname);') + } else if styp == 'IError' { + g.cleanups[mod].writeln('\tIError_free(&$cname);') } } }