Skip to content

Commit ec5ea83

Browse files
deleting pbuf when partially consumed
1 parent c595cc0 commit ec5ea83

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

libraries/lwIpWrapper/src/lwipClient.cpp

+11-5
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ err_t lwipClient::recv_callback(struct tcp_pcb* tpcb, struct pbuf* p, err_t err)
235235

236236
if (p == NULL) {
237237
// Remote host has closed the connection -> close from our side
238-
this->stop();
238+
this->close_pcb();
239239

240240
return ERR_OK;
241241
}
@@ -345,7 +345,7 @@ void lwipClient::flush() {
345345
tcp_output(this->tcp_info->pcb);
346346
}
347347

348-
void lwipClient::stop() {
348+
void lwipClient::close_pcb() {
349349
if(this->tcp_info->pcb != nullptr) {
350350
tcp_recv(this->tcp_info->pcb, nullptr);
351351
tcp_sent(this->tcp_info->pcb, nullptr);
@@ -360,12 +360,18 @@ void lwipClient::stop() {
360360

361361
// FIXME if err != ERR_OK retry, there may be memory issues, retry?
362362
}
363+
}
363364

365+
void lwipClient::stop() {
366+
this->close_pcb();
364367
// reset all the other variables in this class
365368

366-
// if(tcp->p != nullptr) {
367-
// pbuf_free(tcp->p); // FIXME it happens that a pbuf, with ref == 0 is added for some reason
368-
// }
369+
if(this->tcp_info->pbuf_head != nullptr) {
370+
pbuf_free(this->tcp_info->pbuf_head); // FIXME it happens that a pbuf, with ref == 0 is added for some reason
371+
this->tcp_info->pbuf_head = nullptr;
372+
}
373+
this->tcp_info->pbuf_offset = 0;
374+
369375
if(this->tcp_info->server != nullptr) {
370376
// need to first make the server point to nullptr, then remove the client, can cause infinite recursion
371377
auto server = this->tcp_info->server;

libraries/lwIpWrapper/src/lwipClient.h

+1
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ class lwipClient : public arduino::Client {
108108

109109
friend err_t _lwip_tcp_recv_callback(void* arg, struct tcp_pcb* tpcb, struct pbuf* p, err_t err);
110110
friend err_t _lwip_tcp_connected_callback(void* arg, struct tcp_pcb* tpcb, err_t err);
111+
void close_pcb();
111112
};
112113

113114
inline const lwipClient CLIENT_NONE(nullptr, nullptr);

0 commit comments

Comments
 (0)