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

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

Loading…
Cancel
Save