Feature/dialogs optimization (#1360)

Changed create policy in dialogs to fix memory leaks.
This commit is contained in:
ncyellow 2019-03-23 09:32:31 +03:00 committed by xarkes
parent 5d96fc9694
commit f59dce1727
10 changed files with 97 additions and 99 deletions

View File

@ -932,10 +932,10 @@ void MainWindow::on_actionLockUnlock_triggered()
void MainWindow::on_actionFunctionsRename_triggered() void MainWindow::on_actionFunctionsRename_triggered()
{ {
RenameDialog *r = new RenameDialog(this); RenameDialog r(this);
// Get function based on click position // Get function based on click position
//r->setFunctionName(fcn_name); //r->setFunctionName(fcn_name);
r->open(); r.exec();
} }
void MainWindow::on_actionDefault_triggered() void MainWindow::on_actionDefault_triggered()
@ -1058,9 +1058,8 @@ void MainWindow::on_actionRedoSeek_triggered()
void MainWindow::on_actionDisasAdd_comment_triggered() void MainWindow::on_actionDisasAdd_comment_triggered()
{ {
CommentsDialog *c = new CommentsDialog(this); CommentsDialog c(this);
c->exec(); c.exec();
delete c;
} }
void MainWindow::on_actionRefresh_contents_triggered() void MainWindow::on_actionRefresh_contents_triggered()

View File

@ -2,7 +2,7 @@
#include "ui_EditInstructionDialog.h" #include "ui_EditInstructionDialog.h"
#include "core/Cutter.h" #include "core/Cutter.h"
EditInstructionDialog::EditInstructionDialog(QWidget *parent, InstructionEditMode editMode) : EditInstructionDialog::EditInstructionDialog(InstructionEditMode editMode, QWidget *parent) :
QDialog(parent), QDialog(parent),
ui(new Ui::EditInstructionDialog), ui(new Ui::EditInstructionDialog),
editMode(editMode) editMode(editMode)

View File

@ -18,7 +18,7 @@ class EditInstructionDialog : public QDialog
Q_OBJECT Q_OBJECT
public: public:
explicit EditInstructionDialog(QWidget *parent, InstructionEditMode isEditingBytes); explicit EditInstructionDialog(InstructionEditMode isEditingBytes, QWidget *parent = nullptr);
~EditInstructionDialog(); ~EditInstructionDialog();
QString getInstruction(); QString getInstruction();

View File

@ -451,16 +451,16 @@ QList<QKeySequence> DisassemblyContextMenu::getAddBPSequence() const
void DisassemblyContextMenu::on_actionEditInstruction_triggered() void DisassemblyContextMenu::on_actionEditInstruction_triggered()
{ {
EditInstructionDialog *e = new EditInstructionDialog(this, EDIT_TEXT); EditInstructionDialog e(EDIT_TEXT, this);
e->setWindowTitle(tr("Edit Instruction at %1").arg(RAddressString(offset))); e.setWindowTitle(tr("Edit Instruction at %1").arg(RAddressString(offset)));
QString oldInstructionOpcode = Core()->getInstructionOpcode(offset); QString oldInstructionOpcode = Core()->getInstructionOpcode(offset);
QString oldInstructionBytes = Core()->getInstructionBytes(offset); QString oldInstructionBytes = Core()->getInstructionBytes(offset);
e->setInstruction(oldInstructionOpcode); e.setInstruction(oldInstructionOpcode);
if (e->exec()) { if (e.exec()) {
QString userInstructionOpcode = e->getInstruction(); QString userInstructionOpcode = e.getInstruction();
if (userInstructionOpcode != oldInstructionOpcode) { if (userInstructionOpcode != oldInstructionOpcode) {
Core()->editInstruction(offset, userInstructionOpcode); Core()->editInstruction(offset, userInstructionOpcode);
@ -522,14 +522,14 @@ void DisassemblyContextMenu::on_actionJmpReverse_triggered()
void DisassemblyContextMenu::on_actionEditBytes_triggered() void DisassemblyContextMenu::on_actionEditBytes_triggered()
{ {
EditInstructionDialog *e = new EditInstructionDialog(this, EDIT_BYTES); EditInstructionDialog e(EDIT_BYTES, this);
e->setWindowTitle(tr("Edit Bytes at %1").arg(RAddressString(offset))); e.setWindowTitle(tr("Edit Bytes at %1").arg(RAddressString(offset)));
QString oldBytes = Core()->getInstructionBytes(offset); QString oldBytes = Core()->getInstructionBytes(offset);
e->setInstruction(oldBytes); e.setInstruction(oldBytes);
if (e->exec()) { if (e.exec()) {
QString bytes = e->getInstruction(); QString bytes = e.getInstruction();
if (bytes != oldBytes) { if (bytes != oldBytes) {
Core()->editBytes(offset, bytes); Core()->editBytes(offset, bytes);
@ -596,17 +596,17 @@ void DisassemblyContextMenu::on_actionAddComment_triggered()
QString oldComment = Core()->cmd("CC." + RAddressString(offset)); QString oldComment = Core()->cmd("CC." + RAddressString(offset));
// Remove newline at the end added by cmd // Remove newline at the end added by cmd
oldComment.remove(oldComment.length() - 1, 1); oldComment.remove(oldComment.length() - 1, 1);
CommentsDialog *c = new CommentsDialog(this); CommentsDialog c(this);
if (oldComment.isNull() || oldComment.isEmpty()) { if (oldComment.isNull() || oldComment.isEmpty()) {
c->setWindowTitle(tr("Add Comment at %1").arg(RAddressString(offset))); c.setWindowTitle(tr("Add Comment at %1").arg(RAddressString(offset)));
} else { } else {
c->setWindowTitle(tr("Edit Comment at %1").arg(RAddressString(offset))); c.setWindowTitle(tr("Edit Comment at %1").arg(RAddressString(offset)));
} }
c->setComment(oldComment); c.setComment(oldComment);
if (c->exec()) { if (c.exec()) {
QString comment = c->getComment(); QString comment = c.getComment();
if (comment.isEmpty()) { if (comment.isEmpty()) {
Core()->delComment(offset); Core()->delComment(offset);
} else { } else {
@ -617,43 +617,43 @@ void DisassemblyContextMenu::on_actionAddComment_triggered()
void DisassemblyContextMenu::on_actionAnalyzeFunction_triggered() void DisassemblyContextMenu::on_actionAnalyzeFunction_triggered()
{ {
RenameDialog *dialog = new RenameDialog(this); RenameDialog dialog(this);
dialog->setWindowTitle(tr("Analyze function at %1").arg(RAddressString(offset))); dialog.setWindowTitle(tr("Analyze function at %1").arg(RAddressString(offset)));
dialog->setPlaceholderText(tr("Function name")); dialog.setPlaceholderText(tr("Function name"));
if (dialog->exec()) { if (dialog.exec()) {
QString function_name = dialog->getName(); QString function_name = dialog.getName();
Core()->createFunctionAt(offset, function_name); Core()->createFunctionAt(offset, function_name);
} }
} }
void DisassemblyContextMenu::on_actionAddFlag_triggered() void DisassemblyContextMenu::on_actionAddFlag_triggered()
{ {
FlagDialog *dialog = new FlagDialog(offset, this->parentWidget()); FlagDialog dialog(offset, this->parentWidget());
dialog->exec(); dialog.exec();
} }
void DisassemblyContextMenu::on_actionRename_triggered() void DisassemblyContextMenu::on_actionRename_triggered()
{ {
RCore *core = Core()->core(); RCore *core = Core()->core();
RenameDialog *dialog = new RenameDialog(this); RenameDialog dialog(this);
RAnalFunction *fcn = r_anal_get_fcn_at (core->anal, offset, R_ANAL_FCN_TYPE_NULL); RAnalFunction *fcn = r_anal_get_fcn_at (core->anal, offset, R_ANAL_FCN_TYPE_NULL);
RFlagItem *f = r_flag_get_i (core->flags, offset); RFlagItem *f = r_flag_get_i (core->flags, offset);
if (fcn) { if (fcn) {
/* Rename function */ /* Rename function */
dialog->setWindowTitle(tr("Rename function %1").arg(fcn->name)); dialog.setWindowTitle(tr("Rename function %1").arg(fcn->name));
dialog->setName(fcn->name); dialog.setName(fcn->name);
if (dialog->exec()) { if (dialog.exec()) {
QString new_name = dialog->getName(); QString new_name = dialog.getName();
Core()->renameFunction(fcn->name, new_name); Core()->renameFunction(fcn->name, new_name);
} }
} else if (f) { } else if (f) {
/* Rename current flag */ /* Rename current flag */
dialog->setWindowTitle(tr("Rename flag %1").arg(f->name)); dialog.setWindowTitle(tr("Rename flag %1").arg(f->name));
dialog->setName(f->name); dialog.setName(f->name);
if (dialog->exec()) { if (dialog.exec()) {
QString new_name = dialog->getName(); QString new_name = dialog.getName();
Core()->renameFlag(f->name, new_name); Core()->renameFlag(f->name, new_name);
} }
} else { } else {
@ -671,22 +671,22 @@ void DisassemblyContextMenu::on_actionRenameUsedHere_triggered()
QJsonObject thingUsedHere = array.first().toObject(); QJsonObject thingUsedHere = array.first().toObject();
QString type = thingUsedHere.value("type").toString(); QString type = thingUsedHere.value("type").toString();
RenameDialog *dialog = new RenameDialog(this); RenameDialog dialog(this);
QString oldName; QString oldName;
if (type == "address") { if (type == "address") {
RVA offset = thingUsedHere["offset"].toVariant().toULongLong(); RVA offset = thingUsedHere["offset"].toVariant().toULongLong();
dialog->setWindowTitle(tr("Add flag at %1").arg(RAddressString(offset))); dialog.setWindowTitle(tr("Add flag at %1").arg(RAddressString(offset)));
dialog->setName("label." + QString::number(offset, 16)); dialog.setName("label." + QString::number(offset, 16));
} else { } else {
oldName = thingUsedHere.value("name").toString(); oldName = thingUsedHere.value("name").toString();
dialog->setWindowTitle(tr("Rename %1").arg(oldName)); dialog.setWindowTitle(tr("Rename %1").arg(oldName));
dialog->setName(oldName); dialog.setName(oldName);
} }
if (dialog->exec()) { if (dialog.exec()) {
QString newName = dialog->getName().trimmed(); QString newName = dialog.getName().trimmed();
if (!newName.isEmpty()) { if (!newName.isEmpty()) {
Core()->cmd("an " + newName + " @ " + QString::number(offset)); Core()->cmd("an " + newName + " @ " + QString::number(offset));
@ -716,16 +716,16 @@ void DisassemblyContextMenu::on_actionSetFunctionVarTypes_triggered()
void DisassemblyContextMenu::on_actionXRefs_triggered() void DisassemblyContextMenu::on_actionXRefs_triggered()
{ {
XrefsDialog *dialog = new XrefsDialog(this); XrefsDialog dialog(this);
dialog->fillRefsForAddress(offset, RAddressString(offset), false); dialog.fillRefsForAddress(offset, RAddressString(offset), false);
dialog->exec(); dialog.exec();
} }
void DisassemblyContextMenu::on_actionDisplayOptions_triggered() void DisassemblyContextMenu::on_actionDisplayOptions_triggered()
{ {
auto *dialog = new PreferencesDialog(this->window()); PreferencesDialog dialog(this->window());
dialog->showSection(PreferencesDialog::Section::Disassembly); dialog.showSection(PreferencesDialog::Section::Disassembly);
dialog->show(); dialog.exec();
} }
void DisassemblyContextMenu::on_actionSetToCode_triggered() void DisassemblyContextMenu::on_actionSetToCode_triggered()
@ -755,11 +755,11 @@ void DisassemblyContextMenu::on_actionSetToData_triggered()
void DisassemblyContextMenu::on_actionSetToDataEx_triggered() void DisassemblyContextMenu::on_actionSetToDataEx_triggered()
{ {
auto dialog = new SetToDataDialog(offset, this->window()); SetToDataDialog dialog(offset, this->window());
if (!dialog->exec()) { if (!dialog.exec()) {
return; return;
} }
setToData(dialog->getItemSize(), dialog->getItemCount()); setToData(dialog.getItemSize(), dialog.getItemCount());
} }
void DisassemblyContextMenu::on_actionStructureOffsetMenu_triggered(QAction *action) void DisassemblyContextMenu::on_actionStructureOffsetMenu_triggered(QAction *action)
@ -792,39 +792,39 @@ void DisassemblyContextMenu::on_actionDeleteFunction_triggered()
void DisassemblyContextMenu::on_actionEditFunction_triggered() void DisassemblyContextMenu::on_actionEditFunction_triggered()
{ {
RCore *core = Core()->core(); RCore *core = Core()->core();
EditFunctionDialog *dialog = new EditFunctionDialog(this); EditFunctionDialog dialog(this);
RAnalFunction *fcn = r_anal_get_fcn_in(core->anal, offset, 0); RAnalFunction *fcn = r_anal_get_fcn_in(core->anal, offset, 0);
if (fcn) { if (fcn) {
dialog->setWindowTitle(tr("Edit function %1").arg(fcn->name)); dialog.setWindowTitle(tr("Edit function %1").arg(fcn->name));
dialog->setNameText(fcn->name); dialog.setNameText(fcn->name);
QString startAddrText = "0x" + QString::number(fcn->addr, 16); QString startAddrText = "0x" + QString::number(fcn->addr, 16);
dialog->setStartAddrText(startAddrText); dialog.setStartAddrText(startAddrText);
QString endAddrText = "0x" + QString::number(fcn->addr + fcn->_size, 16); QString endAddrText = "0x" + QString::number(fcn->addr + fcn->_size, 16);
dialog->setEndAddrText(endAddrText); dialog.setEndAddrText(endAddrText);
QString stackSizeText; QString stackSizeText;
stackSizeText.sprintf("%d", fcn->stack); stackSizeText.sprintf("%d", fcn->stack);
dialog->setStackSizeText(stackSizeText); dialog.setStackSizeText(stackSizeText);
QStringList callConList = Core()->cmd("afcl").split("\n"); QStringList callConList = Core()->cmd("afcl").split("\n");
callConList.removeLast(); callConList.removeLast();
dialog->setCallConList(callConList); dialog.setCallConList(callConList);
dialog->setCallConSelected(fcn->cc); dialog.setCallConSelected(fcn->cc);
if (dialog->exec()) { if (dialog.exec()) {
QString new_name = dialog->getNameText(); QString new_name = dialog.getNameText();
Core()->renameFunction(fcn->name, new_name); Core()->renameFunction(fcn->name, new_name);
QString new_start_addr = dialog->getStartAddrText(); QString new_start_addr = dialog.getStartAddrText();
fcn->addr = Core()->math(new_start_addr); fcn->addr = Core()->math(new_start_addr);
QString new_end_addr = dialog->getEndAddrText(); QString new_end_addr = dialog.getEndAddrText();
Core()->cmd("afu " + new_end_addr); Core()->cmd("afu " + new_end_addr);
QString new_stack_size = dialog->getStackSizeText(); QString new_stack_size = dialog.getStackSizeText();
fcn->stack = int(Core()->math(new_stack_size)); fcn->stack = int(Core()->math(new_stack_size));
Core()->cmd("afc " + dialog->getCallConSelected()); Core()->cmd("afc " + dialog.getCallConSelected());
emit Core()->functionsChanged(); emit Core()->functionsChanged();
} }
} }

View File

@ -186,10 +186,10 @@ void BreakpointWidget::showBreakpointContextMenu(const QPoint &pt)
void BreakpointWidget::addBreakpointDialog() void BreakpointWidget::addBreakpointDialog()
{ {
BreakpointsDialog *dialog = new BreakpointsDialog(this); BreakpointsDialog dialog(this);
if (dialog->exec()) { if (dialog.exec()) {
QString bps = dialog->getBreakpoints(); QString bps = dialog.getBreakpoints();
if (!bps.isEmpty()) { if (!bps.isEmpty()) {
QStringList bpList = bps.split(' ', QString::SkipEmptyParts); QStringList bpList = bps.split(' ', QString::SkipEmptyParts);
for (const QString &bp : bpList) { for (const QString &bp : bpList) {

View File

@ -161,12 +161,12 @@ void DebugActions::continueUntilMain()
void DebugActions::attachProcessDialog() void DebugActions::attachProcessDialog()
{ {
AttachProcDialog *dialog = new AttachProcDialog(main); AttachProcDialog dialog(main);
bool success = false; bool success = false;
while (!success) { while (!success) {
success = true; success = true;
if (dialog->exec()) { if (dialog.exec()) {
int pid = dialog->getPID(); int pid = dialog.getPID();
if (pid >= 0) { if (pid >= 0) {
attachProcess(pid); attachProcess(pid);
} else { } else {
@ -177,7 +177,6 @@ void DebugActions::attachProcessDialog()
} }
} }
} }
delete dialog;
} }
void DebugActions::attachProcess(int pid) void DebugActions::attachProcess(int pid)

View File

@ -183,10 +183,10 @@ void FlagsWidget::on_actionRename_triggered()
FlagDescription flag = ui->flagsTreeView->selectionModel()->currentIndex().data( FlagDescription flag = ui->flagsTreeView->selectionModel()->currentIndex().data(
FlagsModel::FlagDescriptionRole).value<FlagDescription>(); FlagsModel::FlagDescriptionRole).value<FlagDescription>();
RenameDialog *r = new RenameDialog(this); RenameDialog r(this);
r->setName(flag.name); r.setName(flag.name);
if (r->exec()) { if (r.exec()) {
QString new_name = r->getName(); QString new_name = r.getName();
Core()->renameFlag(flag.name, new_name); Core()->renameFlag(flag.name, new_name);
} }
} }

View File

@ -604,11 +604,11 @@ void FunctionsWidget::on_actionDisasAdd_comment_triggered()
FunctionModel::FunctionDescriptionRole).value<FunctionDescription>(); FunctionModel::FunctionDescriptionRole).value<FunctionDescription>();
// Create dialog // Create dialog
CommentsDialog *c = new CommentsDialog(this); CommentsDialog c(this);
if (c->exec()) { if (c.exec()) {
// Get new function name // Get new function name
QString comment = c->getComment(); QString comment = c.getComment();
// Rename function in r2 core // Rename function in r2 core
Core()->setComment(function.offset, comment); Core()->setComment(function.offset, comment);
// Seek to new renamed function // Seek to new renamed function
@ -624,14 +624,14 @@ void FunctionsWidget::on_actionFunctionsRename_triggered()
FunctionModel::FunctionDescriptionRole).value<FunctionDescription>(); FunctionModel::FunctionDescriptionRole).value<FunctionDescription>();
// Create dialog // Create dialog
RenameDialog *r = new RenameDialog(this); RenameDialog r(this);
// Set function name in dialog // Set function name in dialog
r->setName(function.name); r.setName(function.name);
// If user accepted // If user accepted
if (r->exec()) { if (r.exec()) {
// Get new function name // Get new function name
QString new_name = r->getName(); QString new_name = r.getName();
// Rename function in r2 core // Rename function in r2 core
Core()->renameFunction(function.name, new_name); Core()->renameFunction(function.name, new_name);
@ -653,9 +653,9 @@ void FunctionsWidget::on_action_References_triggered()
// Get selected item in functions tree view // Get selected item in functions tree view
FunctionDescription function = ui->functionsTreeView->selectionModel()->currentIndex().data( FunctionDescription function = ui->functionsTreeView->selectionModel()->currentIndex().data(
FunctionModel::FunctionDescriptionRole).value<FunctionDescription>(); FunctionModel::FunctionDescriptionRole).value<FunctionDescription>();
XrefsDialog *x = new XrefsDialog(this); XrefsDialog x(this);
x->fillRefsForAddress(function.offset, function.name, true); x.fillRefsForAddress(function.offset, function.name, true);
x->exec(); x.exec();
} }
void FunctionsWidget::showTitleContextMenu(const QPoint &pt) void FunctionsWidget::showTitleContextMenu(const QPoint &pt)

View File

@ -131,14 +131,14 @@ void StackWidget::editStack()
bool ok; bool ok;
int row = viewStack->selectionModel()->currentIndex().row(); int row = viewStack->selectionModel()->currentIndex().row();
QString offset = viewStack->selectionModel()->currentIndex().sibling(row, 0).data().toString(); QString offset = viewStack->selectionModel()->currentIndex().sibling(row, 0).data().toString();
EditInstructionDialog *e = new EditInstructionDialog(this, EDIT_NONE); EditInstructionDialog e(EDIT_NONE, this);
e->setWindowTitle(tr("Edit stack at %1").arg(offset)); e.setWindowTitle(tr("Edit stack at %1").arg(offset));
QString oldBytes = viewStack->selectionModel()->currentIndex().sibling(row, 1).data().toString(); QString oldBytes = viewStack->selectionModel()->currentIndex().sibling(row, 1).data().toString();
e->setInstruction(oldBytes); e.setInstruction(oldBytes);
if (e->exec()) { if (e.exec()) {
QString bytes = e->getInstruction(); QString bytes = e.getInstruction();
if (bytes != oldBytes) { if (bytes != oldBytes) {
Core()->editBytesEndian(offset.toULongLong(&ok, 16), bytes); Core()->editBytesEndian(offset.toULongLong(&ok, 16), bytes);
} }

View File

@ -271,10 +271,10 @@ void StringsWidget::on_actionX_refs_triggered()
StringDescription str = ui->stringsTreeView->selectionModel()->currentIndex().data( StringDescription str = ui->stringsTreeView->selectionModel()->currentIndex().data(
StringsModel::StringDescriptionRole).value<StringDescription>(); StringsModel::StringDescriptionRole).value<StringDescription>();
XrefsDialog *x = new XrefsDialog(this); XrefsDialog x(this);
x->fillRefsForAddress(str.vaddr, RAddressString(str.vaddr), false); x.fillRefsForAddress(str.vaddr, RAddressString(str.vaddr), false);
x->setAttribute(Qt::WA_DeleteOnClose); x.setAttribute(Qt::WA_DeleteOnClose);
x->exec(); x.exec();
} }
void StringsWidget::on_actionCopy() void StringsWidget::on_actionCopy()