From d16c93aa08ef8d2dcb21f9c7de9df5cf597ad22f Mon Sep 17 00:00:00 2001 From: Kaori Hagihara Date: Thu, 3 Nov 2011 17:20:00 +0000 Subject: [PATCH] solved memory leak of opj_server, and removed redundant defines --- applications/jpip/CHANGES | 4 ++ .../jpip/libopenjpip/boxheader_manager.c | 1 + applications/jpip/libopenjpip/cache_manager.c | 52 +++++++------- applications/jpip/libopenjpip/cache_manager.h | 10 +-- .../jpip/libopenjpip/cachemodel_manager.c | 5 +- .../jpip/libopenjpip/dec_clientmsg_handler.c | 70 ++++++++++++------- .../jpip/libopenjpip/imgsock_manager.c | 21 ++++-- .../jpip/libopenjpip/imgsock_manager.h | 16 +++-- applications/jpip/libopenjpip/index_manager.c | 2 +- .../jpip/libopenjpip/manfbox_manager.c | 4 +- .../jpip/libopenjpip/target_manager.c | 19 +++-- .../jpip/libopenjpip/target_manager.h | 9 +-- applications/jpip/util/jpip_to_j2k.c | 2 +- 13 files changed, 127 insertions(+), 88 deletions(-) diff --git a/applications/jpip/CHANGES b/applications/jpip/CHANGES index 04a7ed78..e920f56b 100644 --- a/applications/jpip/CHANGES +++ b/applications/jpip/CHANGES @@ -5,6 +5,10 @@ What's New for OpenJPIP ! : changed + : added +November 3, 2011 +* [kaori] solved memory leak of opj_server +! [kaori] removed redundant defines + November 2, 2011 * [antonin] additional patches for autotools and cmake diff --git a/applications/jpip/libopenjpip/boxheader_manager.c b/applications/jpip/libopenjpip/boxheader_manager.c index 2f465a3b..0a8b2215 100644 --- a/applications/jpip/libopenjpip/boxheader_manager.c +++ b/applications/jpip/libopenjpip/boxheader_manager.c @@ -59,6 +59,7 @@ boxheader_param_t * gene_boxheader( int fd, Byte8_t offset) boxlen = fetch_8bytebigendian( fd, offset+8); headlen = 16; } + boxheader = (boxheader_param_t *)malloc( sizeof( boxheader_param_t)); boxheader->headlen = headlen; boxheader->length = boxlen; diff --git a/applications/jpip/libopenjpip/cache_manager.c b/applications/jpip/libopenjpip/cache_manager.c index 597a5896..adc0ba87 100644 --- a/applications/jpip/libopenjpip/cache_manager.c +++ b/applications/jpip/libopenjpip/cache_manager.c @@ -33,9 +33,6 @@ #include #include "cache_manager.h" -//! maximum length of channel identifier -#define MAX_LENOFCID 30 - cachelist_param_t * gene_cachelist() { cachelist_param_t *cachelist; @@ -66,12 +63,11 @@ cache_param_t * gene_cache( char *targetname, int csn, char *tid, char *cid) cache_param_t *cache; cache = (cache_param_t *)malloc( sizeof(cache_param_t)); - strcpy( cache->filename, targetname); - strcpy( cache->tid, tid); + cache->filename = strdup( targetname); + cache->tid = strdup( tid); cache->csn = csn; cache->cid = (char **)malloc( sizeof(char *)); - *cache->cid = (char *)malloc( MAX_LENOFCID); - strcpy( *cache->cid, cid); + *cache->cid = strdup( cid); cache->numOfcid = 1; #if 1 cache->metadatalist = NULL; @@ -87,6 +83,9 @@ cache_param_t * gene_cache( char *targetname, int csn, char *tid, char *cid) void delete_cache( cache_param_t **cache) { int i; + + free( (*cache)->filename); + free( (*cache)->tid); delete_metadatalist( &(*cache)->metadatalist); @@ -111,6 +110,9 @@ cache_param_t * search_cache( char targetname[], cachelist_param_t *cachelist) { cache_param_t *foundcache; + if( !targetname) + return NULL; + foundcache = cachelist->first; while( foundcache != NULL){ @@ -143,6 +145,9 @@ cache_param_t * search_cacheBycid( char cid[], cachelist_param_t *cachelist) cache_param_t *foundcache; int i; + if( !cid) + return NULL; + foundcache = cachelist->first; while( foundcache != NULL){ @@ -158,6 +163,9 @@ cache_param_t * search_cacheBytid( char tid[], cachelist_param_t *cachelist) { cache_param_t *foundcache; + if( !tid) + return NULL; + foundcache = cachelist->first; while( foundcache != NULL){ @@ -170,31 +178,28 @@ cache_param_t * search_cacheBytid( char tid[], cachelist_param_t *cachelist) void add_cachecid( char *cid, cache_param_t *cache) { - char **tmp; - int i; + if( !cid) + return; - tmp = cache->cid; - - cache->cid = (char **)malloc( (cache->numOfcid+1)*sizeof(char *)); - - for( i=0; inumOfcid; i++){ - cache->cid[i] = (char *)malloc( MAX_LENOFCID); - strcpy( cache->cid[i], tmp[i]); - free( tmp[i]); + if( realloc( cache->cid, (cache->numOfcid+1)*sizeof(char *)) == NULL){ + fprintf( stderr, "failed to add new cid to cache table in add_cachecid()\n"); + return; } - free( tmp); - - cache->cid[ cache->numOfcid] = (char *)malloc( MAX_LENOFCID); - strcpy( cache->cid[ cache->numOfcid], cid); + + cache->cid[ cache->numOfcid] = strdup( cid); cache->numOfcid ++; } void update_cachetid( char *tid, cache_param_t *cache) { + if( !tid) + return; + if( tid[0] != '0' && strcmp( tid, cache->tid) !=0){ fprintf( stderr, "tid is updated to %s for %s\n", tid, cache->filename); - strcpy( cache->tid, tid); + free( cache->tid); + cache->tid = strdup( tid); } } @@ -231,8 +236,7 @@ void remove_cidInCache( char *cid, cache_param_t *cache) for( i=0, j=0; inumOfcid; i++){ if( i != idx){ - cache->cid[j] = (char *)malloc( MAX_LENOFCID); - strcpy( cache->cid[j], tmp[i]); + cache->cid[j] = strdup( tmp[i]); j++; } free( tmp[i]); diff --git a/applications/jpip/libopenjpip/cache_manager.h b/applications/jpip/libopenjpip/cache_manager.h index 61c13815..e2e2bafb 100644 --- a/applications/jpip/libopenjpip/cache_manager.h +++ b/applications/jpip/libopenjpip/cache_manager.h @@ -34,16 +34,10 @@ #include "metadata_manager.h" #include "ihdrbox_manager.h" -//! maximum length of target name -#define MAX_LENOFTARGET 128 - -//! maximum length of target identifier -#define MAX_LENOFTID 30 - //! cache parameters typedef struct cache_param{ - char filename[MAX_LENOFTARGET]; //!< file name - char tid[MAX_LENOFTID]; //!< taregt identifier + char *filename; //!< file name + char *tid; //!< taregt identifier int csn; //!< codestream number char **cid; //!< dynamic array of channel identifiers int numOfcid; //!< number of cids diff --git a/applications/jpip/libopenjpip/cachemodel_manager.c b/applications/jpip/libopenjpip/cachemodel_manager.c index 7a97ee34..ba5a3ec1 100644 --- a/applications/jpip/libopenjpip/cachemodel_manager.c +++ b/applications/jpip/libopenjpip/cachemodel_manager.c @@ -184,9 +184,8 @@ void delete_cachemodel( cachemodel_param_t **cachemodel) free( (*cachemodel)->tp_model); free( (*cachemodel)->th_model); - if( (*cachemodel)->target->codeidx->SIZ.Csiz > 1) - for( i=0; i<(*cachemodel)->target->codeidx->SIZ.Csiz; i++) - free( (*cachemodel)->pp_model[i]); + for( i=0; i<(*cachemodel)->target->codeidx->SIZ.Csiz; i++) + free( (*cachemodel)->pp_model[i]); free( (*cachemodel)->pp_model); #ifndef SERVER diff --git a/applications/jpip/libopenjpip/dec_clientmsg_handler.c b/applications/jpip/libopenjpip/dec_clientmsg_handler.c index 875e297d..294285af2 100644 --- a/applications/jpip/libopenjpip/dec_clientmsg_handler.c +++ b/applications/jpip/libopenjpip/dec_clientmsg_handler.c @@ -37,20 +37,16 @@ #include "jpipstream_manager.h" #include "jp2k_encoder.h" - -//! maximum length of channel identifier -#define MAX_LENOFCID 30 - void handle_JPIPstreamMSG( SOCKET connected_socket, cachelist_param_t *cachelist, Byte_t **jpipstream, int *streamlen, msgqueue_param_t *msgqueue) { Byte_t *newjpipstream; int newstreamlen = 0; cache_param_t *cache; - char target[MAX_LENOFTARGET], tid[MAX_LENOFTID], cid[MAX_LENOFCID]; + char *target, *tid, *cid; metadatalist_param_t *metadatalist; - newjpipstream = receive_JPIPstream( connected_socket, target, tid, cid, &newstreamlen); + newjpipstream = receive_JPIPstream( connected_socket, &target, &tid, &cid, &newstreamlen); parse_JPIPstream( newjpipstream, newstreamlen, *streamlen, msgqueue); @@ -61,11 +57,11 @@ void handle_JPIPstreamMSG( SOCKET connected_socket, cachelist_param_t *cachelist parse_metamsg( msgqueue, *jpipstream, *streamlen, metadatalist); // cid registration - if( target[0] != 0){ + if( target != NULL){ if((cache = search_cache( target, cachelist))){ - if( tid[0] != 0) + if( tid != NULL) update_cachetid( tid, cache); - if( cid[0] != 0) + if( cid != NULL) add_cachecid( cid, cache); } else{ @@ -80,6 +76,10 @@ void handle_JPIPstreamMSG( SOCKET connected_socket, cachelist_param_t *cachelist delete_metadatalist( &cache->metadatalist); cache->metadatalist = metadatalist; + if( target) free( target); + if( tid) free( tid); + if( cid) free( cid); + response_signal( connected_socket, true); } @@ -87,14 +87,19 @@ void handle_PNMreqMSG( SOCKET connected_socket, Byte_t *jpipstream, msgqueue_par { Byte_t *pnmstream; ihdrbox_param_t *ihdrbox; - char cid[MAX_LENOFCID], tmp[10]; + char *CIDorTID, tmp[10]; cache_param_t *cache; int fw, fh; - receive_line( connected_socket, cid); - if(!(cache = search_cacheBycid( cid, cachelist))) - if(!(cache = search_cacheBytid( cid, cachelist))) + CIDorTID = receive_string( connected_socket); + + if(!(cache = search_cacheBycid( CIDorTID, cachelist))) + if(!(cache = search_cacheBytid( CIDorTID, cachelist))){ + free( CIDorTID); return; + } + + free( CIDorTID); receive_line( connected_socket, tmp); fw = atoi( tmp); @@ -112,12 +117,17 @@ void handle_PNMreqMSG( SOCKET connected_socket, Byte_t *jpipstream, msgqueue_par void handle_XMLreqMSG( SOCKET connected_socket, Byte_t *jpipstream, cachelist_param_t *cachelist) { - char cid[MAX_LENOFCID]; + char *cid; cache_param_t *cache; - receive_line( connected_socket, cid); - if(!(cache = search_cacheBycid( cid, cachelist))) + cid = receive_string( connected_socket); + + if(!(cache = search_cacheBycid( cid, cachelist))){ + free( cid); return; + } + + free( cid); boxcontents_param_t *boxcontents = cache->metadatalist->last->boxcontents; Byte_t *xmlstream = (Byte_t *)malloc( boxcontents->length); @@ -128,12 +138,14 @@ void handle_XMLreqMSG( SOCKET connected_socket, Byte_t *jpipstream, cachelist_pa void handle_TIDreqMSG( SOCKET connected_socket, cachelist_param_t *cachelist) { - char target[MAX_LENOFTARGET], *tid = NULL; + char *target, *tid = NULL; cache_param_t *cache; int tidlen = 0; - receive_line( connected_socket, target); + target = receive_string( connected_socket); cache = search_cache( target, cachelist); + + free( target); if( cache){ tid = cache->tid; @@ -144,13 +156,15 @@ void handle_TIDreqMSG( SOCKET connected_socket, cachelist_param_t *cachelist) void handle_CIDreqMSG( SOCKET connected_socket, cachelist_param_t *cachelist) { - char target[MAX_LENOFTARGET], *cid = NULL; + char *target, *cid = NULL; cache_param_t *cache; int cidlen = 0; - receive_line( connected_socket, target); + target = receive_string( connected_socket); cache = search_cache( target, cachelist); + free( target); + if( cache){ if( cache->numOfcid > 0){ cid = cache->cid[ cache->numOfcid-1]; @@ -162,23 +176,29 @@ void handle_CIDreqMSG( SOCKET connected_socket, cachelist_param_t *cachelist) void handle_dstCIDreqMSG( SOCKET connected_socket, cachelist_param_t *cachelist) { - char cid[MAX_LENOFCID]; + char *cid; - receive_line( connected_socket, cid); + cid = receive_string( connected_socket); remove_cachecid( cid, cachelist); response_signal( connected_socket, true); + + free( cid); } void handle_JP2saveMSG( SOCKET connected_socket, cachelist_param_t *cachelist, msgqueue_param_t *msgqueue, Byte_t *jpipstream) { - char cid[MAX_LENOFCID]; + char *cid; cache_param_t *cache; Byte_t *jp2stream; Byte8_t jp2len; - receive_line( connected_socket, cid); - if(!(cache = search_cacheBycid( cid, cachelist))) + cid = receive_string( connected_socket); + if(!(cache = search_cacheBycid( cid, cachelist))){ + free( cid); return; + } + + free( cid); jp2stream = recons_jp2( msgqueue, jpipstream, cache->csn, &jp2len); diff --git a/applications/jpip/libopenjpip/imgsock_manager.c b/applications/jpip/libopenjpip/imgsock_manager.c index ccc2b04c..79149f5f 100644 --- a/applications/jpip/libopenjpip/imgsock_manager.c +++ b/applications/jpip/libopenjpip/imgsock_manager.c @@ -123,15 +123,13 @@ msgtype_t identify_clientmsg( SOCKET connected_socket) return MSGERROR; } -Byte_t * receive_JPIPstream( SOCKET connected_socket, char *target, char *tid, char *cid, int *streamlen) +Byte_t * receive_JPIPstream( SOCKET connected_socket, char **target, char **tid, char **cid, int *streamlen) { Byte_t *jpipstream=NULL, *ptr; char buf[BUF_LEN], versionstring[] = "version 1.2"; int linelen, redlen, remlen; - target[0] = 0; - cid[0] = 0; - tid[0] = 0; + *target = *cid = *tid = NULL; if((linelen = receive_line( connected_socket, buf)) == 0) return NULL; @@ -145,17 +143,17 @@ Byte_t * receive_JPIPstream( SOCKET connected_socket, char *target, char *tid, c if( strstr( buf, "jp2")){ // register cid option - strcpy( target, buf); + *target = strdup( buf); if((linelen = receive_line( connected_socket, buf)) == 0) return NULL; if( strcmp( buf, "0") != 0) - strcpy( tid, buf); + *tid = strdup( buf); if((linelen = receive_line( connected_socket, buf)) == 0) return NULL; if( strcmp( buf, "0") != 0) - strcpy( cid, buf); + *cid = strdup( buf); if((linelen = receive_line( connected_socket, buf)) == 0) return NULL; @@ -281,6 +279,15 @@ int receive_line(SOCKET connected_socket, char *p) return len; } +char * receive_string( SOCKET connected_socket) +{ + char buf[BUF_LEN]; + + receive_line( connected_socket, buf); + + return strdup(buf); +} + void response_signal( SOCKET connected_socket, bool succeed) { Byte_t code; diff --git a/applications/jpip/libopenjpip/imgsock_manager.h b/applications/jpip/libopenjpip/imgsock_manager.h index bb8e1bcf..5961a581 100644 --- a/applications/jpip/libopenjpip/imgsock_manager.h +++ b/applications/jpip/libopenjpip/imgsock_manager.h @@ -71,13 +71,13 @@ msgtype_t identify_clientmsg( SOCKET connected_socket); * receive a JPT- JPP- stream from client * * @param [in] connected_socket file descriptor of the connected socket - * @param [out] target received target file name (if not received, null string) - * @param [out] tid received target identifier (if not received, null string) - * @param [out] cid received channel identifier (if not received, null string) + * @param [out] target address of received target file name string pointer ( malloced, if not received, NULL) + * @param [out] tid address of received target identifier string pointer ( malloced, if not received, null string) + * @param [out] cid address of received channel identifier string pointer ( malloced, if not received, null string) * @param [out] streamlen length of the received codestream * @return JPT- JPP- codestream */ -Byte_t * receive_JPIPstream( SOCKET connected_socket, char *target, char *tid, char *cid, int *streamlen); +Byte_t * receive_JPIPstream( SOCKET connected_socket, char **target, char **tid, char **cid, int *streamlen); /** * send PGM/PPM image stream to the client @@ -135,6 +135,14 @@ void response_signal( SOCKET connected_socket, bool succeed); */ int receive_line(SOCKET connected_socket, char *buf); +/** + * receive a string line (ending with '\n') from client, return malloc string + * + * @param [in] connected_socket file descriptor of the connected socket + * @return pointer to the string (memory allocated) + */ +char * receive_string( SOCKET connected_socket); + /** * close socket * diff --git a/applications/jpip/libopenjpip/index_manager.c b/applications/jpip/libopenjpip/index_manager.c index 2f8268a5..83e2d3b5 100644 --- a/applications/jpip/libopenjpip/index_manager.c +++ b/applications/jpip/libopenjpip/index_manager.c @@ -564,7 +564,7 @@ bool set_ppixdata( box_param_t *cidx_box, index_param_t *jp2idx) free( faix_box); } - free(manf); + delete_manfbox( &manf); return true; } diff --git a/applications/jpip/libopenjpip/manfbox_manager.c b/applications/jpip/libopenjpip/manfbox_manager.c index 6b1fccef..d2b41536 100644 --- a/applications/jpip/libopenjpip/manfbox_manager.c +++ b/applications/jpip/libopenjpip/manfbox_manager.c @@ -75,12 +75,12 @@ void delete_manfbox( manfbox_param_t **manf) bhPtr = (*manf)->first; while( bhPtr != NULL){ - bhNext=bhPtr->next; + bhNext = bhPtr->next; #ifndef SERVER // fprintf( logstream, "local log: boxheader %.4s deleted!\n", bhPtr->type); #endif free(bhPtr); - bhPtr=bhNext; + bhPtr = bhNext; } free( *manf); } diff --git a/applications/jpip/libopenjpip/target_manager.c b/applications/jpip/libopenjpip/target_manager.c index 692dd59f..813c1a99 100644 --- a/applications/jpip/libopenjpip/target_manager.c +++ b/applications/jpip/libopenjpip/target_manager.c @@ -69,19 +69,19 @@ targetlist_param_t * gene_targetlist() */ int open_jp2file( char filename[]); -target_param_t * gene_target( targetlist_param_t *targetlist, char *targetname) +target_param_t * gene_target( targetlist_param_t *targetlist, char *targetpath) { target_param_t *target; int fd; index_param_t *jp2idx; static int last_csn = 0; - if( targetname[0]=='\0'){ - fprintf( FCGI_stderr, "Error: exception, no targetname in gene_target()\n"); + if( targetpath[0]=='\0'){ + fprintf( FCGI_stderr, "Error: exception, no targetpath in gene_target()\n"); return NULL; } - if((fd = open_jp2file( targetname)) == -1){ + if((fd = open_jp2file( targetpath)) == -1){ fprintf( FCGI_stdout, "Status: 404\r\n"); return NULL; } @@ -93,7 +93,7 @@ target_param_t * gene_target( targetlist_param_t *targetlist, char *targetname) target = (target_param_t *)malloc( sizeof(target_param_t)); snprintf( target->tid, MAX_LENOFTID, "%x-%x", (unsigned int)time(NULL), (unsigned int)rand()); - strcpy( target->filename, targetname); + target->filename = strdup( targetpath); target->fd = fd; target->csn = last_csn++; target->codeidx = jp2idx; @@ -109,7 +109,7 @@ target_param_t * gene_target( targetlist_param_t *targetlist, char *targetname) targetlist->last = target; #ifndef SERVER - fprintf( logstream, "local log: target %s generated\n", targetname); + fprintf( logstream, "local log: target %s generated\n", targetpath); #endif return target; @@ -129,11 +129,16 @@ void unrefer_target( target_param_t *target) void delete_target( target_param_t **target) { close( (*target)->fd); - delete_index ( &(*target)->codeidx); + + if( (*target)->codeidx) + delete_index ( &(*target)->codeidx); #ifndef SERVER fprintf( logstream, "local log: target: %s deleted\n", (*target)->filename); #endif + + free( (*target)->filename); + free(*target); } diff --git a/applications/jpip/libopenjpip/target_manager.h b/applications/jpip/libopenjpip/target_manager.h index 027ac243..a5469e79 100644 --- a/applications/jpip/libopenjpip/target_manager.h +++ b/applications/jpip/libopenjpip/target_manager.h @@ -37,13 +37,10 @@ //! maximum length of target identifier #define MAX_LENOFTID 30 -//! maximum length of target name -#define MAX_LENOFTARGET 128 - //! target parameters typedef struct target_param{ char tid[MAX_LENOFTID]; //!< taregt identifier - char filename[MAX_LENOFTARGET]; //!< file name + char *filename; //!< file name int fd; //!< file descriptor int csn; //!< codestream number index_param_t *codeidx; //!< index information of codestream @@ -74,10 +71,10 @@ targetlist_param_t * gene_targetlist(); * generate a target * * @param[in] targetlist target list to insert the generated target - * @param[in] targetname target file name + * @param[in] targetpath file path or URL of the target * @return pointer to the generated target */ -target_param_t * gene_target( targetlist_param_t *targetlist, char *targetname); +target_param_t * gene_target( targetlist_param_t *targetlist, char *targetpath); /** diff --git a/applications/jpip/util/jpip_to_j2k.c b/applications/jpip/util/jpip_to_j2k.c index 15dfc310..28876377 100644 --- a/applications/jpip/util/jpip_to_j2k.c +++ b/applications/jpip/util/jpip_to_j2k.c @@ -64,7 +64,7 @@ int main(int argc,char *argv[]) if(!( fwrite_jp2k( argv[2], dec))) return -1; - output_log( true, false, false, dec); + // output_log( true, false, false, dec); destroy_jpipdecoder( &dec);