🐛 fix(jieba): 修复内存释放问题#131
Merged
yanyiwu merged 2 commits intoyanyiwu:masterfrom Jun 4, 2025
Merged
Conversation
Contributor
kevinmatthe
commented
Jun 4, 2025
- 添加原子操作确保只释放一次内存
- 优化导入包顺序
- 添加freed标志位防止重复释放
- 添加原子操作确保只释放一次内存 - 优化导入包顺序 - 添加freed标志位防止重复释放
- 新增TestTypicalDoubleFree测试函数 - 使用runtime.GC()调用垃圾回收器以运行finalizers - 确保jieba实例正确释放
Contributor
Author
|
经过充分的调查可以明确,Double Free的问题来源于下面的情况: 此处的Finalizer将会为GC注册Jieba Object的回收函数 Lines 58 to 59 in 51c61c7 而在Free的实现中,没有校验CGO对象是否已经被释放过: Lines 63 to 65 in 51c61c7 所以,在典型的使用场景下: jieba := gojieba.NewJieba()
defer jieba.Free()在函数退出时,CGO 对象的释放操作会被defer执行。然而,Go 的垃圾回收器(GC)具有非确定性的触发时机。当 GC 在对象已被手动释放之后再次尝试回收该对象时,CGO 层会调用底层 C++ 逻辑对已释放的内存执行重复的 delete 操作,进而访问无效内存地址,导致段错误(segmentation fault),最终引发程序以 Fatal 异常方式崩溃。 可以通过手动触发运行时的GC,来复现这一典型问题: x := NewJieba()
defer x.Free()
runtime.GC() // call GC to run finalizers上面的代码可以稳定的触发段错误异常,导致程序退出。修复前导致的退出现场,可见测试PR:#132 同时该项测试也提交到了当前PR中,用于验证改动修复的有效性。 |
Contributor
Author
Contributor
Author
|
@yanyiwu Please review this |
Contributor
Author
|
Maybe create a release for the changes since 2024? |
Closed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.