Skip to content

Using new/delete within the 'on' statement causes a memory leak. #428

@AchcarLucas

Description

@AchcarLucas

Platform

ESP32

IDE / Tooling

Arduino (IDE/CLI)

What happened?

I have the following code

AsyncCallbackWebHandler& RawRequest::onServer() {
    // PAGE 'raw data' http://[IP-DO-ESP]/raw?page={page}
    return this->webServer->on("/raw", HTTP_GET, [this](AsyncWebServerRequest *request) {
        STARTING_SERVER_PROCESSING();

        uint16_t limit = 10;
        uint16_t currentPage = 1;

        if (request->hasParam("page")) {
            currentPage = request->getParam("page")->value().toInt();
            if (currentPage < 1) {
                currentPage = 1;
            }
        }

        uint32_t totalPages = this->getDataMonitor()->getTotalPages(limit);
        std::list<Sample> samples = this->getDataMonitor()->selectSamples(currentPage, limit);

        Serial.println("Current Page: " + String(currentPage) + " Total Pages: " + String(totalPages));

        RawPage rawPage = RawPage(
            DATABASE,
            currentPage,
            totalPages,
            limit,
            &samples
        );

        request->send(200, "text/html", rawPage.page());

        FINISH_SERVER_PROCESSING();
    });
}

If rawPage is created with new

AsyncCallbackWebHandler& RawRequest::onServer() {
    // PAGE 'raw data' http://[IP-DO-ESP]/raw?page={page}
    return this->webServer->on("/raw", HTTP_GET, [this](AsyncWebServerRequest *request) {
        STARTING_SERVER_PROCESSING();

        uint16_t limit = 10;
        uint16_t currentPage = 1;

        if (request->hasParam("page")) {
            currentPage = request->getParam("page")->value().toInt();
            if (currentPage < 1) {
                currentPage = 1;
            }
        }

        uint32_t totalPages = this->getDataMonitor()->getTotalPages(limit);
        std::list<Sample> samples = this->getDataMonitor()->selectSamples(currentPage, limit);

        Serial.println("Current Page: " + String(currentPage) + " Total Pages: " + String(totalPages));

        Page *rawPage = new RawPage(
            DATABASE,
            currentPage,
            totalPages,
            limit,
            &samples
        );

        request->send(200, "text/html", rawPage.page());
        delete rawPage

        FINISH_SERVER_PROCESSING();
    });
}

Even with the delete function at the end, a memory leak still occurs.

AchcarLucas/Flow-Control-System@2ba8941

Above is the commit I had to make (converting everything to local) to remove the memory leak.

Stack Trace

Memory Leak

Minimal Reproductible Example (MRE)

Memory Leak

I confirm that:

  • I have read the documentation.
  • I have searched for similar discussions.
  • I have searched for similar issues.
  • I have looked at the examples.
  • I have upgraded to the lasted version of ESPAsyncWebServer (and AsyncTCP for ESP32).

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions