Skip to content

Commit 2ff37d7

Browse files
ADKasterdiegoiast
andcommitted
Ladybird/Everywhere: Ensure that Qt objects are created with parents
This prevents memory leaks detected by both Valgrind and ASAN/LSAN. Valgrind is still suspicious of the leaked JS::VM from Web::Bindings::main_thread_vm() but there's other issues with leak checking all the GC'd objects. Co-Authored-By: Diego Iastrubni <diegoiast@gmail.com>
1 parent f9af283 commit 2ff37d7

File tree

7 files changed

+41
-41
lines changed

7 files changed

+41
-41
lines changed

Ladybird/BrowserWindow.cpp

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ extern Browser::Settings* s_settings;
2323

2424
BrowserWindow::BrowserWindow()
2525
{
26-
m_tabs_container = new QTabWidget;
26+
m_tabs_container = new QTabWidget(this);
2727
m_tabs_container->setElideMode(Qt::TextElideMode::ElideRight);
2828
m_tabs_container->setMovable(true);
2929
m_tabs_container->setTabsClosable(true);
@@ -32,30 +32,30 @@ BrowserWindow::BrowserWindow()
3232

3333
auto* menu = menuBar()->addMenu("&File");
3434

35-
auto* new_tab_action = new QAction("New &Tab");
35+
auto* new_tab_action = new QAction("New &Tab", this);
3636
new_tab_action->setShortcut(QKeySequence(Qt::CTRL | Qt::Key_T));
3737
menu->addAction(new_tab_action);
3838

39-
auto* settings_action = new QAction("&Settings");
39+
auto* settings_action = new QAction("&Settings", this);
4040
settings_action->setShortcut(QKeySequence(Qt::CTRL | Qt::Key_Comma));
4141
menu->addAction(settings_action);
4242

43-
auto* close_current_tab_action = new QAction("Close Current Tab");
43+
auto* close_current_tab_action = new QAction("Close Current Tab", this);
4444
close_current_tab_action->setShortcut(QKeySequence(Qt::CTRL | Qt::Key_W));
4545
menu->addAction(close_current_tab_action);
4646

47-
auto* quit_action = new QAction("&Quit");
47+
auto* quit_action = new QAction("&Quit", this);
4848
quit_action->setShortcut(QKeySequence(Qt::CTRL | Qt::Key_Q));
4949
menu->addAction(quit_action);
5050

5151
auto* view_menu = menuBar()->addMenu("&View");
5252

53-
auto* open_next_tab_action = new QAction("Open &Next Tab");
53+
auto* open_next_tab_action = new QAction("Open &Next Tab", this);
5454
open_next_tab_action->setShortcut(QKeySequence(Qt::CTRL | Qt::Key_PageDown));
5555
view_menu->addAction(open_next_tab_action);
5656
QObject::connect(open_next_tab_action, &QAction::triggered, this, &BrowserWindow::open_next_tab);
5757

58-
auto* open_previous_tab_action = new QAction("Open &Previous Tab");
58+
auto* open_previous_tab_action = new QAction("Open &Previous Tab", this);
5959
open_previous_tab_action->setShortcut(QKeySequence(Qt::CTRL | Qt::Key_PageUp));
6060
view_menu->addAction(open_previous_tab_action);
6161
QObject::connect(open_previous_tab_action, &QAction::triggered, this, &BrowserWindow::open_previous_tab);
@@ -66,19 +66,19 @@ BrowserWindow::BrowserWindow()
6666

6767
auto* color_scheme_group = new QActionGroup(this);
6868

69-
auto* auto_color_scheme = new QAction("&Auto");
69+
auto* auto_color_scheme = new QAction("&Auto", this);
7070
auto_color_scheme->setCheckable(true);
7171
color_scheme_group->addAction(auto_color_scheme);
7272
color_scheme_menu->addAction(auto_color_scheme);
7373
QObject::connect(auto_color_scheme, &QAction::triggered, this, &BrowserWindow::enable_auto_color_scheme);
7474

75-
auto* light_color_scheme = new QAction("&Light");
75+
auto* light_color_scheme = new QAction("&Light", this);
7676
light_color_scheme->setCheckable(true);
7777
color_scheme_group->addAction(light_color_scheme);
7878
color_scheme_menu->addAction(light_color_scheme);
7979
QObject::connect(light_color_scheme, &QAction::triggered, this, &BrowserWindow::enable_light_color_scheme);
8080

81-
auto* dark_color_scheme = new QAction("&Dark");
81+
auto* dark_color_scheme = new QAction("&Dark", this);
8282
dark_color_scheme->setCheckable(true);
8383
color_scheme_group->addAction(dark_color_scheme);
8484
color_scheme_menu->addAction(dark_color_scheme);
@@ -88,23 +88,23 @@ BrowserWindow::BrowserWindow()
8888

8989
auto* inspect_menu = menuBar()->addMenu("&Inspect");
9090

91-
auto* view_source_action = new QAction("View &Source");
91+
auto* view_source_action = new QAction("View &Source", this);
9292
view_source_action->setIcon(QIcon(QString("%1/res/icons/16x16/filetype-html.png").arg(s_serenity_resource_root.characters())));
9393
view_source_action->setShortcut(QKeySequence(Qt::CTRL | Qt::Key_U));
9494
inspect_menu->addAction(view_source_action);
9595
QObject::connect(view_source_action, &QAction::triggered, this, [this] {
9696
if (m_current_tab) {
9797
auto source = m_current_tab->view().source();
9898

99-
auto* text_edit = new QPlainTextEdit;
99+
auto* text_edit = new QPlainTextEdit(this);
100100
text_edit->setFont(QFontDatabase::systemFont(QFontDatabase::SystemFont::FixedFont));
101101
text_edit->resize(800, 600);
102102
text_edit->setPlainText(source.characters());
103103
text_edit->show();
104104
}
105105
});
106106

107-
auto* js_console_action = new QAction("Show &JS Console");
107+
auto* js_console_action = new QAction("Show &JS Console", this);
108108
js_console_action->setIcon(QIcon(QString("%1/res/icons/16x16/filetype-javascript.png").arg(s_serenity_resource_root.characters())));
109109
js_console_action->setShortcut(QKeySequence(Qt::CTRL | Qt::SHIFT | Qt::Key_J));
110110
inspect_menu->addAction(js_console_action);
@@ -126,49 +126,49 @@ BrowserWindow::BrowserWindow()
126126

127127
auto* debug_menu = menuBar()->addMenu("&Debug");
128128

129-
auto* dump_dom_tree_action = new QAction("Dump DOM Tree");
129+
auto* dump_dom_tree_action = new QAction("Dump DOM Tree", this);
130130
dump_dom_tree_action->setIcon(QIcon(QString("%1/res/icons/browser/dom-tree.png").arg(s_serenity_resource_root.characters())));
131131
debug_menu->addAction(dump_dom_tree_action);
132132
QObject::connect(dump_dom_tree_action, &QAction::triggered, this, [this] {
133133
debug_request("dump-dom-tree");
134134
});
135135

136-
auto* dump_layout_tree_action = new QAction("Dump Layout Tree");
136+
auto* dump_layout_tree_action = new QAction("Dump Layout Tree", this);
137137
dump_layout_tree_action->setIcon(QIcon(QString("%1/res/icons/16x16/layout.png").arg(s_serenity_resource_root.characters())));
138138
debug_menu->addAction(dump_layout_tree_action);
139139
QObject::connect(dump_layout_tree_action, &QAction::triggered, this, [this] {
140140
debug_request("dump-layout-tree");
141141
});
142142

143-
auto* dump_stacking_context_tree_action = new QAction("Dump Stacking Context Tree");
143+
auto* dump_stacking_context_tree_action = new QAction("Dump Stacking Context Tree", this);
144144
dump_stacking_context_tree_action->setIcon(QIcon(QString("%1/res/icons/16x16/layers.png").arg(s_serenity_resource_root.characters())));
145145
debug_menu->addAction(dump_stacking_context_tree_action);
146146
QObject::connect(dump_stacking_context_tree_action, &QAction::triggered, this, [this] {
147147
debug_request("dump-stacking-context-tree");
148148
});
149149

150-
auto* dump_style_sheets_action = new QAction("Dump Style Sheets");
150+
auto* dump_style_sheets_action = new QAction("Dump Style Sheets", this);
151151
dump_style_sheets_action->setIcon(QIcon(QString("%1/res/icons/16x16/filetype-css.png").arg(s_serenity_resource_root.characters())));
152152
debug_menu->addAction(dump_style_sheets_action);
153153
QObject::connect(dump_style_sheets_action, &QAction::triggered, this, [this] {
154154
debug_request("dump-style-sheets");
155155
});
156156

157-
auto* dump_history_action = new QAction("Dump History");
157+
auto* dump_history_action = new QAction("Dump History", this);
158158
dump_history_action->setIcon(QIcon(QString("%1/res/icons/16x16/history.png").arg(s_serenity_resource_root.characters())));
159159
debug_menu->addAction(dump_history_action);
160160
QObject::connect(dump_history_action, &QAction::triggered, this, [this] {
161161
debug_request("dump-history");
162162
});
163163

164-
auto* dump_cookies_action = new QAction("Dump Cookies");
164+
auto* dump_cookies_action = new QAction("Dump Cookies", this);
165165
dump_cookies_action->setIcon(QIcon(QString("%1/res/icons/browser/cookie.png").arg(s_serenity_resource_root.characters())));
166166
debug_menu->addAction(dump_cookies_action);
167167
QObject::connect(dump_cookies_action, &QAction::triggered, this, [this] {
168168
debug_request("dump-cookies");
169169
});
170170

171-
auto* dump_local_storage_action = new QAction("Dump Local Storage");
171+
auto* dump_local_storage_action = new QAction("Dump Local Storage", this);
172172
dump_local_storage_action->setIcon(QIcon(QString("%1/res/icons/browser/local-storage.png").arg(s_serenity_resource_root.characters())));
173173
debug_menu->addAction(dump_local_storage_action);
174174
QObject::connect(dump_local_storage_action, &QAction::triggered, this, [this] {
@@ -177,7 +177,7 @@ BrowserWindow::BrowserWindow()
177177

178178
debug_menu->addSeparator();
179179

180-
auto* show_line_box_borders_action = new QAction("Show Line Box Borders");
180+
auto* show_line_box_borders_action = new QAction("Show Line Box Borders", this);
181181
show_line_box_borders_action->setCheckable(true);
182182
debug_menu->addAction(show_line_box_borders_action);
183183
QObject::connect(show_line_box_borders_action, &QAction::triggered, this, [this, show_line_box_borders_action] {
@@ -187,14 +187,14 @@ BrowserWindow::BrowserWindow()
187187

188188
debug_menu->addSeparator();
189189

190-
auto* collect_garbage_action = new QAction("Collect Garbage");
190+
auto* collect_garbage_action = new QAction("Collect Garbage", this);
191191
collect_garbage_action->setIcon(QIcon(QString("%1/res/icons/16x16/trash-can.png").arg(s_serenity_resource_root.characters())));
192192
debug_menu->addAction(collect_garbage_action);
193193
QObject::connect(collect_garbage_action, &QAction::triggered, this, [this] {
194194
debug_request("collect-garbage");
195195
});
196196

197-
auto* clear_cache_action = new QAction("Clear Cache");
197+
auto* clear_cache_action = new QAction("Clear Cache", this);
198198
clear_cache_action->setIcon(QIcon(QString("%1/res/icons/browser/clear-cache.png").arg(s_serenity_resource_root.characters())));
199199
debug_menu->addAction(clear_cache_action);
200200
QObject::connect(clear_cache_action, &QAction::triggered, this, [this] {
@@ -243,7 +243,7 @@ BrowserWindow::BrowserWindow()
243243

244244
debug_menu->addSeparator();
245245

246-
auto* enable_scripting_action = new QAction("Enable Scripting");
246+
auto* enable_scripting_action = new QAction("Enable Scripting", this);
247247
enable_scripting_action->setCheckable(true);
248248
enable_scripting_action->setChecked(true);
249249
debug_menu->addAction(enable_scripting_action);
@@ -252,7 +252,7 @@ BrowserWindow::BrowserWindow()
252252
debug_request("scripting", state ? "on" : "off");
253253
});
254254

255-
auto* enable_same_origin_policy_action = new QAction("Enable Same-Origin Policy");
255+
auto* enable_same_origin_policy_action = new QAction("Enable Same-Origin Policy", this);
256256
enable_same_origin_policy_action->setCheckable(true);
257257
debug_menu->addAction(enable_same_origin_policy_action);
258258
QObject::connect(enable_same_origin_policy_action, &QAction::triggered, this, [this, enable_same_origin_policy_action] {

Ladybird/Settings.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88

99
namespace Browser {
1010

11-
Settings::Settings()
11+
Settings::Settings(QObject* parent)
1212
{
13-
m_qsettings = new QSettings("Serenity", "Ladybird");
13+
m_qsettings = new QSettings("Serenity", "Ladybird", parent);
1414
}
1515

1616
QString Settings::homepage()

Ladybird/Settings.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ namespace Browser {
1515

1616
class Settings {
1717
public:
18-
Settings();
18+
Settings(QObject* parent);
1919

2020
QString homepage();
2121
void set_homepage(QString const& homepage);

Ladybird/SettingsDialog.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ extern Browser::Settings* s_settings;
1414
SettingsDialog::SettingsDialog(QMainWindow* window)
1515
: m_window(window)
1616
{
17-
m_layout = new QFormLayout;
18-
m_homepage = new QLineEdit;
19-
m_ok_button = new QPushButton("&Save");
17+
m_layout = new QFormLayout(this);
18+
m_homepage = new QLineEdit(this);
19+
m_ok_button = new QPushButton("&Save", this);
2020

21-
m_layout->addWidget(new QLabel("Homepage"));
21+
m_layout->addWidget(new QLabel("Homepage", this));
2222
m_layout->addWidget(m_homepage);
2323
m_layout->addWidget(m_ok_button);
2424

Ladybird/SimpleWebView.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -586,13 +586,13 @@ void SimpleWebView::did_get_js_console_messages(i32, Vector<String>, Vector<Stri
586586
void SimpleWebView::ensure_js_console_widget()
587587
{
588588
if (!m_js_console_widget) {
589-
m_js_console_widget = new QWidget;
589+
m_js_console_widget = new QWidget(this);
590590
m_js_console_widget->setWindowTitle("JS Console");
591-
auto* layout = new QVBoxLayout;
591+
auto* layout = new QVBoxLayout(m_js_console_widget);
592592
m_js_console_widget->setLayout(layout);
593-
m_js_console_output_edit = new QTextEdit;
593+
m_js_console_output_edit = new QTextEdit(this);
594594
m_js_console_output_edit->setReadOnly(true);
595-
m_js_console_input_edit = new QLineEdit;
595+
m_js_console_input_edit = new QLineEdit(this);
596596
layout->addWidget(m_js_console_output_edit);
597597
layout->addWidget(m_js_console_input_edit);
598598
m_js_console_widget->resize(640, 480);

Ladybird/Tab.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,16 @@ extern String s_serenity_resource_root;
1919
extern Browser::Settings* s_settings;
2020

2121
Tab::Tab(BrowserWindow* window)
22-
: m_window(window)
22+
: QWidget(window)
23+
, m_window(window)
2324
{
2425
m_layout = new QBoxLayout(QBoxLayout::Direction::TopToBottom, this);
2526
m_layout->setSpacing(0);
2627
m_layout->setContentsMargins(0, 0, 0, 0);
2728

2829
m_view = new SimpleWebView;
29-
m_toolbar = new QToolBar;
30-
m_location_edit = new QLineEdit;
30+
m_toolbar = new QToolBar(this);
31+
m_location_edit = new QLineEdit(this);
3132

3233
m_hover_label = new QLabel(this);
3334
m_hover_label->hide();

Ladybird/main.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,8 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
2828
args_parser.add_positional_argument(url, "URL to open", "url", Core::ArgsParser::Required::No);
2929
args_parser.parse(arguments);
3030

31-
s_settings = new Browser::Settings();
32-
3331
BrowserWindow window;
32+
s_settings = new Browser::Settings(&window);
3433
window.setWindowTitle("Ladybird");
3534
window.resize(800, 600);
3635
window.show();

0 commit comments

Comments
 (0)