From a3f209e52143e823210a2d4cef5e52a8c15639f7 Mon Sep 17 00:00:00 2001 From: Georg Hopp Date: Wed, 17 Sep 2014 20:03:26 +0100 Subject: [PATCH] fix some memory leaks --- include/tr/interface/protocol.h | 5 ++-- src/connection.c | 21 ++++++++-------- src/i_protocol.c | 4 +-- src/io_handler.c | 5 ++++ src/protocol_handler.c | 17 +++++++------ src/protocol_raw.c | 4 +-- testers/test_handler.c | 44 ++++++++++++++------------------- testers/testclient.sh | 8 +++--- testers/testserver2.c | 3 ++- 9 files changed, 57 insertions(+), 54 deletions(-) diff --git a/include/tr/interface/protocol.h b/include/tr/interface/protocol.h index 39982a6..480f607 100644 --- a/include/tr/interface/protocol.h +++ b/include/tr/interface/protocol.h @@ -35,7 +35,8 @@ typedef TR_ProtoMessage (* fptr_TR_protoCreateMessage)(void *); typedef TR_ProtoMessage (* fptr_TR_protoCreateRequest)(void *, va_list *); typedef TR_ProtoMessage (* fptr_TR_protoCreateResponse)(void *, va_list *); typedef TR_RemoteData (* fptr_TR_protoCompose)(void *, TR_ProtoMessage); -typedef size_t (* fptr_TR_protoParse)(void *, TR_ProtoMessage, TR_RemoteData); +typedef TR_RemoteData (* fptr_TR_protoParse)( + void *, TR_ProtoMessage, TR_RemoteData); TR_INTERFACE(TR_Protocol) { TR_IFID; @@ -51,7 +52,7 @@ TR_ProtoMessage TR_vprotoCreateRequest(void *, TR_Socket, va_list*); TR_ProtoMessage TR_protoCreateRequest(void *, TR_Socket, ...); TR_ProtoMessage TR_vprotoCreateResponse(void *, TR_Socket, va_list*); TR_ProtoMessage TR_protoCreateResponse(void *, TR_Socket, ...); -size_t TR_protoParse(void *, TR_ProtoMessage, TR_RemoteData); +TR_RemoteData TR_protoParse(void *, TR_ProtoMessage, TR_RemoteData); TR_RemoteData TR_protoCompose(void *, TR_ProtoMessage); #endif // __TR_INTERFACE_PROTOCOL_H__ diff --git a/src/connection.c b/src/connection.c index f3b3b16..e970b19 100644 --- a/src/connection.c +++ b/src/connection.c @@ -63,7 +63,6 @@ connectionNextMessage(void * _this, TR_RemoteData * data) TR_CommEndPoint comm = _this; TR_ProtoMessage ret_message = NULL; TR_RemoteData new_data = NULL; - size_t end; if (*data) { if (! this->current_message || this->current_message->ready) @@ -72,7 +71,15 @@ connectionNextMessage(void * _this, TR_RemoteData * data) TR_protoCreateMessage(comm->protocol, (*data)->remote); } - end = TR_protoParse(comm->protocol, this->current_message, *data); + /* + * This will return NULL if all data was consumed or a new TR_RemoteData + * with the remaining data. + * In other words, the protocol implementatio is completely responsible + * for the data management here. It can decide to copy the data, but + * then has to free the given TR_RemoteData by itself. This gives most + * flexibility when writing a protocol. + */ + new_data = TR_protoParse(comm->protocol, this->current_message, *data); /** * We define that the only valid reason for a protocol parser to not @@ -98,18 +105,12 @@ connectionNextMessage(void * _this, TR_RemoteData * data) * true and we should make it an ERROR). */ if (this->current_message->ready) { - if (end != ((TR_SizedData)*data)->size) { - new_data = TR_new( - TR_RemoteData, - ((TR_SizedData)*data)->data + end, - ((TR_SizedData)*data)->size - end, - (*data)->remote); - } ret_message = this->current_message; this->current_message = NULL; } else { - if (end != ((TR_SizedData)*data)->size) { + if (new_data) { TR_delete(*data); + TR_delete(new_data); TR_loggerLog( TR_logger, TR_LOGGER_WARNING, diff --git a/src/i_protocol.c b/src/i_protocol.c index 7f6b37e..c6329d6 100644 --- a/src/i_protocol.c +++ b/src/i_protocol.c @@ -85,10 +85,10 @@ TR_protoCreateResponse(void * _this, TR_Socket remote, ...) return callret; } -size_t +TR_RemoteData TR_protoParse(void * _this, TR_ProtoMessage message, TR_RemoteData data) { - size_t callret; + TR_RemoteData callret; TR_RETCALL(_this, TR_Protocol, parse, callret, message, data); return callret; } diff --git a/src/io_handler.c b/src/io_handler.c index 55126e2..6232b11 100644 --- a/src/io_handler.c +++ b/src/io_handler.c @@ -78,6 +78,11 @@ ioHandlerRead(void * _this, TR_Event event) return TR_EVENT_DONE; case TRUE: + if (event->subject->fin) { + TR_delete(data); + return TR_EVENT_DONE; + } + revent = TR_eventSubjectEmit( event->subject, TR_CEP_EVENT_NEW_DATA, diff --git a/src/protocol_handler.c b/src/protocol_handler.c index 8340a7c..9b58874 100644 --- a/src/protocol_handler.c +++ b/src/protocol_handler.c @@ -21,6 +21,7 @@ */ #include +#include #include "trbase.h" #include "trevent.h" @@ -55,17 +56,19 @@ protocolHandlerParse(void * _this, TR_Event event) TR_ProtoMessage message; while ((message = TR_cepNextMessage(endpoint, &data))) { - TR_eventHandlerIssueEvent( + if (! TR_eventHandlerIssueEvent( (TR_EventHandler)_this, TR_eventSubjectEmit( event->subject, TR_CEP_EVENT_NEW_MSG, - message)); - - if (message->close) { - // also check that we are a response. Well this is - // how it is done in the python code... - TR_cepSetClose(endpoint); + message))) { + TR_delete(message); + } else { + if (message->close) { + // also check that we are a response. Well this is + // how it is done in the python code... + TR_cepSetClose(endpoint); + } } } diff --git a/src/protocol_raw.c b/src/protocol_raw.c index db8d744..ce0a0b3 100644 --- a/src/protocol_raw.c +++ b/src/protocol_raw.c @@ -48,7 +48,7 @@ protocolRawCreateMessage(void * _this, TR_Socket remote) } static -size_t +TR_RemoteData protocolRawParse(void * _this, TR_ProtoMessage _message, TR_RemoteData data) { TR_ProtoMessageRaw message = (TR_ProtoMessageRaw)_message; @@ -56,7 +56,7 @@ protocolRawParse(void * _this, TR_ProtoMessage _message, TR_RemoteData data) message->data = data; _message->ready = 1; - return ((TR_SizedData)data)->size; + return NULL; } static diff --git a/testers/test_handler.c b/testers/test_handler.c index 51ef39d..e15d36a 100644 --- a/testers/test_handler.c +++ b/testers/test_handler.c @@ -11,7 +11,7 @@ static TR_EventDone testHandlerHeartbeat(TR_EventHandler this, TR_Event event) { - printf("%zd beats since last beat / handled: %llu/s\n", + printf("%zd beat(s) since last beat / handled: %llu/s\n", ((TR_EventDispatcher)event->subject)->n_beats, ((TestHandler)this)->handled); ((TestHandler)this)->handled = 0; @@ -23,22 +23,10 @@ static TR_EventDone testHandlerNewMessage(TR_EventHandler this, TR_Event event) { -// TR_ProtoMessageRaw msg = event->data; -// TR_SizedData data = (TR_SizedData)msg->data; -// char buf[data->size + 1]; -// int i; TR_Event _event; ((TestHandler)this)->handled++; -// printf("handled data %p\n", event->data); -// memcpy(buf, data->data, data->size); -// buf[data->size] = 0; -// for (i = 0; buf[i]; i++) { -// if (! isprint(buf[i])) buf[i] = '.'; -// } -// printf("echo message: %s(%zd)\n", buf, data->size); - _event = TR_eventSubjectEmit( event->subject, TR_CEP_EVENT_MSG_READY, @@ -58,14 +46,16 @@ testHandlerClose(TR_EventHandler this, TR_Event event) return TR_EVENT_PENDING; } -//static -//TR_EventDone -//testHandlerUpgrade(TR_EventHandler this, TR_Event event) -//{ -// printf("upgrade: %"PRIdPTR"\n", event->id); -// -// return TR_EVENT_PENDING; -//} +#if 0 +static +TR_EventDone +testHandlerUpgrade(TR_EventHandler this, TR_Event event) +{ + printf("upgrade: %"PRIdPTR"\n", event->id); + + return TR_EVENT_PENDING; +} +#endif static int @@ -103,11 +93,13 @@ testHandlerCvInit(TR_class_ptr class) TR_CommEndPoint, TR_CEP_EVENT_CLOSE, testHandlerClose); -// TR_EVENT_HANDLER_SET_METHOD( -// class, -// TR_CommEndPoint, -// TR_CEP_EVENT_UPGRADE, -// testHandlerUpgrade); +#if 0 + TR_EVENT_HANDLER_SET_METHOD( + class, + TR_CommEndPoint, + TR_CEP_EVENT_UPGRADE, + testHandlerUpgrade); +#endif } TR_INSTANCE(TR_Hash, testHandlerEventMethods); diff --git a/testers/testclient.sh b/testers/testclient.sh index 4df73f9..773e14b 100755 --- a/testers/testclient.sh +++ b/testers/testclient.sh @@ -1,9 +1,9 @@ #!/bin/sh BS=8192 -COUNT=10000 -CONCURENT=200 -IP="192.168.2.13" +COUNT=1000000 +CONCURENT=20 +IP="localhost" pids="" i=0 @@ -12,7 +12,7 @@ MESSAGE="GET / HTTP/1.1\r\nConnection: keep-alive\r\n\r\n" while [ $i -lt ${CONCURENT} ] do dd if=/dev/zero bs=${BS} count=${COUNT} | nc -q 1 ${IP} 5678 >/dev/null & - #echo -en "${MESSAGE}" | nc -q 1 ${IP} 5678 & + #echo -en "${MESSAGE}" | nc -q 1 -u ${IP} 5678 & pids="${pids} $!" i=$((i + 1)) diff --git a/testers/testserver2.c b/testers/testserver2.c index 584be80..ded7fc1 100644 --- a/testers/testserver2.c +++ b/testers/testserver2.c @@ -10,6 +10,7 @@ #include "test_handler.h" TR_INSTANCE(TR_LoggerSyslog, mylogger, {TR_LOGGER_INFO}); +TR_INSTANCE(TR_LoggerStderr, mylogger2, {TR_LOGGER_INFO}); int main (int argc, char * argv[]) @@ -18,7 +19,7 @@ main (int argc, char * argv[]) TR_Protocol protocol = TR_new(TR_ProtocolRaw); TestHandler test_handler = TR_new(TestHandler); - TR_logger = TR_INSTANCE_CAST(TR_Logger, mylogger); + TR_logger = TR_INSTANCE_CAST(TR_Logger, mylogger2); TR_serverAddHandler(server, (TR_EventHandler)test_handler); TR_serverBindTcp(server, "0.0.0.0", 5678, protocol);