Fix memory leak on destruction and make use of unique_ptr.

The _currentPageElement was not deleted on destruction. This caused a
memory leak. Furthermore there was an explicit destructor call which should
never be used. Now the memory handling is delegated to a unique_ptr which
makes it easier to track.
pull/144/head
Sven Steckmann 5 years ago
parent 02e98f54f4
commit a4cc82c808
  1. 24
      src/AutoConnect.cpp
  2. 2
      src/AutoConnect.h

@ -361,11 +361,7 @@ void AutoConnect::home(const String& uri) {
*/ */
void AutoConnect::end(void) { void AutoConnect::end(void) {
_responsePage.reset(); _responsePage.reset();
_currentPageElement.reset();
if (_currentPageElement != nullptr) {
_currentPageElement->~PageElement();
_currentPageElement = nullptr;
}
_stopPortal(); _stopPortal();
_dnsServer.reset(); _dnsServer.reset();
@ -912,12 +908,13 @@ bool AutoConnect::_classifyHandle(HTTPMethod method, String uri) {
_purgePages(); _purgePages();
// Create the page dynamically // Create the page dynamically
if ((_currentPageElement = _setupPage(uri)) == nullptr) _currentPageElement.reset( _setupPage(uri) );
if (_aux) { if (!_currentPageElement && _aux) {
// Requested URL is not a normal page, exploring AUX pages // Requested URL is not a normal page, exploring AUX pages
_currentPageElement = _aux->_setupPage(uri); _currentPageElement.reset(_aux->_setupPage(uri));
} }
if (_currentPageElement != nullptr) {
if (_currentPageElement) {
AC_DBG_DUMB(",generated:%s", uri.c_str()); AC_DBG_DUMB(",generated:%s", uri.c_str());
_uri = uri; _uri = uri;
_responsePage->addElement(*_currentPageElement); _responsePage->addElement(*_currentPageElement);
@ -947,9 +944,8 @@ void AutoConnect::_handleUpload(const String& requestUri, const HTTPUpload& uplo
*/ */
void AutoConnect::_purgePages(void) { void AutoConnect::_purgePages(void) {
_responsePage->clearElement(); _responsePage->clearElement();
if (_currentPageElement != nullptr) { if (_currentPageElement) {
delete _currentPageElement; _currentPageElement.reset();
_currentPageElement = nullptr;
_uri = String(""); _uri = String("");
} }
} }

@ -275,7 +275,7 @@ class AutoConnect {
* menu page corresponding to the URI is generated. * menu page corresponding to the URI is generated.
*/ */
std::unique_ptr<PageBuilder> _responsePage; std::unique_ptr<PageBuilder> _responsePage;
PageElement* _currentPageElement = nullptr; std::unique_ptr<PageElement> _currentPageElement;
/** Extended pages made up with AutoConnectAux */ /** Extended pages made up with AutoConnectAux */
AutoConnectAux* _aux = nullptr; /**< A top of registered AutoConnectAux */ AutoConnectAux* _aux = nullptr; /**< A top of registered AutoConnectAux */

Loading…
Cancel
Save