Operating System - HP-UX
1830026 Members
11452 Online
109998 Solutions
New Discussion

Re: C Land code Crash in free()

 
Dhaya
New Member

C Land code Crash in free()

Hi,

My 'C' code crashes during a file write func message(). I am a starter so pls bear with me. The code crashes when it called for 'n' times regularly. Also the same code will be called by multiple threads simultaneously. The following is the code.

void message(filename, s)
char *filename;
char *s;
{
FILE *ef;
long secs_now;
char file_name[255];
char timestr[30];

time(&secs_now);
strcpy(timestr, ctime(&secs_now));
timestr[strlen(timestr) - 1] = '\0';

if ((ef = fopen(filename,"a")) == NULL)
{
fprintf(stdout,"Could not open logfile->%s<-.\n",filename);
exit(0);
}
else
{
fprintf(ef,"\n%-25s: INFO: %s",timestr,s);
fclose(ef);
}

The gdb output is

warning: exec file is newer than core file.
Core was generated by `tin_serv'.
Program terminated with signal 10, Bus error.

warning: The shared libraries were not privately mapped; setting a
breakpoint in a shared library will not work until you rerun the program.


warning: Can't find file /prod/appl/term/bin/tin_serv referenced in dld_list.
Reading symbols from /usr/lib/libnsl.1...done.
Reading symbols from /usr/lib/libxti.2...done.
Reading symbols from /software/oracle8/product/8.0.6/lib/libclntsh.sl.1.0...
done.
Reading symbols from /usr/lib/libpthread.1...done.
Reading symbols from /usr/lib/libm.2...done.
Reading symbols from /usr/lib/libcl.2...done.
Reading symbols from /usr/lib/libisamstub.1...done.
Reading symbols from /usr/lib/librt.2...done.
Reading symbols from /usr/lib/libnss_dns.1...done.
Reading symbols from /usr/lib/libc.2...done.
Reading symbols from /usr/lib/libdld.2...done.
#0 0xc0097b9c in _sigfillset () from /usr/lib/libc.2
#0 0xc0097b9c in _sigfillset () from /usr/lib/libc.2

(gdb) bt
#0 0xc0097b9c in _sigfillset () from /usr/lib/libc.2
#1 0xc009a3a8 in free () from /usr/lib/libc.2
#2 0xc009a238 in free () from /usr/lib/libc.2
#3 0xf740 in message (filename=void, s=void) at utils.c:250
#4 0x7168 in $00000011 () at parse_req.c:558
#5 0x43cc in ProcessRequest (sockfd=7, log_file=void, tin_log_dir=void,
tin_report_dir=void, tin_template_dir=void) at process_request.c:28
#6 0x4194 in child_main (i=0, listenfd=3, addrlen=16, dbserver=void,
dbname=void, login=void, password=void, tin_log_dir=void,
tin_report_dir=void, tin_template_dir=void) at child.c:63
#7 0x3fd4 in child_make (i=0, listenfd=3, addrlen=16, dbserver=void,
dbname=void, login=void, password=void, tin_log_dir=void,
tin_report_dir=void, tin_template_dir=void) at child.c:28
#8 0x388c in main (argc=13, argv=0x7efe0d0c) at main.c:77
(gdb)

Any immediate response will do a world of help.

Thanks,
Dhayanand
Dhaya here!
2 REPLIES 2
A. Clay Stephenson
Acclaimed Contributor

Re: C Land code Crash in free()

Hi:

warning: exec file is newer than core file.

This message means that all the debugger output
is suspect. It means that you are running the debugger on an executable with a later timestamp than the core file. You need to run
the program and let it crash then debug with the current executable. Also, make sure that you turn off all optimization when you compile/link as this can skew the debugger output as well.

It is quite possible that the error is nowhere near where you think it is because these files are not in sync.


If it ain't broke, I can fix that.
Deepak Extross
Honored Contributor

Re: C Land code Crash in free()

A couple of things to note:

You are defining the variable secs_now to be a 'long integer'. But ctime() works with time_t stucture, not long integer. The operating system may typdef time_t to long, but as a developer, you should leave this to the OS.

Instead of terminating the string with a '\0' after populating it, it is recommended you do a memset before populating the string. Instead of
<<
char timestr[30];
strcpy(timestr, ctime(&secs_now));
timestr[strlen(timestr) - 1] = '\0';
>>
Do it this way:
char timestr[30];
memset (timestr, '\0', 30);
strcpy(timestr, ctime(&secs_now));