How to get when send_P finishes in ESPAsyncWebServer

So, I'm making a webserver but to avoid memory leaks I obviously need to deallocate buffer. but how do I get when ESPAsyncWebServer finishes send_P

if (bytesRead > 0) {
  request->send_P(200, contentType, buffer, fileSize);
 } else {
   request->send(500, "text/html", "<h1>500 - Error Reading File</h1>");
 }
   delete[] buffer;
  }

In this code. buffer will get deleted too early.

This is what I used when serving a picture from ESP32-CAM. The lambda deallocates the photo buffer when the client disconnects.

void handleImageRequest(AsyncWebServerRequest *request) {
	camera_fb_t *photoBuffer { esp_camera_fb_get() };
	if (photoBuffer != nullptr) {
		Serial.printf("Got Photo, Width = %d, Height = %d\n", photoBuffer->width, photoBuffer->height);
		AsyncClient *currentClient { request->client() };
		auto returnPhotoBuffer { [photoBuffer](void *ptr, AsyncClient *client) {
			esp_camera_fb_return(photoBuffer);
		} };
		currentClient->onDisconnect(returnPhotoBuffer, nullptr);
		request->send_P(200, "image/jpeg", photoBuffer->buf, photoBuffer->len);
	} else {
		request->send(200, "text/plain", "No Photo Available");
	}
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.