Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tb_sort_all 函数排序发生错误 #187

Closed
huye opened this issue Jun 14, 2022 · 5 comments
Closed

tb_sort_all 函数排序发生错误 #187

huye opened this issue Jun 14, 2022 · 5 comments

Comments

@huye
Copy link

huye commented Jun 14, 2022

注:提问题时若使用不能用/没效果/有问题/报错此类模糊表达,但又没有根据下面的模板给出任何相关辅助信息的,将绝对不会有任何反馈。

描述问题

我使用 lldb 调试发现是在快排函数内部如下代码时 l 的值为0引起
// key => hole tb_iterator_copy(iterator, l, key);
然后导致复制的内存重叠(buff == data)
// copy element tb_memcpy(buff, data, element->size);
我测试时,随机生成的数据如下:
item: 1804289383, 846930886
item: 1681692777, 1714636915
item: 1957747793, 424238335
item: 719885386, 1649760492
item: 596516649, 1189641421
item: 1025202362, 1350490027
item: 783368690, 1102520059
item: 2044897763, 1967513926
item: 1365180540, 1540383426

期待的结果

在之前调试的某个版本中有成功执行,但是排序的结果也是不正常的,如上面的数据排序后的结果成了
item: 596516649, 1189641421
item: 596516649, 1189641421
item: 719885386, 1649760492
item: 719885386, 1649760492
item: 783368690, 1102520059
item: 1365180540, 1540383426
item: 1681692777, 1714636915
item: 1681692777, 1714636915
item: 2044897763, 1967513926
这个排序功能应该有好几处错误。

错误信息

[tbox]: [memcpy]: [overlap]: [0x10330c768, 8] => [0x10330c768, 8]
[tbox]: [backtrace]: [memcpy]: [overlap]: [0x0000010311105c]: 0 tbox 0x000000010311105c tb_memcpy + 572
[tbox]: [backtrace]: [memcpy]: [overlap]: [0x000001031605b7]: 1 tbox 0x00000001031605b7 tb_element_mem_copy + 151
[tbox]: [backtrace]: [memcpy]: [overlap]: [0x0000010315d5ed]: 2 tbox 0x000000010315d5ed tb_vector_itor_copy + 125
[tbox]: [backtrace]: [memcpy]: [overlap]: [0x00000103163560]: 3 tbox 0x0000000103163560 tb_iterator_copy + 144
[tbox]: [backtrace]: [memcpy]: [overlap]: [0x0000010315a649]: 4 tbox 0x000000010315a649 tb_quick_sort + 809
[tbox]: [backtrace]: [memcpy]: [overlap]: [0x0000010315a65e]: 5 tbox 0x000000010315a65e tb_quick_sort + 830
[tbox]: [backtrace]: [memcpy]: [overlap]: [0x0000010315a65e]: 6 tbox 0x000000010315a65e tb_quick_sort + 830
[tbox]: [backtrace]: [memcpy]: [overlap]: [0x00000103159a41]: 7 tbox 0x0000000103159a41 tb_sort + 257
[tbox]: [backtrace]: [memcpy]: [overlap]: [0x00000103159ba2]: 8 tbox 0x0000000103159ba2 tb_sort_all + 66
[tbox]: [backtrace]: [memcpy]: [overlap]: [0x0000010310858d]: 9 tbox 0x000000010310858d main + 701
[tbox]: [malloc]: [from]: data: from: tb_vector_init(): 232, src/tbox/container/vector.c
[tbox]: [backtrace]: [malloc]: [from]: [0x000001031230ee]: 0 tbox 0x00000001031230ee tb_allocator_nalloc0_ + 110
[tbox]: [backtrace]: [malloc]: [from]: [0x0000010315d047]: 1 tbox 0x000000010315d047 tb_vector_init + 519
[tbox]: [backtrace]: [malloc]: [from]: [0x0000010310839a]: 2 tbox 0x000000010310839a main + 202
[tbox]: [backtrace]: [malloc]: [from]: [0x00000112aa951e]: 3 dyld 0x0000000112aa951e start + 462

相关环境

[tbox]: version: tbox_v1_6_8_202206142225_x86_64_sse3_mach by Apple LLVM 13.1.6 (clang-1316.0.21.2.3)

其他信息

我测试的完整代码
`
/* //////////////////////////////////////////////////////////////////////////////////////

  • includes
    */
    #include "tbox/tbox.h"

typedef struct rmap {
int one;
int two;
} rmap_s, *rmap_t;

static tb_void_t map_free(tb_element_ref_t element, tb_pointer_t buff) {
// 不作数据清除,减少资源浪费
}

static tb_long_t map_comp(tb_vector_ref_t vt, tb_cpointer_t v, tb_cpointer_t t) {
return ((rmap_t)v)->one - ((rmap_t)t)->one;
}

/* //////////////////////////////////////////////////////////////////////////////////////

  • main
    /
    tb_int_t main(tb_int_t argc, tb_char_t
    * argv) {
    // init tbox
    if (!tb_init(tb_null, tb_null)) return -1;

    // trace
    tb_trace_i("hello tbox!");

    rmap_s map;

    tb_vector_ref_t vec = tb_vector_init(128, tb_element_mem(sizeof map, map_free, 0));

    tb_vector_clear(vec);

    for (int i = 0; i < 9; i++) {
    map.one = tb_random();
    map.two = tb_random();
    tb_vector_insert_tail(vec, &map);
    }

    {
    tb_for_all(rmap_t, it, vec) {
    tb_printf("item: %d, %d\n", it->one, it->two);
    }
    }

    tb_trace_i("sort");
    tb_sort_all(vec, map_comp);

    {
    tb_for_all(rmap_t, it, vec) {
    tb_printf("item: %d, %d\n", it->one, it->two);
    }
    }

    // exit tbox
    tb_exit();
    return 0;
    }
    `

@waruqi
Copy link
Member

waruqi commented Jun 16, 2022

我大概知道问题原因,但是这个目前不太好改,改起来比较费时间,你可以先确保 tb_element_mem 的 map 类型 size > 8 ,就行了。。比如在塞个 int 到 rmap_t 里面去

@waruqi
Copy link
Member

waruqi commented Jun 16, 2022

我修复了,更新到 dev 分支试下

@waruqi
Copy link
Member

waruqi commented Jun 17, 2022

可以了么?

@huye
Copy link
Author

huye commented Jun 17, 2022

可以了,谢谢老大啊。

就是在更新包代码的时候不知道如何正确操作,我 xmake c,xmake f -c, xmake f --ccache=n 都执行过了,好像都更新不了。最后我是手动删除了 ~/.xmake 再编译再更新到了 tbox 包。是我操作不对吗,老大指导一下

@waruqi
Copy link
Member

waruqi commented Jun 17, 2022

master 版本,不会每次更新,除非你 xrepo remote --all tbox 删掉已经装的版本

@waruqi waruqi closed this as completed Jun 17, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants