From 7ce989d12b2858026d484a2add91e089608bcdb2 Mon Sep 17 00:00:00 2001 From: Relintai Date: Thu, 18 Nov 2021 12:07:28 +0100 Subject: [PATCH] Don't leak memory. --- core/html/html_parser.cpp | 29 ++++++++++++++++++++--------- core/html/html_parser.h | 2 +- 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/core/html/html_parser.cpp b/core/html/html_parser.cpp index 9fb3385..c778ac7 100644 --- a/core/html/html_parser.cpp +++ b/core/html/html_parser.cpp @@ -230,6 +230,13 @@ HTMLParserTag::HTMLParserTag() { } HTMLParserTag::~HTMLParserTag() { + for (int i = 0; i < tags.size(); ++i) { + delete tags[i]; + } + + for (int i = 0; i < attributes.size(); ++i) { + delete attributes[i]; + } } void HTMLParser::parse(const String &data) { @@ -244,7 +251,6 @@ void HTMLParser::parse(const String &data) { t->data = data.substr(i, j - i + 1); t->process(); - t->print(); tags.push_back(t); @@ -270,25 +276,30 @@ void HTMLParser::parse(const String &data) { } //process tags into hierarchical order - //Vector tag_stack; - //for (int i = 0; i < tags.size(); ++i) { - //} - + Vector tag_stack; for (int i = 0; i < tags.size(); ++i) { - delete tags[i]; } } String HTMLParser::to_string() { - return html->to_string(); + if (!root) { + return ""; + } + + return root->to_string(); } void HTMLParser::print() { - html->print(); + if (root) { + root->print(); + } } HTMLParser::HTMLParser() { - html = nullptr; + root = nullptr; } HTMLParser::~HTMLParser() { + if (root) { + delete root; + } } diff --git a/core/html/html_parser.h b/core/html/html_parser.h index 98b5780..24213a2 100644 --- a/core/html/html_parser.h +++ b/core/html/html_parser.h @@ -49,7 +49,7 @@ public: class HTMLParser { public: - HTMLParserTag *html; + HTMLParserTag *root; void parse(const String &data); //void parse_tag(const String &data, const int index);