Skip to content

Commit 2b0b674

Browse files
alphagoccclaude
andcommitted
fix(exttestcase): fix memory leak and noDfs reentrancy guard
- Convert all ExtTestCaseUpdaterDialog new/delete to std::unique_ptr, fixing a memory leak in modifySelected() where the third branch (single file editing) was missing delete - Fix noDfs reentrancy guard in ExtTestCaseTable::whenItemSelectionChanged() where line 163 incorrectly set noDfs=false instead of true, making the guard ineffective against recursive signal invocations Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> (cherry picked from commit cb67cd5)
1 parent 2e89fbf commit 2b0b674

2 files changed

Lines changed: 13 additions & 19 deletions

File tree

src/exttestcasemodifier.cpp

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
#include "core/task.h"
1212
#include "core/testcase.h"
1313
#include "exttestcaseupdaterdialog.h"
14+
//
15+
#include <memory>
1416

1517
ExtTestCaseModifier::ExtTestCaseModifier(QWidget *parent) : QWidget(parent), ui(new Ui::ExtTestCaseModifier) {
1618
ui->setupUi(this);
@@ -63,8 +65,8 @@ void ExtTestCaseModifier::modifySelected() {
6365
int l = hav.front(), r = hav.back();
6466

6567
if (l != r) {
66-
auto *dialog = new ExtTestCaseUpdaterDialog(this, editTask, editSettings, -1, MAY_EDIT, NO_EDIT,
67-
MAY_EDIT, MAY_EDIT, NO_EDIT);
68+
auto dialog = std::make_unique<ExtTestCaseUpdaterDialog>(
69+
this, editTask, editSettings, -1, MAY_EDIT, NO_EDIT, MAY_EDIT, MAY_EDIT, NO_EDIT);
6870

6971
if (dialog->exec() == QDialog::Accepted) {
7072
auto sc = dialog->getScore();
@@ -82,10 +84,8 @@ void ExtTestCaseModifier::modifySelected() {
8284
editTask->getTestCase(i)->setMemoryLimit(ml);
8385
}
8486
}
85-
86-
delete dialog;
8787
} else {
88-
auto *dialog = new ExtTestCaseUpdaterDialog(
88+
auto dialog = std::make_unique<ExtTestCaseUpdaterDialog>(
8989
this, editTask, editSettings, l + 1, editTask->getTestCase(l)->getFullScore(), NO_EDIT,
9090
editTask->getTestCase(l)->getTimeLimit(), editTask->getTestCase(l)->getMemoryLimit(), 1,
9191
editTask->getTestCase(l)->getDependenceSubtask());
@@ -96,14 +96,12 @@ void ExtTestCaseModifier::modifySelected() {
9696
editTask->getTestCase(l)->setMemoryLimit(dialog->getMemoryLimit());
9797
editTask->getTestCase(l)->setDependenceSubtask(dialog->getDepends());
9898
}
99-
100-
delete dialog;
10199
}
102100
} else {
103101
int who = res.front().first, loc = res.front().second.first;
104102

105-
auto *dialog = new ExtTestCaseUpdaterDialog(this, editTask, editSettings, -1, NO_EDIT, 1, NO_EDIT,
106-
NO_EDIT, NO_EDIT);
103+
auto dialog = std::make_unique<ExtTestCaseUpdaterDialog>(this, editTask, editSettings, -1, NO_EDIT, 1,
104+
NO_EDIT, NO_EDIT, NO_EDIT);
107105

108106
if (dialog->exec() == QDialog::Accepted) {
109107
editTask->getTestCase(who)->setInputFiles(loc, dialog->getInput());
@@ -375,9 +373,9 @@ void ExtTestCaseModifier::splitSelected() {
375373
}
376374

377375
void ExtTestCaseModifier::appendNewSub() {
378-
auto *dialog =
379-
new ExtTestCaseUpdaterDialog(this, editTask, editSettings, editTask->getTestCaseList().size() + 1,
380-
EDIT_WITH_DEFAULT, 1, EDIT_WITH_DEFAULT, EDIT_WITH_DEFAULT, 1);
376+
auto dialog = std::make_unique<ExtTestCaseUpdaterDialog>(
377+
this, editTask, editSettings, editTask->getTestCaseList().size() + 1, EDIT_WITH_DEFAULT, 1,
378+
EDIT_WITH_DEFAULT, EDIT_WITH_DEFAULT, 1);
381379

382380
if (dialog->exec() == QDialog::Accepted) {
383381
auto *now = new TestCase;
@@ -390,22 +388,18 @@ void ExtTestCaseModifier::appendNewSub() {
390388
editTask->addTestCase(now);
391389
}
392390

393-
delete dialog;
394-
395391
refresh();
396392
}
397393

398394
void ExtTestCaseModifier::appendNewCase() {
399395
int who = ui->testCaseTable->getSelectedHaveSub().constLast();
400396

401-
auto *dialog = new ExtTestCaseUpdaterDialog(this, editTask, editSettings, who + 1, NO_EDIT, 1, NO_EDIT,
402-
NO_EDIT, NO_EDIT);
397+
auto dialog = std::make_unique<ExtTestCaseUpdaterDialog>(this, editTask, editSettings, who + 1, NO_EDIT,
398+
1, NO_EDIT, NO_EDIT, NO_EDIT);
403399

404400
if (dialog->exec() == QDialog::Accepted) {
405401
editTask->getTestCase(who)->addSingleCase(dialog->getInput(), dialog->getOutput());
406402
}
407403

408-
delete dialog;
409-
410404
refresh();
411405
}

src/exttestcasetable.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ void ExtTestCaseTable::whenItemSelectionChanged() {
160160
if (noDfs)
161161
return;
162162

163-
noDfs = false;
163+
noDfs = true;
164164

165165
isCanModify = false;
166166
isCanAddSub = true;

0 commit comments

Comments
 (0)