Skip to content

Commit 5ebde96

Browse files
committed
Allocate _tempObject in a way that permits automatic deallocation
1 parent 7bf8437 commit 5ebde96

1 file changed

Lines changed: 7 additions & 5 deletions

File tree

examples/ChunkRequest/ChunkRequest.ino

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,8 @@ void handleRequest(AsyncWebServerRequest *request) {
7878
state->outFile.close();
7979
request->send(201); // Created
8080
}
81-
// Finally, release the resources used by state
82-
delete state;
83-
request->_tempObject = nullptr;
81+
// The resources used by state will be automatically freed
82+
// when the framework frees the _tempObject pointer
8483
return;
8584
}
8685

@@ -112,8 +111,11 @@ void handleBody(AsyncWebServerRequest *request, uint8_t *data, size_t len, size_
112111
// parse the url to a proper path
113112
String path = request->url();
114113

115-
state = new RequestState{File()};
116-
request->_tempObject = static_cast<void *>(state);
114+
// Allocate the _tempObject memory
115+
request->_tempObject = std::malloc(sizeof(RequestState));
116+
117+
// Use placement new to construct the RequestState object therein
118+
RequestState *state = new (request->_tempObject) RequestState{File()};
117119

118120
if (total) {
119121
#ifdef ESP32

0 commit comments

Comments
 (0)