]> git.bitcoin.ninja Git - ldk-java/commitdiff
Free constructed LDK strings when relevant.
authorMatt Corallo <git@bluematt.me>
Sun, 18 Jul 2021 02:24:24 +0000 (02:24 +0000)
committerMatt Corallo <git@bluematt.me>
Sun, 18 Jul 2021 03:17:52 +0000 (03:17 +0000)
With LDK C Bindings v0.0.99.2, this reduces memory leaks during tests from:
  538 allocations remained for 1188720 bytes.
to
  358 allocations remained for 1141342 bytes.

gen_type_mapping.py

index cbec2ee98a3fad009297d1aa275d87300fcd0a88..3fb0ba7c919d97a63a79b2c59e6ffbd8119b574c 100644 (file)
@@ -183,10 +183,13 @@ class TypeMappingGenerator:
                     ret_conv_name = ty_info.var_name + "_conv",
                     to_hu_conv = None, to_hu_conv_name = None, from_hu_conv = None)
             else:
+                free_str = ""
+                if not holds_ref:
+                    free_str = "\nStr_free(" + ty_info.var_name + "_str);"
                 return ConvInfo(ty_info = ty_info, arg_name = ty_info.var_name,
                     arg_conv = arg_conv, arg_conv_name = arg_conv_name, arg_conv_cleanup = None,
                     ret_conv = ("LDKStr " + ty_info.var_name + "_str = ",
-                        ";\njstring " + ty_info.var_name + "_conv = " + self.consts.str_ref_to_native_call(ty_info.var_name + "_str." + ty_info.arr_access, ty_info.var_name + "_str." + ty_info.arr_len) + ";"),
+                        ";\njstring " + ty_info.var_name + "_conv = " + self.consts.str_ref_to_native_call(ty_info.var_name + "_str." + ty_info.arr_access, ty_info.var_name + "_str." + ty_info.arr_len) + ";" + free_str),
                     ret_conv_name = ty_info.var_name + "_conv", to_hu_conv = None, to_hu_conv_name = None, from_hu_conv = None)
         elif ty_info.var_name == "" and not print_void:
             # We don't have a parameter name, and want one, just call it arg