From bb71b1f07e84302b60c4c4180bb33e93b0a1c422 Mon Sep 17 00:00:00 2001 From: Thorsten von Eicken Date: Sat, 27 Jun 2015 01:04:37 -0700 Subject: [PATCH] fix memory leak --- espfs/espfs.c | 7 +++---- httpd/httpd.c | 26 +++++++++++++------------- httpd/httpdespfs.c | 5 ++++- httpd/httpdespfs.h | 2 +- user/user_main.c | 4 +++- 5 files changed, 24 insertions(+), 20 deletions(-) diff --git a/espfs/espfs.c b/espfs/espfs.c index 82ba88f..3da6692 100644 --- a/espfs/espfs.c +++ b/espfs/espfs.c @@ -47,7 +47,6 @@ It's written for use with httpd, but doesn't need to be used as such. static char* espFsData = NULL; - struct EspFsFile { EspFsHeader *header; char decompressor; @@ -147,7 +146,7 @@ EspFsFile ICACHE_FLASH_ATTR *espFsOpen(char *fileName) { return NULL; } if (h.flags&FLAG_LASTFILE) { - os_printf("End of image.\n"); + //os_printf("End of image.\n"); return NULL; } //Grab the name of the file. @@ -159,7 +158,7 @@ EspFsFile ICACHE_FLASH_ATTR *espFsOpen(char *fileName) { //Yay, this is the file we need! p+=h.nameLen; //Skip to content. r=(EspFsFile *)os_malloc(sizeof(EspFsFile)); //Alloc file desc mem -// os_printf("Alloc %p\n", r); + //os_printf("Alloc %p[%d]\n", r, sizeof(EspFsFile)); if (r==NULL) return NULL; r->header=(EspFsHeader *)hpos; r->decompressor=h.compression; @@ -266,7 +265,7 @@ void ICACHE_FLASH_ATTR espFsClose(EspFsFile *fh) { // os_printf("Freed %p\n", dec); } #endif -// os_printf("Freed %p\n", fh); + //os_printf("Freed %p\n", fh); os_free(fh); } diff --git a/httpd/httpd.c b/httpd/httpd.c index 72ccd97..3249889 100644 --- a/httpd/httpd.c +++ b/httpd/httpd.c @@ -118,16 +118,20 @@ static HttpdConnData ICACHE_FLASH_ATTR *httpdFindConnData(void *arg) { //Retires a connection for re-use static void ICACHE_FLASH_ATTR httpdRetireConn(HttpdConnData *conn) { + conn->conn = NULL; // don't try to send anything, the SDK crashes... + if (conn->cgi != NULL) conn->cgi(conn); // free cgi data + if (conn->post->buff != NULL) { + os_free(conn->post->buff); + } + conn->cgi=NULL; + conn->post->buff=NULL; + conn->remote_port = 0; + conn->remote_ip[0] = 0; + uint32 dt = conn->startTime; if (dt > 0) dt = (system_get_time() - dt)/1000; os_printf("%s Closed, %ums, heap=%ld\n", connStr, dt, (unsigned long)system_get_free_heap_size()); - if (conn->post->buff!=NULL) os_free(conn->post->buff); - conn->post->buff=NULL; - conn->cgi=NULL; - conn->conn=NULL; - conn->remote_port = 0; - conn->remote_ip[0] = 0; } //Stupid li'l helper function that returns the value of a hex char. @@ -301,8 +305,7 @@ static void ICACHE_FLASH_ATTR httpdSentCb(void *arg) { if (conn->cgi==NULL) { //Marked for destruction? //os_printf("Closing 0x%p/0x%p->0x%p\n", arg, conn->conn, conn); - espconn_disconnect(conn->conn); - //httpdRetireConn(conn); // can't call this, we will get a diconnect callback! + espconn_disconnect(conn->conn); // we will get a disconnect callback return; //No need to call xmitSendBuff. } @@ -514,19 +517,16 @@ static void ICACHE_FLASH_ATTR httpdDisconCb(void *arg) { debugConn(arg, "httpdDisconCb"); HttpdConnData *conn = httpdFindConnData(arg); if (conn == NULL) return; - if (conn->cgi != NULL) conn->cgi(conn); // free cgi data httpdRetireConn(conn); } // Callback indicating a failure in the connection. "Recon" is probably intended in the sense -// of "you need to reconnect". Sigh... +// of "you need to reconnect". Sigh... Note that there is no DiconCb after ReconCb static void ICACHE_FLASH_ATTR httpdReconCb(void *arg, sint8 err) { debugConn(arg, "httpdReconCb"); HttpdConnData *conn = httpdFindConnData(arg); - os_printf("%s reset, err=%d\n", connStr, err); + os_printf("%s ***** reset, err=%d\n", connStr, err); if (conn == NULL) return; - conn->conn = NULL; // don't tr to send anything, the SDK crashes... - if (conn->cgi != NULL) conn->cgi(conn); // free cgi data httpdRetireConn(conn); } diff --git a/httpd/httpdespfs.c b/httpd/httpdespfs.c index 0089dc5..b6853aa 100644 --- a/httpd/httpdespfs.c +++ b/httpd/httpdespfs.c @@ -34,6 +34,8 @@ int ICACHE_FLASH_ATTR cgiEspFsHook(HttpdConnData *connData) { char acceptEncodingBuffer[64]; int isGzip; + //os_printf("cgiEspFsHook conn=%p conn->conn=%p file=%p\n", connData, connData->conn, file); + if (connData->conn==NULL) { //Connection aborted. Clean up. espFsClose(file); @@ -89,7 +91,7 @@ int ICACHE_FLASH_ATTR cgiEspFsHook(HttpdConnData *connData) { } } - +#if 0 //cgiEspFsHtml is a simple HTML file that gets prefixed by head.tpl int ICACHE_FLASH_ATTR cgiEspFsHtml(HttpdConnData *connData) { EspFsFile *file = connData->cgiData; @@ -160,6 +162,7 @@ error: // error response return HTTPD_CGI_DONE; } } +#endif #if 0 //cgiEspFsTemplate can be used as a template. diff --git a/httpd/httpdespfs.h b/httpd/httpdespfs.h index 39b5bfc..fb07008 100644 --- a/httpd/httpdespfs.h +++ b/httpd/httpdespfs.h @@ -5,6 +5,6 @@ int cgiEspFsHook(HttpdConnData *connData); int ICACHE_FLASH_ATTR cgiEspFsTemplate(HttpdConnData *connData); -int ICACHE_FLASH_ATTR cgiEspFsHtml(HttpdConnData *connData); +//int ICACHE_FLASH_ATTR cgiEspFsHtml(HttpdConnData *connData); #endif diff --git a/user/user_main.c b/user/user_main.c index d7e1ba1..e8aa929 100644 --- a/user/user_main.c +++ b/user/user_main.c @@ -94,6 +94,8 @@ HttpdBuiltInUrl builtInUrls[]={ }; +#define SHOW_HEAP_USE + #ifdef SHOW_HEAP_USE static ETSTimer prHeapTimer; @@ -146,7 +148,7 @@ void user_init(void) { #ifdef SHOW_HEAP_USE os_timer_disarm(&prHeapTimer); os_timer_setfn(&prHeapTimer, prHeapTimerCb, NULL); - os_timer_arm(&prHeapTimer, 3000, 1); + os_timer_arm(&prHeapTimer, 10000, 1); #endif struct rst_info *rst_info = system_get_rst_info();