From 3fbd744add53d9fda720a8bb10fb0231fe6dca98 Mon Sep 17 00:00:00 2001 From: Colin Xu <127581131+ColinXHL@users.noreply.github.com> Date: Wed, 3 Dec 2025 23:08:43 +0800 Subject: [PATCH] =?UTF-8?q?[=E5=85=A8=E8=87=AA=E5=8A=A8=E5=9C=B0=E8=84=89?= =?UTF-8?q?=E8=8A=B1]:=20=E4=BF=AE=E5=A4=8D=E5=9C=B0=E8=84=89=E8=8A=B1OCR?= =?UTF-8?q?=E8=AF=86=E5=88=AB=E5=86=85=E5=AD=98=E6=B3=84=E6=BC=8F=E9=97=AE?= =?UTF-8?q?=E9=A2=98=20(#2433)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix(js): 修复地脉花OCR识别内存泄漏问题 将resList变量声明移至try块外部,并在finally块中统一释放资源,避免多处dispose调用导致的内存管理问题 * update version 4.4.5 * fix(js): 修复浓缩树脂OCR识别内存泄漏问题 将textList变量声明移至try块外部,并在finally块中统一释放OCR识别结果资源,避免多处dispose调用导致的内存管理问题 * fix(js): 修复地脉花OCR识别captureRegion内存泄漏问题 将captureRegion变量声明移至try块外部,并在finally块中添加资源释放,避免captureGameRegion()调用产生的内存泄漏 --- repo/js/AutoLeyLineOutcrop/manifest.json | 2 +- .../utils/calCountByResin.js | 57 ++++++++++++------- 2 files changed, 36 insertions(+), 23 deletions(-) diff --git a/repo/js/AutoLeyLineOutcrop/manifest.json b/repo/js/AutoLeyLineOutcrop/manifest.json index ab9a709f4..f1cd05168 100644 --- a/repo/js/AutoLeyLineOutcrop/manifest.json +++ b/repo/js/AutoLeyLineOutcrop/manifest.json @@ -1,7 +1,7 @@ { "manifest_version": 1, "name": "全自动地脉花", - "version": "4.4.4", + "version": "4.4.5", "tags": ["地脉花"], "bgi_version": "0.52.0", "description": "基于OCR图像识别的全自动刷取地脉花。\n💡更多信息请查看README! \n\n----------注意事项----------\n●仅支持BetterGI 0.52.0 及以上版本!\n●部分地脉花因特殊原因不支持全自动,具体的点位请在手册中查看。\n●树脂使用的优先级:2倍原粹树脂 > 浓缩树脂 > 原粹树脂。\n●运行时会传送到七天神像设置中设置的七天神像,需要关闭七天神像设置中的“是否就近七天神像恢复血量”,并指定七天神像。\n●战斗策略注意调度器设置中地图追踪行走配置里的“允许在JsSpript中使用”和“覆盖JS中的自动战斗配置”,只有在都打开的情况下脚本才会使用下面的战斗配置,否则会使用独立任务中的战斗策略。战斗超时时间不能大于脚本自定义配置中的时间。\n\n如果遇到问题,请先参照README中的方法进行解决。", diff --git a/repo/js/AutoLeyLineOutcrop/utils/calCountByResin.js b/repo/js/AutoLeyLineOutcrop/utils/calCountByResin.js index 7b6191b40..180bd850a 100644 --- a/repo/js/AutoLeyLineOutcrop/utils/calCountByResin.js +++ b/repo/js/AutoLeyLineOutcrop/utils/calCountByResin.js @@ -156,20 +156,20 @@ function isPointInRegion(point, region) { * @returns {number|null} 识别到的数字或null */ async function recognizeNumberByOCR(ocrRegion, pattern) { + let resList = null; + let captureRegion = null; try { - // 直接链式调用,避免内存管理问题 const ocrRo = RecognitionObject.ocr(ocrRegion.x, ocrRegion.y, ocrRegion.width, ocrRegion.height); - const resList = captureGameRegion().findMulti(ocrRo); + captureRegion = captureGameRegion(); + resList = captureRegion.findMulti(ocrRo); if (!resList || resList.length === 0) { log.warn("OCR未识别到任何文本"); - resList.dispose(); return null; } for (const res of resList) { if (!res || !res.text) { - resList.dispose(); continue; } @@ -177,17 +177,22 @@ async function recognizeNumberByOCR(ocrRegion, pattern) { if (numberMatch) { const number = parseInt(numberMatch[1] || numberMatch[0]); if (!isNaN(number)) { - resList.dispose(); return number; } } } + return null; } catch (error) { log.error(`OCR识别时发生异常: ${error.message}`); - resList.dispose(); + return null; + } finally { + if (resList && typeof resList.dispose === 'function') { + resList.dispose(); + } + if (captureRegion && typeof captureRegion.dispose === 'function') { + captureRegion.dispose(); + } } - resList.dispose(); - return null; } // ==================== 树脂计数函数 ==================== @@ -258,22 +263,30 @@ async function countCondensedResin() { // 点击浓缩树脂打开说明界面统计 condensedResin.click(); await sleep(CONFIG.UI_DELAY); - let captureRegion = captureGameRegion(); - // OCR识别整个界面的文本 - let ocrRo = RecognitionObject.Ocr(0, 0, captureRegion.width, captureRegion.height); - let textList = captureRegion.findMulti(ocrRo); - captureRegion.dispose(); - for (const res of textList) { - if (res.text.includes("当前拥有")) { - const match = res.text.match(/当前拥有\s*([0-5ss])/); - if (match && match[1]) { - const count = parseInt(match[1]); - log.info(`浓缩树脂数量(说明界面): ${count}`); - keyPress("ESCAPE"); - await sleep(CONFIG.UI_DELAY); - return count; + let captureRegion = captureGameRegion(); + let textList = null; + try { + // OCR识别整个界面的文本 + let ocrRo = RecognitionObject.Ocr(0, 0, captureRegion.width, captureRegion.height); + textList = captureRegion.findMulti(ocrRo); + + for (const res of textList) { + if (res.text.includes("当前拥有")) { + const match = res.text.match(/当前拥有\s*([0-5ss])/); + if (match && match[1]) { + const count = parseInt(match[1]); + log.info(`浓缩树脂数量(说明界面): ${count}`); + keyPress("ESCAPE"); + await sleep(CONFIG.UI_DELAY); + return count; + } } } + } finally { + if (textList && typeof textList.dispose === 'function') { + textList.dispose(); + } + captureRegion.dispose(); } log.warn(`未能识别浓缩树脂数量`);