fix(cgo): resolve memory leaks in C string handling

* Fixed memory leaks by ensuring proper deallocation of C-allocated strings in `ime_darwin.go` and `window_darwin.go`.
* Added error handling for nil pointers when retrieving current input methods and active window names.
* Updated language files to include new performance profiling options for memory.
This commit is contained in:
qianlifeng 2025-06-28 23:27:41 +08:00
parent 713ed58fb2
commit f219a2efce
No known key found for this signature in database
9 changed files with 49 additions and 5 deletions

View File

@ -437,7 +437,12 @@ func (a *MacRetriever) getRunningProcesses() (infos []processInfo) {
//only show user process //only show user process
continue continue
} }
appPath := C.GoString(C.get_process_path(pid)) cPath := C.get_process_path(pid)
if cPath == nil {
continue
}
appPath := C.GoString(cPath)
C.free(unsafe.Pointer(cPath))
if appPath == "" { if appPath == "" {
continue continue
} }

View File

@ -140,6 +140,28 @@ func (r *SysPlugin) Init(ctx context.Context, initParams plugin.InitParams) {
}, },
}) })
r.commands = append(r.commands, SysCommand{
Title: "i18n:plugin_sys_performance_memory_profiling",
Icon: plugin.CPUProfileIcon,
Action: func(ctx context.Context, actionContext plugin.ActionContext) {
memoryProfPath := path.Join(util.GetLocation().GetWoxDataDirectory(), "memory.prof")
f, err := os.Create(memoryProfPath)
if err != nil {
util.GetLogger().Info(ctx, "failed to create memory profile file: "+err.Error())
return
}
util.GetLogger().Info(ctx, "start memory profile")
profileErr := pprof.WriteHeapProfile(f)
if profileErr != nil {
util.GetLogger().Info(ctx, "failed to start memory profile: "+profileErr.Error())
return
}
util.GetLogger().Info(ctx, "memory profile saved to "+memoryProfPath)
},
})
r.commands = append(r.commands, SysCommand{ r.commands = append(r.commands, SysCommand{
Title: "i18n:plugin_sys_delete_image_cache", Title: "i18n:plugin_sys_delete_image_cache",
Icon: common.NewWoxImageEmoji("🗑️"), Icon: common.NewWoxImageEmoji("🗑️"),

View File

@ -203,6 +203,7 @@
"plugin_sys_open_wox_settings": "Open Wox Settings", "plugin_sys_open_wox_settings": "Open Wox Settings",
"plugin_sys_open_system_settings": "Open System Settings", "plugin_sys_open_system_settings": "Open System Settings",
"plugin_sys_performance_cpu_profiling": "Performance CPU profiling", "plugin_sys_performance_cpu_profiling": "Performance CPU profiling",
"plugin_sys_performance_memory_profiling": "Performance Memory profiling",
"plugin_sys_delete_image_cache": "Delete all image cache", "plugin_sys_delete_image_cache": "Delete all image cache",
"plugin_sys_open_plugin_settings": "Open %s settings", "plugin_sys_open_plugin_settings": "Open %s settings",
"plugin_websearch_trigger_keyword": "keyword", "plugin_websearch_trigger_keyword": "keyword",

View File

@ -201,6 +201,7 @@
"plugin_sys_open_wox_settings": "Abrir configurações do Wox", "plugin_sys_open_wox_settings": "Abrir configurações do Wox",
"plugin_sys_open_system_settings": "Abrir configurações do sistema", "plugin_sys_open_system_settings": "Abrir configurações do sistema",
"plugin_sys_performance_cpu_profiling": "Perfil de desempenho da CPU", "plugin_sys_performance_cpu_profiling": "Perfil de desempenho da CPU",
"plugin_sys_performance_memory_profiling": "Perfil de desempenho de memória",
"plugin_sys_delete_image_cache": "Excluir todo o cache de imagem", "plugin_sys_delete_image_cache": "Excluir todo o cache de imagem",
"plugin_sys_open_plugin_settings": "Abrir configurações do plugin %s", "plugin_sys_open_plugin_settings": "Abrir configurações do plugin %s",
"plugin_websearch_trigger_keyword": "Palavra-chave", "plugin_websearch_trigger_keyword": "Palavra-chave",

View File

@ -201,6 +201,7 @@
"plugin_sys_open_wox_settings": "Открыть настройки Wox", "plugin_sys_open_wox_settings": "Открыть настройки Wox",
"plugin_sys_open_system_settings": "Открыть системные настройки", "plugin_sys_open_system_settings": "Открыть системные настройки",
"plugin_sys_performance_cpu_profiling": "Профилирование производительности ЦП", "plugin_sys_performance_cpu_profiling": "Профилирование производительности ЦП",
"plugin_sys_performance_memory_profiling": "Профилирование производительности памяти",
"plugin_sys_delete_image_cache": "Удалить весь кэш изображений", "plugin_sys_delete_image_cache": "Удалить весь кэш изображений",
"plugin_sys_open_plugin_settings": "Открыть настройки %s", "plugin_sys_open_plugin_settings": "Открыть настройки %s",
"plugin_websearch_trigger_keyword": "ключевое слово", "plugin_websearch_trigger_keyword": "ключевое слово",

View File

@ -203,6 +203,7 @@
"plugin_sys_open_wox_settings": "打开Wox设置", "plugin_sys_open_wox_settings": "打开Wox设置",
"plugin_sys_open_system_settings": "打开系统设置", "plugin_sys_open_system_settings": "打开系统设置",
"plugin_sys_performance_cpu_profiling": "性能 CPU 分析", "plugin_sys_performance_cpu_profiling": "性能 CPU 分析",
"plugin_sys_performance_memory_profiling": "性能 内存 分析",
"plugin_sys_delete_image_cache": "删除所有图片缓存", "plugin_sys_delete_image_cache": "删除所有图片缓存",
"plugin_sys_open_plugin_settings": "打开 %s 设置", "plugin_sys_open_plugin_settings": "打开 %s 设置",
"plugin_websearch_trigger_keyword": "关键字", "plugin_websearch_trigger_keyword": "关键字",

View File

@ -29,7 +29,14 @@ func SwitchInputMethodABC() error {
errorChan <- err errorChan <- err
}) })
inputMethod := C.GoString(C.getCurrentInputMethod()) // Fix memory leak: properly free the C-allocated string
cInputMethod := C.getCurrentInputMethod()
if cInputMethod == nil {
errorChan <- errors.New("failed to get current input method")
return
}
inputMethod := C.GoString(cInputMethod)
C.free(unsafe.Pointer(cInputMethod))
if inputMethod == "" { if inputMethod == "" {
errorChan <- errors.New("failed to get current input method") errorChan <- errors.New("failed to get current input method")
return return

View File

@ -37,6 +37,10 @@ func GetActiveWindowIcon() (image.Image, error) {
func GetActiveWindowName() string { func GetActiveWindowName() string {
name := C.getActiveWindowName() name := C.getActiveWindowName()
if name == nil {
return ""
}
defer C.free(unsafe.Pointer(name))
return C.GoString(name) return C.GoString(name)
} }

View File

@ -51,11 +51,13 @@ func GetActiveWindowIcon() (image.Image, error) {
func GetActiveWindowName() string { func GetActiveWindowName() string {
cStr := C.getActiveWindowName() cStr := C.getActiveWindowName()
if cStr == nil {
return ""
}
defer C.free(unsafe.Pointer(cStr))
length := C.int(C.strlen(cStr)) length := C.int(C.strlen(cStr))
bytes := C.GoBytes(unsafe.Pointer(cStr), length) bytes := C.GoBytes(unsafe.Pointer(cStr), length)
str := string(bytes) return string(bytes)
C.free(unsafe.Pointer(cStr))
return str
} }
func GetActiveWindowPid() int { func GetActiveWindowPid() int {