From e311459a5ab2a956e64b941dcecf9c83648e52c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=A8=E7=BA=A2=E5=B2=A9?= Date: Thu, 28 Dec 2023 19:54:41 +0800 Subject: [PATCH] =?UTF-8?q?U:=20=E5=A2=9E=E5=8A=A0SetReleaseCallback=20?= =?UTF-8?q?=E5=BA=94=E7=94=A8=E8=BF=90=E8=A1=8C=E7=BB=93=E6=9D=9F=E5=90=8E?= =?UTF-8?q?=E9=87=8A=E6=94=BE=E8=B5=84=E6=BA=90=E4=B9=8B=E5=89=8D=E6=89=A7?= =?UTF-8?q?=E8=A1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lcl/api/dllimports/importfuncs.go | 2 +- lcl/api/energy_api.go | 12 ++++++++++++ lcl/api/importdef.go | 3 +++ 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/lcl/api/dllimports/importfuncs.go b/lcl/api/dllimports/importfuncs.go index f22dba7..d3d5acb 100644 --- a/lcl/api/dllimports/importfuncs.go +++ b/lcl/api/dllimports/importfuncs.go @@ -25,7 +25,7 @@ func internalGetImportFunc(uiLib DLL, table []*ImportTable, index int) ProcAddr var err error item.addr, err = uiLib.GetProcAddr(item.name) if err != nil { - fmt.Println(err) + fmt.Println(err, item.name) return 0 } atomic.StorePointer((*unsafe.Pointer)(unsafe.Pointer(&table[index].addr)), unsafe.Pointer(item.addr)) diff --git a/lcl/api/energy_api.go b/lcl/api/energy_api.go index deed4ae..aa499c9 100644 --- a/lcl/api/energy_api.go +++ b/lcl/api/energy_api.go @@ -14,6 +14,8 @@ package api import "github.com/energye/golcl/lcl/api/dllimports" +var releaseCallback func() + func APIInit() { uiLib = loadUILib() } @@ -25,6 +27,16 @@ func ImportDefFunc(importTable []*dllimports.ImportTable, index int) dllimports. // EnergyLibRelease 在energy中释放 func EnergyLibRelease() { + if releaseCallback != nil { + releaseCallback() + } callGC() closeLib() } + +// SetReleaseCallback 应用运行结束后释放资源之前执行 +func SetReleaseCallback(fn func()) { + if releaseCallback == nil { + releaseCallback = fn + } +} diff --git a/lcl/api/importdef.go b/lcl/api/importdef.go index 52ad9e2..1936d92 100644 --- a/lcl/api/importdef.go +++ b/lcl/api/importdef.go @@ -27,6 +27,9 @@ func Application_CreateForm(app uintptr) uintptr { func Application_Run(app uintptr) { defer func() { + if releaseCallback != nil { + releaseCallback() + } // 开启了finalizerOn选项后,以防止关闭库后GC还没开始调用。 callGC() // 运行结束后就结束close掉lib,不然他不会关掉的