@@ -17,8 +17,6 @@ static inline bool isParamChar(char c) {
1717 return ((c) && ((c) != ' {' ) && ((c) != ' [' ) && ((c) != ' &' ) && ((c) != ' =' ));
1818}
1919
20- static void doNotDelete (AsyncWebServerRequest *) {}
21-
2220using namespace asyncsrv ;
2321
2422enum {
@@ -108,8 +106,6 @@ AsyncWebServerRequest::~AsyncWebServerRequest() {
108106 _response = nullptr ;
109107 }
110108
111- _this.reset ();
112-
113109 if (_tempObject != NULL ) {
114110 free (_tempObject);
115111 }
@@ -124,6 +120,8 @@ AsyncWebServerRequest::~AsyncWebServerRequest() {
124120}
125121
126122void AsyncWebServerRequest::_onData (void *buf, size_t len) {
123+ std::shared_ptr<AsyncWebServerRequest> self = _this; // Ensure we stay in scope over the function
124+
127125 // SSL/TLS handshake detection
128126#ifndef ASYNC_TCP_SSL_ENABLED
129127 if (_parseState == PARSE_REQ_START && len && ((uint8_t *)buf)[0 ] == 0x16 ) { // 0x16 indicates a Handshake message (SSL/TLS).
@@ -240,6 +238,7 @@ void AsyncWebServerRequest::_onData(void *buf, size_t len) {
240238}
241239
242240void AsyncWebServerRequest::_onPoll () {
241+ std::shared_ptr<AsyncWebServerRequest> self = _this; // Ensure we stay in scope over the function
243242 // os_printf("p\n");
244243 if (_response && _client && _client->canSend ()) {
245244 _response->_ack (this , 0 , 0 );
@@ -252,6 +251,8 @@ void AsyncWebServerRequest::_onAck(size_t len, uint32_t time) {
252251 return ;
253252 }
254253
254+ std::shared_ptr<AsyncWebServerRequest> self = _this; // Ensure we stay in scope over the function
255+
255256 if (!_response->_finished ()) {
256257 _response->_ack (this , len, time);
257258 // recheck if response has just completed, close connection
@@ -271,6 +272,7 @@ void AsyncWebServerRequest::_onError(int8_t error) {
271272void AsyncWebServerRequest::_onTimeout (uint32_t time) {
272273 (void )time;
273274 // os_printf("TIMEOUT: %u, state: %s\n", time, _client->stateToString());
275+ // We do not need to lock the shared pointer here as we do no work after closing the client
274276 _client->close ();
275277}
276278
@@ -283,7 +285,7 @@ void AsyncWebServerRequest::_onDisconnect() {
283285 if (_onDisconnectfn) {
284286 _onDisconnectfn ();
285287 }
286- _server-> _handleDisconnect ( this );
288+ this -> _this . reset (); // release shared pointer to this request, allowing for object destruction
287289}
288290
289291void AsyncWebServerRequest::_addGetParams (const String ¶ms) {
@@ -1054,9 +1056,6 @@ AsyncWebServerRequestPtr AsyncWebServerRequest::pause() {
10541056 return _this;
10551057 }
10561058 client ()->setRxTimeout (0 );
1057- // this shared ptr will hold the request pointer until it gets destroyed following a disconnect.
1058- // this is just used as a holder providing weak observers, so the deleter is a no-op.
1059- _this = std::shared_ptr<AsyncWebServerRequest>(this , doNotDelete);
10601059 _paused = true ;
10611060 return _this;
10621061}
@@ -1065,7 +1064,6 @@ void AsyncWebServerRequest::abort() {
10651064 if (!_sent) {
10661065 _sent = true ;
10671066 _paused = false ;
1068- _this.reset ();
10691067 async_ws_log_v (" Abort request: %s" , _url.c_str ());
10701068 _client->abort ();
10711069 }
@@ -1327,7 +1325,9 @@ void AsyncWebServerRequest::requestAuthentication(AsyncAuthType method, const ch
13271325 r->addHeader (T_WWW_AUTH , header.c_str ());
13281326 } else {
13291327 async_ws_log_e (" Failed to allocate" );
1328+ delete r;
13301329 abort ();
1330+ return ;
13311331 }
13321332
13331333 break ;
@@ -1351,7 +1351,9 @@ void AsyncWebServerRequest::requestAuthentication(AsyncAuthType method, const ch
13511351 r->addHeader (T_WWW_AUTH , header.c_str ());
13521352 } else {
13531353 async_ws_log_e (" Failed to allocate" );
1354+ delete r;
13541355 abort ();
1356+ return ;
13551357 }
13561358 }
13571359 break ;
0 commit comments