Skip to content

使用zig的内存分配器作为awtk的内存分配器后,内存分配器检测出大量的内存泄露! #944

@ufbycd

Description

@ufbycd

zig的DebugAllocator内存分配器(以前叫GeneralPurposeAllocator)支持内存泄露检测,并且支持泄露产生位置的堆栈回溯。这个特性对于C的内存管理来说,绝对是神器。于是捣鼓了下让awtk使用zig的内存分配器:

首先在awtk/src/mem.c文件添加用于设置外部内存分配器的接口如下:

ret_t mem_set_allocator(mem_allocator_t* allocator) {
  return_value_if_fail(allocator != NULL, RET_BAD_PARAMS);
  s_allocator = allocator;
  return RET_OK;
}

然后在zig源码上实现mem_allocator,见这里:https://gitee.com/ufbycd/awtk-mvvm-zig-example/blob/dev/src/awtk.zig#L118

然后运行awtk-mvvm-zig-example程序,程序退出后分配器打印了大量内存泄露信息,泄露位置集中在fscript_parser和AWTK-MVVM的data_binding。我分析了一两个位置,确实是内存泄露了,比如这个:

error(gpa): memory address 0x7ecfd3f41780 leaked:
/home/chenss/workspace/zlgopen/awtk-mvvm-zig-example/src/awtk.zig:148:43: 0x12ce088 in alloc (awtk-mvvm-zig-example)
const slice = self.allocator.alloc(u8, @intCast(space)) catch return null;
^
src/tkc/mem_allocator.h:74:10: 0x7ecfd717306a in mem_allocator_alloc (src/tkc/mem.c)
src/tkc/mem.c:165:10: 0x7ecfd7174799 in tk_calloc (src/tkc/mem.c)
src/tkc/fscript.c:354:18: 0x7ecfd7161b46 in func_args_init (src/tkc/fscript.c)
src/tkc/fscript.c:3383:7: 0x7ecfd716d3f1 in fscript_func_call_init_func (src/tkc/fscript.c)
src/tkc/fscript.c:3445:3: 0x7ecfd716d711 in fscript_func_call_create (src/tkc/fscript.c)
src/tkc/fscript.c:2238:18: 0x7ecfd7168eec in fscript_load (src/tkc/fscript.c)
src/tkc/fscript.c:2267:10: 0x7ecfd7169142 in fscript_create_ex (src/tkc/fscript.c)

完整的内存泄露输出信息见附件,完整的zig代码实现在awtk-mvvm-zig-example仓库的dev分支:https://gitee.com/ufbycd/awtk-mvvm-zig-example/blob/dev

我只分析了一两个位置,不太确定是否真的有这么多泄露,建议还是仔细分析下。

awtk-memory-leak.txt

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions