Conversation
|
Hi @Gchuchu ❤️ 感谢你的贡献!我们将在最少半小时,最多5天内阅读此 PR 并回复你 |
There was a problem hiding this comment.
Pull request overview
This PR introduces a new memory-pool allocator intended to back lib/xy.h’s allocation utilities, and begins migrating existing code to use the new allocator semantics.
Changes:
- Added a new
lib/mempool.himplementation and included it fromlib/xy.h. - Switched many
xy.hallocations/frees frommalloc/free/realloc/calloctomp_*APIs. - Adjusted some call sites (e.g., fixed an incorrect
xy_strcatargument count) and temporarily commented out a few frees incore.c.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 8 comments.
| File | Description |
|---|---|
| src/framework/core.c | Comments out several frees (URL/tmpfile) in code paths that allocate dynamically. |
| src/chsrc-main.c | Fixes xy_strcat varargs count for the default-scope message. |
| lib/xy.h | Routes core allocation helpers and various frees through the new mempool API. |
| lib/mempool.h | Adds the new mempool implementation and auto-init/finalize hooks. |
You can also share your feedback on Copilot code review. Take the survey.
|
|
||
| /* 释放 url 内存 */ | ||
| if (url) free (url); | ||
| // if (url) free (url); |
| remove (tmpfile); | ||
| free (tmpfile); /* 释放 tmpfile 路径内存 */ | ||
| // free (tmpfile); /* 释放 tmpfile 路径内存 */ |
| chsrc_run (cmd, RunOpt_Dont_Abort_On_Failure); | ||
| remove (tmpfile); | ||
| free (tmpfile); | ||
| // free (tmpfile); |
| chsrc_run (cmd, RunOpt_Dont_Abort_On_Failure); | ||
| remove (tmpfile); | ||
| free (tmpfile); | ||
| // free (tmpfile); |
| chsrc_run (cmd, RunOpt_Dont_Abort_On_Failure); | ||
| remove (tmpfile); | ||
| free (tmpfile); | ||
| // free (tmpfile); |
| #define MEM_POOL | ||
|
|
||
|
|
||
| #include <stddef.h> |
| static MemPool *_mp_create (size_t initial_size); | ||
| static void *_mp_malloc (MemPool *pool, size_t size); | ||
| static void *_mp_calloc (MemPool *pool, size_t nmemb, size_t size); | ||
| static void *_mp_realloc(MemPool *pool, void *ptr, size_t new_size); | ||
| static void _mp_free (MemPool *pool, void *ptr); | ||
| static void _mp_destroy(MemPool *pool); |
| __attribute__((constructor)) | ||
| void | ||
| mp_initialize (size_t size) | ||
| { | ||
| MemPool *pool = _mp_create(size); | ||
| global_pool = pool; | ||
| } |
|
👍👍👍 我的疑惑是:
#300 已经修过一次了 (
|
对于我上个评论的回复
代码通过使用 属性的使用问题
逻辑上的泄漏依然没有消除这样存在的问题是:实际上逻辑上存在的代码泄漏没有修复,只不过现在有 mempool 来兜底释放,让 抛开“命令行程序”这个程序范畴,内存管理库,从其功能本身来看,整体释放的时机也不应该是在 引入 mempool 带来的真正价值没有体现接上一条,真正需要让内存不泄露的场景应该是:长时间运行的程序,内存不会一直无限增长。如果 |
|
所以我的建议是: 找到 |
但是说实话,这种玩意漏就漏了,咱又不是嵌入式,相信搞编程需要设置环境的电脑内存肯定>4G……并且理论上来说chsrc(截至目前)也不会长时间地运行并调用这种神奇东西,就算让它漏还能漏几个点? |
|
@Mikachu2333 新提交了一个 #344 ,修复了一部分代码泄漏的部分。但是像 为了达到这个效果,mempool 的设计理念和API可能要调整,应该叫做 |
Mikachu2333
left a comment
There was a problem hiding this comment.
这个地方还是请 ccmywish 大佬详细审吧,我本人非科班出身,且对c的研究仅为初学者水平,确实无力提供更多意见。
|
从 因此我认为不需要 引入 但是PR中提到的
我觉得是有必要的,不过建议新开一个 PR 进行处理。感谢你的贡献 |
针对仍然潜在的内存泄漏点:
针对mempool针对
针对
针对
针对具体对xy.h中函数的分析结果:
char *buf = xy_malloc0 (64);
sprintf (buf, "%.2f %s", speed, scale[i]);
|

问题描述
方案与实现
详细描述针对该问题或功能改进的解决方案
针对问题1,采用了内存池的方案,具体来说
1.1. 在lib中添加mempool.h并提供实现和接口,用于xy.h
1.2. 在lib中修改xy.h中分配和释放内存的接口
1.3. 通过
valgrind --tool=memcheck --leak-check=full ./chsrc-debug get ubuntu进行测试,修改后无内存泄露。但是,减少了内存分配次数,些许增加了内存的使用量(25KB->31KB)注:mempool.h提供了通用的接口,不仅限于xy.h
针对问题2,修改了一处xy_strcat的使用错误,具体来说
2.1. cli_print_target_features函数中的倒数第二处xy_strcat参数量填写错误,修改后正确运行