From 9921017a4b14b379d99305e17f8583ff90de0c1b Mon Sep 17 00:00:00 2001 From: fcolin Date: Thu, 18 Aug 2005 07:36:18 +0000 Subject: memory leak malloc/free cleanup --- src/Makefile.win32 | 2 +- src/ivy.c | 57 +++++++++++++++++++++++++++++++++++++----------------- src/ivyargument.c | 10 ++++++++++ src/ivybind.c | 8 ++++++-- src/ivyloop.c | 14 ++++++++------ src/ivyperf.c | 17 ++++++++++------ src/ivyprobe.c | 34 ++++++++++++++++++++------------ src/ivysocket.c | 13 +++++++------ src/list.h | 4 ++-- src/timer.c | 4 ++++ 10 files changed, 110 insertions(+), 53 deletions(-) (limited to 'src') diff --git a/src/Makefile.win32 b/src/Makefile.win32 index d3e30d0..6e7e43c 100755 --- a/src/Makefile.win32 +++ b/src/Makefile.win32 @@ -34,7 +34,7 @@ PCREOBJ = #CC=gcc #CFLAGS = -g -Wall -CFLAGS = -DWIN32 -nologo -GS -Zi #-DDEBUG +CFLAGS = -DWIN32 -D_CRTDBG_MAP_ALLOC -nologo -GS -Zi -MDd #-DDEBUG #LIBTOOL=ar q # linux and solaris #LIBTOOL=libtool -static -o LIBTOOL=lib -nologo /out: diff --git a/src/ivy.c b/src/ivy.c index 9c18baf..d4b4880 100644 --- a/src/ivy.c +++ b/src/ivy.c @@ -17,7 +17,14 @@ */ #include +#include +#include +#include +#include +#include + #ifdef WIN32 +#include #include #else #include @@ -25,13 +32,6 @@ #include #include #endif -#include -#include -#include -#include - - -#include #include "ivychannel.h" #include "ivybind.h" @@ -80,8 +80,8 @@ typedef struct _msg_snd *MsgSndPtr; struct _msg_rcv { /* requete d'emission d'un client */ IvyBindingType type; int id; - const char *regexp; /* regexp du message a recevoir */ - MsgCallback callback; /* callback a declanche a la reception */ + char *regexp; /* regexp du message a recevoir */ + MsgCallback callback; /* callback a declanche a la reception */ void *user_data; /* stokage d'info client */ }; @@ -280,8 +280,8 @@ static void MsgSendTo( Client client, MsgType msgtype, int id, int len_arg, cons static int MsgSendCallTo (Client client, const char *message, MsgSndPtr msg ) { - static void * buffer = NULL; - static int buf_len = 0; + void * buffer = NULL; + int buf_len = 0; int len; IvyArgument args; @@ -304,22 +304,39 @@ static int MsgSendCallTo (Client client, const char *message, MsgSndPtr msg ) len = IvyArgumentSerialize(args, &buf_len, &buffer, 0); MsgSendTo( client, Msg, msg->id, len, buffer ); + IvyArgumentFree( args ); /* TODO supress malloc /free */ + free( buffer ); return 1; } +static BOOL RemoveBinding(HASHKEYTYPE key, void *data, va_list args) +{ + MsgRcvPtr msg = (MsgRcvPtr)data; + free( msg->regexp ); + free ( msg ); + return FALSE; /* iter throught all hash table */ +} static void IvyCleanup() { IvyClientPtr clnt,next; - + MsgSndPtr msg,msg_next; /* destruction des connexions clients */ IVY_LIST_EACH_SAFE( clients, clnt, next ) { /* on dit au revoir */ MsgSendTo( clnt->client, Bye, 0, 0, "" ); SocketClose( clnt->client ); + IVY_LIST_EACH_SAFE( clnt->msg_send, msg, msg_next ) + { + free( msg->str_regexp ); + IvyBindingFree( msg->bind ); + } IVY_LIST_EMPTY( clnt->msg_send ); } IVY_LIST_EMPTY( clients ); - + /* destruction de mes bindings */ + hash_search( msg_recv, RemoveBinding); + hash_destroy( msg_recv ); + free( applicationUniqueId ); /* destruction des sockets serveur et supervision */ SocketServerClose( server ); SocketClose( broadcast ); @@ -578,6 +595,7 @@ static char* Receive( Client client, void *data, char *message, unsigned int len printf("Calling id=%d for %s\n", id, rcv->regexp); #endif (*rcv->callback)( clnt, rcv->user_data, arguments ); + IvyArgumentFree( arguments ); /* TODO evy , suppress malloc/free on each callback */ return ptr_end; } else @@ -621,7 +639,7 @@ static char* Receive( Client client, void *data, char *message, unsigned int len } return ptr_end; } -BOOL SendRegexp(HASHKEYTYPE key, void *data, va_list args) +static BOOL SendRegexp(HASHKEYTYPE key, void *data, va_list args) { Client client = va_arg( args, Client); MsgRcvPtr msg = (MsgRcvPtr)data; @@ -669,9 +687,10 @@ static void ClientDelete( Client client, void *data ) #endif //DEBUG if ( clnt->app_name ) free( clnt->app_name ); + if ( clnt->app_id ) free( clnt->app_id ); IVY_LIST_EACH( clnt->msg_send, msg) { - /*regfree(msg->regexp);*/ + IvyBindingFree( msg->bind ); free( msg->str_regexp); } IVY_LIST_EMPTY( clnt->msg_send ); @@ -835,6 +854,7 @@ void IvyInit (const char *appname, const char *ready, void IvyStop() { + IvyCleanup(); IvyChannelStop(); } @@ -987,8 +1007,8 @@ void IvyUnbindMsg (MsgRcvPtr msg) /* demande de reception d'un message */ static MsgRcvPtr IvyBind ( IvyBindingType typ, MsgCallback callback, void *user_data, const char *fmt_regex, va_list ap ) { - static char *buffer = NULL; - static int size = 0; + char *buffer = NULL; + int size = 0; static int recv_id = 0; IvyClientPtr clnt; MsgRcvPtr msg; @@ -1002,7 +1022,7 @@ static MsgRcvPtr IvyBind ( IvyBindingType typ, MsgCallback callback, void *user_ if (msg) { msg->type = typ; msg->id = recv_id++; - msg->regexp = strdup(buffer); + msg->regexp = buffer; msg->callback = callback; msg->user_data = user_data; if ( !hash_add(msg_recv, msg->id, msg) ) @@ -1014,6 +1034,7 @@ static MsgRcvPtr IvyBind ( IvyBindingType typ, MsgCallback callback, void *user_ else { perror("IvyBindMsg can't allocate Binding"); + free ( buffer ); exit(-1); } len = strlen(msg->regexp); diff --git a/src/ivyargument.c b/src/ivyargument.c index 5333b99..5d398d9 100755 --- a/src/ivyargument.c +++ b/src/ivyargument.c @@ -21,7 +21,9 @@ #include #include #include + #ifdef WIN32 +#include #include #else #include @@ -29,6 +31,8 @@ #include #include #endif + + #include "list.h" #include "ivyargument.h" @@ -54,6 +58,12 @@ IvyArgument IvyArgumentNew( int len, const void * value ) } void IvyArgumentFree( IvyArgument arg ) { + IvyArgument p; + IvyArgument psuiv; + IVY_LIST_EACH_SAFE( arg->childrens, p, psuiv ) + { + IvyArgumentFree(p); + } free( arg ); } void IvyArgumentGetValue( IvyArgument arg, int * len, const void **val ) diff --git a/src/ivybind.c b/src/ivybind.c index 656be62..9d95e92 100644 --- a/src/ivybind.c +++ b/src/ivybind.c @@ -15,7 +15,6 @@ * copyright notice regarding this software */ /* Module de gestion de la syntaxe des messages Ivy */ - #include #include #include @@ -24,6 +23,11 @@ #include #include +#ifdef WIN32 +#include +#endif + + #ifdef USE_PCRE_REGEX #define OVECSIZE 60 /* must be multiple of 3, for regexp return */ #include @@ -274,7 +278,7 @@ void IvyBindingParseMessage( const char *msg ) { char *val = strtok( NULL, " ="); if ( arg && val ) - hash_add( msg_args_values, (HASHKEYTYPE)arg, val ); + hash_addstring( msg_args_values, arg, val ); } } int IvyBindingFilter(IvyBindingType typ, int len, const char *exp) diff --git a/src/ivyloop.c b/src/ivyloop.c index 278e098..ab50f75 100644 --- a/src/ivyloop.c +++ b/src/ivyloop.c @@ -15,16 +15,17 @@ * copyright notice regarding this software */ -#ifdef WIN32 -#include -#endif + #include #include #include #include #include -#ifndef WIN32 +#ifdef WIN32 +#include +#include +#else #include #include #include @@ -160,6 +161,7 @@ void IvyChannelInit (void) void IvyChannelStop (void) { MainLoop = 0; + ChannelDefferedDelete(); /* this will force select to exit on multithread */ } void IvyMainLoop(void(*hook)(void)) @@ -181,12 +183,12 @@ void IvyMainLoop(void(*hook)(void)) return; } TimerScan(); - if (ready > 0) { + if ( MainLoop && ready > 0) { IvyChannelHandleExcpt(&exset); IvyChannelHandleRead(&rdset); continue; } - } + } } void IvyIdle() diff --git a/src/ivyperf.c b/src/ivyperf.c index 9eb5c02..f8e9250 100755 --- a/src/ivyperf.c +++ b/src/ivyperf.c @@ -56,26 +56,26 @@ void Reply (IvyClientPtr app, void *user_data, IvyArgument args) { IvyArgument arg; int len; - void* val; + const void* val; arg = IvyArgumentGetChildrens( args ); IvyArgumentGetValue( arg , &len, &val); - IvySendMsg ("pong ts=%.*s tr=%f", len, val, currentTime()); + IvySendMsg ("pong ts=%.*s tr=%f", len, (char*)val, currentTime()); } void Pong (IvyClientPtr app, void *user_data, IvyArgument args) { double current, ts, tr, roundtrip1, roundtrip2, roundtrip3; IvyArgument arg; int len; - void* val; + const void* val; /* TODO bug atof non limite a la longeur de la valeur !!!*/ current = currentTime(); arg = IvyArgumentGetChildrens( args ); IvyArgumentGetValue( arg , &len, &val); - ts = atof( val ); + ts = atof( (char*)val ); arg = IvyArgumentGetNextChild( arg ); IvyArgumentGetValue( arg , &len, &val); - tr = atof( val ); + tr = atof( (char*)val ); roundtrip1 = tr-ts; roundtrip2 = current - tr; roundtrip3 = current - ts; @@ -96,9 +96,14 @@ int main(int argc, char *argv[]) /* Mainloop management */ IvyInit ("IvyPerf", "IvyPerf ready", NULL,NULL,NULL,NULL); - + +#ifdef USE_REGEXP IvyBindMsg (Reply, NULL, "^ping ts=(.*)"); IvyBindMsg (Pong, NULL, "^pong ts=(.*) tr=(.*)"); +#else + IvyBindSimpleMsg (Reply, NULL, "ping ts"); + IvyBindSimpleMsg (Pong, NULL, "pong ts tr"); +#endif IvyStart (0); diff --git a/src/ivyprobe.c b/src/ivyprobe.c index bea1625..dc134a0 100644 --- a/src/ivyprobe.c +++ b/src/ivyprobe.c @@ -238,7 +238,11 @@ void HandleStdin (Channel channel, IVY_HANDLE fd, void *data) fbindcallback=0; } } else if (strcmp(cmd, "quit") == 0) { - exit(0); + IvyStop(); /* this wil exit MainLoop */ + //exit(0); +#ifdef WIN32 + ExitThread( 0 ); /* exit STDin handle thread */ +#endif } } else { cmd = strtok (buf, "\n"); @@ -255,7 +259,7 @@ DWORD WINAPI HandleStdinThread( LPVOID lpParam ) while( 1 ) HandleStdin( 0,0,0); } -void CraeteStdinThread() +void CreateStdinThread() { DWORD dwThreadId, dwThrdParam = 1; HANDLE hThread; @@ -276,6 +280,17 @@ void CraeteStdinThread() } } #endif + +void StartHandleStdin() +{ +#ifdef WIN32 + /* Stdin not compatible with select , select only accept socket */ + /* use Thread to Read StdIn */ + CreateStdinThread(); +#else + IvyChannelAdd (0, NULL, NULL, HandleStdin); +#endif +} void ApplicationCallback (IvyClientPtr app, void *user_data, IvyApplicationEvent event) { char *appname; @@ -294,8 +309,7 @@ void ApplicationCallback (IvyClientPtr app, void *user_data, IvyApplicationEvent printf("%s subscribes to '%s'\n",appname,*msgList++); /* printf("Application(%s): End Messages\n",appname);*/ if (app_count == wait_count) - - IvyChannelAdd (0, NULL, NULL, HandleStdin); + StartHandleStdin(); break; case IvyApplicationDisconnected: @@ -385,14 +399,7 @@ int main(int argc, char *argv[]) IvyBindMsg (Callback, NULL, argv[optind]); if (wait_count == 0) -#ifdef WIN32 - /* Stdin not compatible with select , select only accept socket */ - /* use Thread to Read StdIn */ - CraeteStdinThread(); -#else - IvyChannelAdd (0, NULL, NULL, HandleStdin); -#endif - + StartHandleStdin(); IvyStart (bus); @@ -408,6 +415,9 @@ int main(int argc, char *argv[]) #endif IvyMainLoop (0); + #ifdef _CRTDBG_MAP_ALLOC + _CrtDumpMemoryLeaks(); + #endif return 0; } diff --git a/src/ivysocket.c b/src/ivysocket.c index 9fac44c..d2f3259 100644 --- a/src/ivysocket.c +++ b/src/ivysocket.c @@ -14,16 +14,15 @@ * copyright notice regarding this software */ -#ifdef WIN32 -#include -#endif + #include #include #include #include #include - #ifdef WIN32 +#include +#include #define close closesocket /*#define perror (a ) printf(a" error=%d\n",WSAGetLastError());*/ #else @@ -87,6 +86,8 @@ static void DeleteSocket(void *data) (*client->handle_delete) (client, client->data ); shutdown (client->fd, 2 ); close (client->fd ); + free( client->in_buffer ); + free( client->out_buffer ); IVY_LIST_REMOVE (clients_list, client ); } @@ -95,7 +96,7 @@ static void DeleteServerSocket(void *data) Server server = (Server )data; #ifdef BUGGY_END if (server->handle_delete ) - (*server->handle_delete) (server, NULL ); + (*server->handle_delete) (server->channel, NULL ); #endif shutdown (server->fd, 2 ); close (server->fd ); @@ -301,7 +302,7 @@ void SocketServerClose (Server server ) { if (!server) return; - IvyChannelRemove (server->channel ); + IvyChannelRemove( server->channel ); } char *SocketGetPeerHost (Client client ) diff --git a/src/list.h b/src/list.h index 4441a24..ff01f13 100644 --- a/src/list.h +++ b/src/list.h @@ -82,8 +82,8 @@ #define IVY_LIST_EACH( list, p ) \ for ( p = list ; p ; p = p -> next ) -#define IVY_LIST_EACH_SAFE( list, p, next )\ -for ( p = list ; (next = p ? p->next: p ),p ; p = next ) +#define IVY_LIST_EACH_SAFE( list, p, p_next )\ +for ( p = list ; (p_next = p ? p->next: p ),p ; p = p_next ) #define IVY_LIST_EMPTY( list ) \ { \ diff --git a/src/timer.c b/src/timer.c index 9f13117..7efb3ce 100644 --- a/src/timer.c +++ b/src/timer.c @@ -15,16 +15,20 @@ */ /* Module de gestion des timers autour d'un select */ + #include #include #include #include #include + #ifdef WIN32 +#include #include #else #include #endif + #include "list.h" #include "timer.h" -- cgit v1.1