Just came across a weird problem regarding how to pass the 4th argument to pthread_create().
Originally, I wrote the code as follows:
auditLogEntry *newEntry = NULL;
// malloc and init the memory for newEntry
rc = audit_init_log_entry(&newEntry);
// wrapper of 'goto cleanup'
ERR_IF( rc != 0 );
...
rc2 = pthread_attr_init(&attr);
ERR_IF( rc2 != 0 );
rc2 = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
ERR_IF( rc2 != 0 );
rc2 = pthread_create(&syslog_thread, &attr, syslog_thread_handler, (void *)newEntry);
ERR_IF( rc2 != 0 );
newEntry = NULL;
...
cleanup:
pthread_attr_destroy(&attr);
if (newEntry != NULL)
{
audit_free_log_entry(newEntry);
newEntry = NULL;
}
static void *syslog_thread_handler(void *t)
{
auditLogEntry *entry = (auditLogEntry *)t;
... // code using entry
cleanup:
audit_free_log_entry(entry);
pthread_exit(0);
}
Everything works fine.
Then, I made a change as:
rc2 = pthread_create(&syslog_thread, &attr, syslog_thread_handler, (void *)&newEntry);
ERR_IF( rc2 != 0 );
...
cleanup:
pthread_attr_destroy(&attr);
if (rc != 0 && newEntry != NULL)
{
audit_free_log_entry(newEntry);
newEntry = NULL;
}
static void *syslog_thread_handler(void *t)
{
auditLogEntry **entry = (auditLogEntry **)t;
... // code using *entry
cleanup:
audit_free_log_entry(*entry);
*entry = NULL;
pthread_exit(0);
}
The thread handler would use *entry to access the log entry data, after the above change. But it didn't work. Worse, the process core dumped.
I tried 'man pthread_create', but found no special mentioning of how the last argument should be passed to it.
Any wrong-doing of mine, here?
newEntry = NULL;right after the pthread_create call, and have your syslog_thread_handler thread free the memory. - nos