B
bumps
Guys,
I am getting segmentation fault while I am trying to open a file in a
small function.
int PingSamplesList::saveListElements (char *outputFileString)
{
PingSampleElement *tmpPtr;
int rc = 0;
ofstream outFile;
char lineBuffer[2048];
char *tmpLine = lineBuffer;
cout << "PingSamplesList::saveListElements to file: " <<
outputFileString;
outFile.open(outputFileString, ios:ut | ios::app);
tmpPtr = (PingSampleElement *)head;
while (tmpPtr != NULL)
{
sprintf(tmpLine, "%12.6f %9lf %9lf %9lf %0.4f
%9lf %9lf %9lf %0.4f \n",
tmpPtr->timeStamp,
tmpPtr->up_p_estimate,
tmpPtr->up_p_d_estimate,
tmpPtr->up_p_d_new_estimate,
tmpPtr->up_LossRate,
tmpPtr->dn_p_estimate,
tmpPtr->dn_p_d_estimate,
tmpPtr->dn_p_d_new_estimate,
tmpPtr->dn_LossRate
);
outFile << tmpLine ;
tmpPtr = (PingSampleElement *) tmpPtr->next;
}
outFile.close();
return rc;
}
When the function gets called the first time, it works perfectly. But
when it gets called the second time, it crashes with segmentation
faults. With some debugging statements I found that the program crashes
in the line
outFile.open(outputFileString, ios:ut | ios::app);
and outputFileString variable is NOT NULL.
Backtrace from GDB
#0 0x420741df in malloc_consolidate () from /lib/i686/libc.so.6
#1 0x42073ab7 in _int_malloc () from /lib/i686/libc.so.6
#2 0x42073155 in malloc () from /lib/i686/libc.so.6
#3 0x400ab2fe in operator new(unsigned) (sz=1108521344) at
.../../../../libstdc++-v3/libsupc++/new_op.cc:48
#4 0x400ab44f in operator new[](unsigned) (sz=1108521344) at
.../../../../libstdc++-v3/libsupc++/new_opv.cc:36
/usr/src/build/146482-i386/BUILD/gcc-3.2-20020903/obj-i386-redhat-linux/i386-redhat-linux/libstdc++-v3/include/bits/fstream.tcc:52
at
/usr/src/build/146482-i386/BUILD/gcc-3.2-20020903/obj-i386-redhat-linux/i386-redhat-linux/libstdc++-v3/include/bits/fstream.tcc:91
#7 0x0805d741 in PingSamplesList::saveListElements(String&) ()
#8 0x0804b88c in metricClient::doClient() ()
#9 0x0804ac28 in main ()
#10 0x420158d4 in __libc_start_main () from /lib/i686/libc.so.6
Any help is really appreciated!!
-PS
I am getting segmentation fault while I am trying to open a file in a
small function.
int PingSamplesList::saveListElements (char *outputFileString)
{
PingSampleElement *tmpPtr;
int rc = 0;
ofstream outFile;
char lineBuffer[2048];
char *tmpLine = lineBuffer;
cout << "PingSamplesList::saveListElements to file: " <<
outputFileString;
outFile.open(outputFileString, ios:ut | ios::app);
tmpPtr = (PingSampleElement *)head;
while (tmpPtr != NULL)
{
sprintf(tmpLine, "%12.6f %9lf %9lf %9lf %0.4f
%9lf %9lf %9lf %0.4f \n",
tmpPtr->timeStamp,
tmpPtr->up_p_estimate,
tmpPtr->up_p_d_estimate,
tmpPtr->up_p_d_new_estimate,
tmpPtr->up_LossRate,
tmpPtr->dn_p_estimate,
tmpPtr->dn_p_d_estimate,
tmpPtr->dn_p_d_new_estimate,
tmpPtr->dn_LossRate
);
outFile << tmpLine ;
tmpPtr = (PingSampleElement *) tmpPtr->next;
}
outFile.close();
return rc;
}
When the function gets called the first time, it works perfectly. But
when it gets called the second time, it crashes with segmentation
faults. With some debugging statements I found that the program crashes
in the line
outFile.open(outputFileString, ios:ut | ios::app);
and outputFileString variable is NOT NULL.
Backtrace from GDB
#0 0x420741df in malloc_consolidate () from /lib/i686/libc.so.6
#1 0x42073ab7 in _int_malloc () from /lib/i686/libc.so.6
#2 0x42073155 in malloc () from /lib/i686/libc.so.6
#3 0x400ab2fe in operator new(unsigned) (sz=1108521344) at
.../../../../libstdc++-v3/libsupc++/new_op.cc:48
#4 0x400ab44f in operator new[](unsigned) (sz=1108521344) at
.../../../../libstdc++-v3/libsupc++/new_opv.cc:36
at#5 0x4009235d in std::basic_filebuf said:::_M_allocate_internal_buffer() (this=0x4212b180)
/usr/src/build/146482-i386/BUILD/gcc-3.2-20020903/obj-i386-redhat-linux/i386-redhat-linux/libstdc++-v3/include/bits/fstream.tcc:52
__s=0x4212b180 "\001", __mode=17)#6 0x40092185 in std::basic_filebuf said::pen(char const*, std::_Ios_Openmode) (this=0x4212b180,
at
/usr/src/build/146482-i386/BUILD/gcc-3.2-20020903/obj-i386-redhat-linux/i386-redhat-linux/libstdc++-v3/include/bits/fstream.tcc:91
#7 0x0805d741 in PingSamplesList::saveListElements(String&) ()
#8 0x0804b88c in metricClient::doClient() ()
#9 0x0804ac28 in main ()
#10 0x420158d4 in __libc_start_main () from /lib/i686/libc.so.6
Any help is really appreciated!!
-PS