From 0e19d501bbf4d96bc622436a161b57ff7445a657 Mon Sep 17 00:00:00 2001 From: Rainer Gerhards Date: Thu, 24 Apr 2008 10:54:51 +0200 Subject: fixed newly introduced memory leaks --- runtime/nsd_ptcp.c | 2 -- runtime/nssel.c | 2 ++ tcps_sess.c | 8 +++++--- tcps_sess.h | 3 +-- tcpsrv.c | 24 +++++++++++++++++++----- 5 files changed, 27 insertions(+), 12 deletions(-) diff --git a/runtime/nsd_ptcp.c b/runtime/nsd_ptcp.c index 8dbc80d9..8ab2ccf7 100644 --- a/runtime/nsd_ptcp.c +++ b/runtime/nsd_ptcp.c @@ -259,7 +259,6 @@ LstnInit(netstrms_t *pNS, void *pUsr, rsRetVal(*fAddLstn)(void*,netstrm_t*), uchar *pLstnPort, uchar *pLstnIP, int iSessMax) { DEFiRet; - nsd_ptcp_t **arrLstn = NULL; netstrm_t *pNewStrm = NULL; nsd_t *pNewNsd = NULL; int error, maxs, on = 1; @@ -289,7 +288,6 @@ LstnInit(netstrms_t *pNS, void *pUsr, rsRetVal(*fAddLstn)(void*,netstrm_t*), /* Count max number of sockets we may open */ for(maxs = 0, r = res; r != NULL ; r = r->ai_next, maxs++) /* EMPTY */; - CHKmalloc(arrLstn = (nsd_ptcp_t**) malloc((maxs+1) * sizeof(nsd_ptcp_t*))); numSocks = 0; /* num of sockets counter at start of array */ for(r = res; r != NULL ; r = r->ai_next) { diff --git a/runtime/nssel.c b/runtime/nssel.c index c4f6691e..7cb63e98 100644 --- a/runtime/nssel.c +++ b/runtime/nssel.c @@ -89,6 +89,8 @@ ENDobjConstruct(nssel) /* destructor for the nssel object */ BEGINobjDestruct(nssel) /* be sure to specify the object type also in END and CODESTART macros! */ CODESTARTobjDestruct(nssel) + if(pThis->pDrvrData != NULL) + pThis->Drvr.Destruct(&pThis->pDrvrData); ENDobjDestruct(nssel) diff --git a/tcps_sess.c b/tcps_sess.c index 27bdbf89..33c13aa0 100644 --- a/tcps_sess.c +++ b/tcps_sess.c @@ -100,6 +100,10 @@ ENDobjDebugPrint(tcps_sess) /* set property functions */ +/* set the hostname. Note that the caller *hands over* the string. That is, + * the caller no longer controls it once SetHost() has received it. Most importantly, + * the caller must not free it. -- gerhards, 2008-04-24 + */ static rsRetVal SetHost(tcps_sess_t *pThis, uchar *pszHost) { @@ -109,11 +113,9 @@ SetHost(tcps_sess_t *pThis, uchar *pszHost) if(pThis->fromHost != NULL) { free(pThis->fromHost); - pThis->fromHost = NULL; } - if((pThis->fromHost = strdup((char*)pszHost)) == NULL) - ABORT_FINALIZE(RS_RET_OUT_OF_MEMORY); + pThis->fromHost = pszHost; finalize_it: RETiRet; diff --git a/tcps_sess.h b/tcps_sess.h index 7baa035a..9f3d10d6 100644 --- a/tcps_sess.h +++ b/tcps_sess.h @@ -32,7 +32,6 @@ struct tcpsrv_s; typedef struct tcps_sess_s { BEGINobjInstance; /* Data to implement generic object - MUST be the first data element! */ struct tcpsrv_s *pSrv; /* pointer back to my server (e.g. for callbacks) */ - //int sock; // TODO: remove netstrm_t *pStrm; int iMsg; /* index of next char to store in msg */ int bAtStrtOfFram; /* are we at the very beginning of a new frame? */ @@ -44,7 +43,7 @@ typedef struct tcps_sess_s { int iOctetsRemain; /* Number of Octets remaining in message */ TCPFRAMINGMODE eFraming; char msg[MAXLINE+1]; - char *fromHost; + uchar *fromHost; void *pUsr; /* a user-pointer */ } tcps_sess_t; diff --git a/tcpsrv.c b/tcpsrv.c index 2536cdd6..086d17b8 100644 --- a/tcpsrv.c +++ b/tcpsrv.c @@ -276,7 +276,6 @@ create_tcp_socket(tcpsrv_t *pThis) /* TODO: add capability to specify local listen address! */ CHKiRet(netstrm.LstnInit(pThis->pNS, (void*)pThis, addTcpLstn, TCPLstnPort, NULL, pThis->iSessMax)); - /* OK, we had success. Now it is also time to * initialize our connections */ @@ -312,7 +311,7 @@ SessAccept(tcpsrv_t *pThis, tcps_sess_t **ppSess, netstrm_t *pStrm) netstrm_t *pNewStrm = NULL; int iSess = -1; struct sockaddr_storage addr; - uchar *fromHostFQDN; + uchar *fromHostFQDN = NULL; ISOBJ_TYPE_assert(pThis, tcpsrv); @@ -382,6 +381,14 @@ finalize_it: } +static void +RunCancelCleanup(void *arg) +{ + nssel_t **ppSel = (nssel_t**) arg; + + if(*ppSel != NULL) + nssel.Destruct(ppSel); +} /* This function is called to gather input. */ static rsRetVal Run(tcpsrv_t *pThis) @@ -397,10 +404,11 @@ Run(tcpsrv_t *pThis) ISOBJ_TYPE_assert(pThis, tcpsrv); - /* this is an endless loop - it is terminated when the thread is - * signalled to do so. This, however, is handled by the framework, - * right into the sleep below. + /* this is an endless loop - it is terminated by the framework canelling + * this thread. Thus, we also need to instantiate a cancel cleanup handler + * to prevent us from leaking anything. -- rgerharsd, 20080-04-24 */ + pthread_cleanup_push(RunCancelCleanup, (void*) &pSel); while(1) { CHKiRet(nssel.Construct(&pSel)); // TODO: set driver @@ -466,8 +474,12 @@ Run(tcpsrv_t *pThis) } iTCPSess = TCPSessGetNxtSess(pThis, iTCPSess); } + CHKiRet(nssel.Destruct(&pSel)); } + /* note that this point is usually not reached */ + pthread_cleanup_pop(0); /* remove cleanup handler */ + finalize_it: // TODO: think: is it really good to exit the loop? RETiRet; } @@ -514,6 +526,8 @@ CODESTARTobjDestruct(tcpsrv) if(pThis->pNS != NULL) netstrms.Destruct(&pThis->pNS); + if(pThis->ppLstn != NULL) + free(pThis->ppLstn); ENDobjDestruct(tcpsrv) -- cgit