Skip to content

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

Closed

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).

Activity

  1. AchcarLucas commented on Apr 10, 2026

    @AchcarLucas
    Author

    OBS: https://chatgpt.com/share/69d89a37-15d0-83e9-a6c4-42782ae07540
    Does this make sense? If so, a note should be made in the library about the problem.

  2. locked and limited conversation to collaborators on Apr 12, 2026
  3. converted this issue into a discussion #430 on Apr 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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