libupnp/upnp/sample/tvdevice/linux/upnp_tv_device_main.c
Marcelo Roberto Jimenez 6aa2419cfd SF Bug Tracker [ 2392166 ] ithread_detach not called for finished worker thread
Submitted: Ulrik ( ulsv_enea ) - 2008-12-05 08:24

	Valgrind reports a memory leak due to that the function ithread_detach is
	not called for finished worker threads in ThreadPool.c.

	==21137== 2,176 bytes in 8 blocks are possibly lost in loss record 5 of 5
	==21137== at 0x4C20F3F: calloc (vg_replace_malloc.c:279)
	==21137== by 0x4010F58: _dl_allocate_tls (in /lib/ld-2.6.1.so)
	==21137== by 0x544BA92: pthread_create@@GLIBC_2.2.5 (in
	/lib/libpthread-2.6.1.so)
	==21137== by 0x5F94592: CreateWorker (ThreadPool.c:639)
	==21137== by 0x5F95079: ThreadPoolInit (ThreadPool.c:784)

	I'm using libupnp 1.6.6

	For more info on pthread_detach, see:
	http://gelorakan.wordpress.com/2007/11/26/pthead_create-valgrind-memory-lea
	k-solved/



git-svn-id: https://pupnp.svn.sourceforge.net/svnroot/pupnp/branches/branch-1.6.x@520 119443c7-1b9e-41f8-b6fc-b9c35fce742c
2010-03-21 22:41:49 +00:00

198 lines
6.4 KiB
C

/*******************************************************************************
*
* Copyright (c) 2000-2003 Intel Corporation
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* - Neither name of Intel Corporation nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL INTEL OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
******************************************************************************/
#include "sample_util.h"
#include "upnp_tv_device.h"
#include <stdio.h>
/******************************************************************************
* linux_print
*
* Description:
* Prints a string to standard out.
*
* Parameters:
* None
*
*****************************************************************************/
void
linux_print( const char *string )
{
printf( "%s", string );
}
/******************************************************************************
* TvDeviceCommandLoop
*
* Description:
* Function that receives commands from the user at the command prompt
* during the lifetime of the device, and calls the appropriate
* functions for those commands. Only one command, exit, is currently
* defined.
*
* Parameters:
* None
*
*****************************************************************************/
void *
TvDeviceCommandLoop( void *args )
{
int stoploop = 0;
char cmdline[100];
char cmd[100];
while( !stoploop ) {
sprintf( cmdline, " " );
sprintf( cmd, " " );
SampleUtil_Print( "\n>> " );
// Get a command line
fgets( cmdline, 100, stdin );
sscanf( cmdline, "%s", cmd );
if( strcasecmp( cmd, "exit" ) == 0 ) {
SampleUtil_Print( "Shutting down...\n" );
TvDeviceStop();
exit( 0 );
} else {
SampleUtil_Print( "\n Unknown command: %s\n\n", cmd );
SampleUtil_Print( " Valid Commands:\n" );
SampleUtil_Print( " Exit\n\n" );
}
}
return NULL;
}
/******************************************************************************
* main
*
* Description:
* Main entry point for tv device application.
* Initializes and registers with the sdk.
* Initializes the state stables of the service.
* Starts the command loop.
*
* Parameters:
* int argc - count of arguments
* char ** argv -arguments. The application
* accepts the following optional arguments:
*
* -ip ipaddress
* -port port
* -desc desc_doc_name
* -webdir web_dir_path"
* -help
*
*
*****************************************************************************/
int main( IN int argc, IN char **argv )
{
unsigned int portTemp = 0;
char *ip_address = NULL,
*desc_doc_name = NULL,
*web_dir_path = NULL;
int rc;
ithread_t cmdloop_thread;
#ifndef WIN32
int sig;
sigset_t sigs_to_catch;
#endif
int code;
unsigned int port = 0;
int i = 0;
SampleUtil_Initialize( linux_print );
// Parse options
for( i = 1; i < argc; i++ ) {
if( strcmp( argv[i], "-ip" ) == 0 ) {
ip_address = argv[++i];
} else if( strcmp( argv[i], "-port" ) == 0 ) {
sscanf( argv[++i], "%u", &portTemp );
} else if( strcmp( argv[i], "-desc" ) == 0 ) {
desc_doc_name = argv[++i];
} else if( strcmp( argv[i], "-webdir" ) == 0 ) {
web_dir_path = argv[++i];
} else if( strcmp( argv[i], "-help" ) == 0 ) {
SampleUtil_Print( "Usage: %s -ip ipaddress -port port"
" -desc desc_doc_name -webdir web_dir_path"
" -help (this message)\n", argv[0] );
SampleUtil_Print( "\tipaddress: IP address of the device"
" (must match desc. doc)\n" );
SampleUtil_Print( "\t\te.g.: 192.168.0.4\n" );
SampleUtil_Print( "\tport: Port number to use for "
"receiving UPnP messages (must match desc. doc)\n" );
SampleUtil_Print( "\t\te.g.: 5431\n" );
SampleUtil_Print
( "\tdesc_doc_name: name of device description document\n" );
SampleUtil_Print( "\t\te.g.: tvdevicedesc.xml\n" );
SampleUtil_Print
( "\tweb_dir_path: Filesystem path where web files "
"related to the device are stored\n" );
SampleUtil_Print( "\t\te.g.: /upnp/sample/tvdevice/web\n" );
return 1;
}
}
port = ( unsigned short )portTemp;
TvDeviceStart( ip_address, port, desc_doc_name, web_dir_path, linux_print );
/* start a command loop thread */
code = ithread_create( &cmdloop_thread, NULL, TvDeviceCommandLoop, NULL );
#ifndef WIN32
/*
Catch Ctrl-C and properly shutdown
*/
sigemptyset( &sigs_to_catch );
sigaddset( &sigs_to_catch, SIGINT );
sigwait( &sigs_to_catch, &sig );
SampleUtil_Print( "Shutting down on signal %d...\n", sig );
#else
ithread_join(cmdloop_thread, NULL);
#endif
rc = TvDeviceStop();
return rc;
}