From 4a86f32ac430f4adcac883a8a4dd4903d9fc4f48 Mon Sep 17 00:00:00 2001 From: liujinchang Date: Fri, 30 Oct 2026 03:26:36 +0800 Subject: [PATCH] fix: add memory check before opening files MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. Root cause: Window::addTab() and EditWrapper::openFile() load entire file content into memory without checking available RAM, causing OOM-kill when opening multiple large files 2. Fix: call existing Utils::isMemorySufficientForOperation() with RawOperation type before file loading in both entry points, show warning and abort if memory is insufficient 3. Impact: files are blocked from opening when memory is low instead of crashing; no behavior change when memory is sufficient Log: Prevent crash when opening large files with insufficient memory Influence: 1. Test opening large files under low memory conditions 2. Verify normal file opening is not affected 3. Test opening multiple files simultaneously fix: 打开文件前增加内存检查 1. 根因:Window::addTab() 和 EditWrapper::openFile() 在加载文件时 将整个文件内容读入内存,未检查可用内存,打开多个大文件时触发 OOM-kill 崩溃 2. 方案:在两个入口点加载文件前调用已有的 Utils::isMemorySufficientForOperation()(RawOperation 类型), 内存不足时显示警告并中止打开 3. 影响:内存不足时阻止文件打开而非崩溃;内存充足时行为不变 Log: 打开大文件时内存不足改为提示而非崩溃 Influence: 1. 测试低内存条件下打开大文件 2. 验证正常文件打开不受影响 3. 测试同时打开多个文件 PMS: BUG-111510 --- src/editor/editwrapper.cpp | 15 +++++++++++++++ src/widgets/window.cpp | 9 +++++++++ 2 files changed, 24 insertions(+) diff --git a/src/editor/editwrapper.cpp b/src/editor/editwrapper.cpp index 1f5ed004..be2a43ad 100644 --- a/src/editor/editwrapper.cpp +++ b/src/editor/editwrapper.cpp @@ -257,6 +257,21 @@ void EditWrapper::openFile(const QString &filepath, QString qstrTruePath, bool b Settings::instance()->setSavePath(PathSettingWgt::CurFileBox, absPath); } + // Check if there is sufficient memory to open the file. + QFileInfo memFileInfo(filepath); + if (memFileInfo.exists() && !Utils::isMemorySufficientForOperation(Utils::OperationType::RawOperation, memFileInfo.size(), 0)) { + qCWarning(appLog) << "Insufficient memory to open file:" << filepath << "size:" << memFileInfo.size(); + QWidget *target = m_pTextEdit; + if (target) { +#ifdef DTKWIDGET_CLASS_DSizeMode + Utils::sendFloatMessageFixedFont(target, QIcon(":/images/warning.svg"), tr("Insufficient memory to open this file")); +#else + DMessageManager::instance()->sendMessage(target, QIcon(":/images/warning.svg"), tr("Insufficient memory to open this file")); +#endif + } + return; + } + FileLoadThread *thread = new FileLoadThread(filepath); qCDebug(appLog) << "Created file load thread for:" << filepath; // 优先使用历史记录的编码 diff --git a/src/widgets/window.cpp b/src/widgets/window.cpp index 46b26158..e7ecc1ec 100644 --- a/src/widgets/window.cpp +++ b/src/widgets/window.cpp @@ -728,6 +728,15 @@ void Window::addTab(const QString &filepath, bool activeTab) } } + // Check if there is sufficient memory to open the file. + if (!Utils::isMemorySufficientForOperation(Utils::OperationType::RawOperation, fileInfo.size(), 0)) { + qCWarning(appLog) << "Insufficient memory to open file:" << filepath << "size:" << fileInfo.size(); + QWidget *const msgParent = m_editorWidget->currentWidget() ? m_editorWidget->currentWidget() : this; + DMessageManager::instance()->sendMessage(msgParent, QIcon(":/images/warning.svg"), + tr("Insufficient memory to open this file")); + return; + } + if (StartManager::instance()->checkPath(filepath)) { qCDebug(appLog) << "Path check passed, adding tab for file"; m_tabbar->addTab(filepath, tabName, filepath);