I am trying to print value of argument which is provided to the thread function "monitor_loglevel_change_handler" as a last param in pthread_create() but it prints garbage instead of proper value. However, that is provided and printed properly earlier in the code see the comments
****(Updated my code further with minimum reproducible example as asked by others. Note I have put sleep intentionally in the main program so that thread function gets chance to run)
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<pthread.h>
#include<sys/inotify.h>
#include <errno.h>
#include <sys/stat.h>
char *read_env_param(const char*envkey)
{
if(envkey)
{
char *value = getenv(envkey);
if(value)
return strdup(value);
}
return NULL;
}
static void * monitor_loglevel_change_handler(void* arg)
{
char *fileName = (char*) arg;
int ifd; // the inotify file des
int wfd; // the watched file des
ssize_t n = 0;
char* dname=NULL; // directory name
char* bname = NULL; // basename
char* tok=NULL;
printf("Value of arguments: %s\n", (char*) arg); // --------> prints nothing why??
dname = strdup( fileName); // defrock the file name into dir and basename
if( (tok = strrchr( dname, '/' )) != NULL ) {
*tok = '\0';
bname = strdup( tok+1 );
}
ifd = inotify_init1( 0 ); // initialise watcher setting blocking read (no option)
if( ifd < 0 ) {
fprintf( stderr, "### ERR ### unable to initialise file watch %s\n", strerror( errno ) );
} else {
wfd = inotify_add_watch( ifd, dname, IN_MOVED_TO | IN_CLOSE_WRITE ); // we only care about close write changes
}
}
static int register_log_change_notify(const char *fileName)
{
pthread_attr_t cb_attr;
pthread_t tid;
pthread_attr_init(&cb_attr);
pthread_attr_setdetachstate(&cb_attr,PTHREAD_CREATE_DETACHED);
printf("file name is %s\n", fileName);
return pthread_create(&tid, &cb_attr,&monitor_loglevel_change_handler,(void *)fileName);
}
int enable_log_change_notify(const char* fileName)
{
int ret = -1;
struct stat fileInfo;
printf("filename is %s\n", fileName); // ----> prints properly
if ( lstat(fileName,&fileInfo) == 0 )
{
ret = register_log_change_notify(fileName);
}
return ret;
}
void dynamic_log_level_change()
{
char *logFile_Name = read_env_param("TESTSHA");
char* log_level_init=NULL;
if(logFile_Name)
{
printf("logFile_Name is :%s\n", logFile_Name);
free(log_level_init);
}
enable_log_change_notify(logFile_Name);
free(logFile_Name);
}
void init_log() {
int log_change_monitor = 0;
dynamic_log_level_change();
}
int main()
{
init_log();
sleep(50);
}
full code of above is here sctpThread.cpp
free(logFile_Name);
You are freeing the memory, it is no longer valid.
The simple solution is to transfer ownership for the memory to the thread.
return pthread_create(&tid, &cb_attr,&monitor_loglevel_change_handler,
// allocate new memory dynamically
strdup(fileName));
And then the thread is responsible for free-ing:
static void * monitor_loglevel_change_handler(void* arg) {
.... blabla .......
free(arg);
}