当前位置: 技术问答>linux和unix
linux下fclose出错
来源: 互联网 发布时间:2016-12-10
本文导语: linux 下,c程序,就是简单的写日志,打开文件,写日志,关闭文件,但是每次关闭文件fclose都报错,错误信息如下: *** glibc detected *** ./test: free(): invalid next size (normal): 0x0000000007be5620 *** ======= Backtrace: =======...
linux 下,c程序,就是简单的写日志,打开文件,写日志,关闭文件,但是每次关闭文件fclose都报错,错误信息如下:
*** glibc detected *** ./test: free(): invalid next size (normal): 0x0000000007be5620 ***
======= Backtrace: =========
/lib64/libc.so.6[0x3888a71634]
/lib64/libc.so.6(cfree+0x8c)[0x3888a74c5c]
/lib64/libc.so.6(fclose+0x14b)[0x3888a6089b]
./test[0x40347e]
./test[0x408770]
./test[0x405e89]
/lib64/libc.so.6(__libc_start_main+0xf4)[0x3888a1d8b4]
./test(__gxx_personality_v0+0xa9)[0x4017a9]
======= Memory map: ========
00400000-00411000 r-xp 00000000 fd:1e 6307883 /src/test
00610000-00611000 rw-p 00010000 fd:1e 6307883 /src/test
00611000-00612000 rw-p 00611000 00:00 0
07be4000-07c05000 rw-p 07be4000 00:00 0
3887a00000-3887a1a000 r-xp 00000000 fd:00 229676 /lib64/ld-2.5.so
3887c1a000-3887c1b000 r--p 0001a000 fd:00 229676 /lib64/ld-2.5.so
3887c1b000-3887c1c000 rw-p 0001b000 fd:00 229676 /lib64/ld-2.5.so
3888a00000-3888b4a000 r-xp 00000000 fd:00 229677 /lib64/libc-2.5.so
3888b4a000-3888d49000 ---p 0014a000 fd:00 229677 /lib64/libc-2.5.so
3888d49000-3888d4d000 r--p 00149000 fd:00 229677 /lib64/libc-2.5.so
3888d4d000-3888d4e000 rw-p 0014d000 fd:00 229677 /lib64/libc-2.5.so
3888d4e000-3888d53000 rw-p 3888d4e000 00:00 0
3888e00000-3888e82000 r-xp 00000000 fd:00 229681 /lib64/libm-2.5.so
3888e82000-3889081000 ---p 00082000 fd:00 229681 /lib64/libm-2.5.so
3889081000-3889082000 r--p 00081000 fd:00 229681 /lib64/libm-2.5.so
3889082000-3889083000 rw-p 00082000 fd:00 229681 /lib64/libm-2.5.so
3889600000-3889615000 r-xp 00000000 fd:00 229680 /lib64/libpthread-2.5.so
3889615000-3889814000 ---p 00015000 fd:00 229680 /lib64/libpthread-2.5.so
3889814000-3889815000 r--p 00014000 fd:00 229680 /lib64/libpthread-2.5.so
3889815000-3889816000 rw-p 00015000 fd:00 229680 /lib64/libpthread-2.5.so
3889816000-388981a000 rw-p 3889816000 00:00 0
388aa00000-388aa0d000 r-xp 00000000 fd:00 229683 /lib64/libgcc_s-4.1.2-20080102.so.1
388aa0d000-388ac0d000 ---p 0000d000 fd:00 229683 /lib64/libgcc_s-4.1.2-20080102.so.1
388ac0d000-388ac0e000 rw-p 0000d000 fd:00 229683 /lib64/libgcc_s-4.1.2-20080102.so.1
388ae00000-388aee6000 r-xp 00000000 fd:09 950539 /usr/lib64/libstdc++.so.6.0.8
388aee6000-388b0e5000 ---p 000e6000 fd:09 950539 /usr/lib64/libstdc++.so.6.0.8
388b0e5000-388b0eb000 r--p 000e5000 fd:09 950539 /usr/lib64/libstdc++.so.6.0.8
388b0eb000-388b0ee000 rw-p 000eb000 fd:09 950539 /usr/lib64/libstdc++.so.6.0.8
388b0ee000-388b100000 rw-p 388b0ee000 00:00 0
388ea00000-388ea15000 r-xp 00000000 fd:00 229701 /lib64/libnsl-2.5.so
388ea15000-388ec14000 ---p 00015000 fd:00 229701 /lib64/libnsl-2.5.so
388ec14000-388ec15000 r--p 00014000 fd:00 229701 /lib64/libnsl-2.5.so
388ec15000-388ec16000 rw-p 00015000 fd:00 229701 /lib64/libnsl-2.5.so
388ec16000-388ec18000 rw-p 388ec16000 00:00 0
2b145867a000-2b145867d000 rw-p 2b145867a000 00:00 0
2b14586a2000-2b14586a3000 rw-p 2b14586a2000 00:00 0
2b14589a5000-2b14589a9000 rw-p 2b14589a5000 00:00 0
2b145c000000-2b145c021000 rw-p 2b145c000000 00:00 0
2b145c021000-2b1460000000 ---p 2b145c021000 00:00 0
7fff5241a000-7fff52430000 rw-p 7fff5241a000 00:00 0 [stack]
ffffffffff600000-ffffffffffe00000 ---p 00000000 00:00 0 [vdso]
Aborted
程序如下:
void errLog(int level ,const char *fmt, ...)
{
FILE *fp;
va_list args;
char lsTmpBuf[FILE_NAME_LEN+1];
char lsStrBuf[STR_BUF_LEN+1];
initStr(lsTmpBuf);
initStr(lsStrBuf);
snprintf(lsTmpBuf, FILE_NAME_LEN, "%s/ERROR@%ld.log", ErrLogPath, getCurDate());
fp = fopen(lsTmpBuf, "a+");
if(fp == NULL)
{
printf("ERROR: open perror log file faild[%s][%s],check you path!n", strerror(errno), lsTmpBuf);
exit(1);
}
va_start(args, fmt);
vsprintf(lsStrBuf, fmt, args);
va_end(args);
if(level==1)
{
fprintf(fp, "%s[%s]%s", getSystemTime(0), "WARN",lsStrBuf);
}
else
{
fprintf(fp, "%s[%s]%s", getSystemTime(0), "ERROR",lsStrBuf);
}
if(fp!=NULL)
fclose(fp);
}
后来干脆把中间写的注释掉,直接打开,然后就关闭,还是fclose就错误
fp = fopen(lsTmpBuf, "a+");
if(fp == NULL)
{
printf("ERROR: open perror log file faild[%s][%s],check you path!n", strerror(errno), lsTmpBuf);
exit(1);
}
if(fp!=NULL)
fclose(fp);
这样也错,不知道为什么
*** glibc detected *** ./test: free(): invalid next size (normal): 0x0000000007be5620 ***
======= Backtrace: =========
/lib64/libc.so.6[0x3888a71634]
/lib64/libc.so.6(cfree+0x8c)[0x3888a74c5c]
/lib64/libc.so.6(fclose+0x14b)[0x3888a6089b]
./test[0x40347e]
./test[0x408770]
./test[0x405e89]
/lib64/libc.so.6(__libc_start_main+0xf4)[0x3888a1d8b4]
./test(__gxx_personality_v0+0xa9)[0x4017a9]
======= Memory map: ========
00400000-00411000 r-xp 00000000 fd:1e 6307883 /src/test
00610000-00611000 rw-p 00010000 fd:1e 6307883 /src/test
00611000-00612000 rw-p 00611000 00:00 0
07be4000-07c05000 rw-p 07be4000 00:00 0
3887a00000-3887a1a000 r-xp 00000000 fd:00 229676 /lib64/ld-2.5.so
3887c1a000-3887c1b000 r--p 0001a000 fd:00 229676 /lib64/ld-2.5.so
3887c1b000-3887c1c000 rw-p 0001b000 fd:00 229676 /lib64/ld-2.5.so
3888a00000-3888b4a000 r-xp 00000000 fd:00 229677 /lib64/libc-2.5.so
3888b4a000-3888d49000 ---p 0014a000 fd:00 229677 /lib64/libc-2.5.so
3888d49000-3888d4d000 r--p 00149000 fd:00 229677 /lib64/libc-2.5.so
3888d4d000-3888d4e000 rw-p 0014d000 fd:00 229677 /lib64/libc-2.5.so
3888d4e000-3888d53000 rw-p 3888d4e000 00:00 0
3888e00000-3888e82000 r-xp 00000000 fd:00 229681 /lib64/libm-2.5.so
3888e82000-3889081000 ---p 00082000 fd:00 229681 /lib64/libm-2.5.so
3889081000-3889082000 r--p 00081000 fd:00 229681 /lib64/libm-2.5.so
3889082000-3889083000 rw-p 00082000 fd:00 229681 /lib64/libm-2.5.so
3889600000-3889615000 r-xp 00000000 fd:00 229680 /lib64/libpthread-2.5.so
3889615000-3889814000 ---p 00015000 fd:00 229680 /lib64/libpthread-2.5.so
3889814000-3889815000 r--p 00014000 fd:00 229680 /lib64/libpthread-2.5.so
3889815000-3889816000 rw-p 00015000 fd:00 229680 /lib64/libpthread-2.5.so
3889816000-388981a000 rw-p 3889816000 00:00 0
388aa00000-388aa0d000 r-xp 00000000 fd:00 229683 /lib64/libgcc_s-4.1.2-20080102.so.1
388aa0d000-388ac0d000 ---p 0000d000 fd:00 229683 /lib64/libgcc_s-4.1.2-20080102.so.1
388ac0d000-388ac0e000 rw-p 0000d000 fd:00 229683 /lib64/libgcc_s-4.1.2-20080102.so.1
388ae00000-388aee6000 r-xp 00000000 fd:09 950539 /usr/lib64/libstdc++.so.6.0.8
388aee6000-388b0e5000 ---p 000e6000 fd:09 950539 /usr/lib64/libstdc++.so.6.0.8
388b0e5000-388b0eb000 r--p 000e5000 fd:09 950539 /usr/lib64/libstdc++.so.6.0.8
388b0eb000-388b0ee000 rw-p 000eb000 fd:09 950539 /usr/lib64/libstdc++.so.6.0.8
388b0ee000-388b100000 rw-p 388b0ee000 00:00 0
388ea00000-388ea15000 r-xp 00000000 fd:00 229701 /lib64/libnsl-2.5.so
388ea15000-388ec14000 ---p 00015000 fd:00 229701 /lib64/libnsl-2.5.so
388ec14000-388ec15000 r--p 00014000 fd:00 229701 /lib64/libnsl-2.5.so
388ec15000-388ec16000 rw-p 00015000 fd:00 229701 /lib64/libnsl-2.5.so
388ec16000-388ec18000 rw-p 388ec16000 00:00 0
2b145867a000-2b145867d000 rw-p 2b145867a000 00:00 0
2b14586a2000-2b14586a3000 rw-p 2b14586a2000 00:00 0
2b14589a5000-2b14589a9000 rw-p 2b14589a5000 00:00 0
2b145c000000-2b145c021000 rw-p 2b145c000000 00:00 0
2b145c021000-2b1460000000 ---p 2b145c021000 00:00 0
7fff5241a000-7fff52430000 rw-p 7fff5241a000 00:00 0 [stack]
ffffffffff600000-ffffffffffe00000 ---p 00000000 00:00 0 [vdso]
Aborted
程序如下:
void errLog(int level ,const char *fmt, ...)
{
FILE *fp;
va_list args;
char lsTmpBuf[FILE_NAME_LEN+1];
char lsStrBuf[STR_BUF_LEN+1];
initStr(lsTmpBuf);
initStr(lsStrBuf);
snprintf(lsTmpBuf, FILE_NAME_LEN, "%s/ERROR@%ld.log", ErrLogPath, getCurDate());
fp = fopen(lsTmpBuf, "a+");
if(fp == NULL)
{
printf("ERROR: open perror log file faild[%s][%s],check you path!n", strerror(errno), lsTmpBuf);
exit(1);
}
va_start(args, fmt);
vsprintf(lsStrBuf, fmt, args);
va_end(args);
if(level==1)
{
fprintf(fp, "%s[%s]%s", getSystemTime(0), "WARN",lsStrBuf);
}
else
{
fprintf(fp, "%s[%s]%s", getSystemTime(0), "ERROR",lsStrBuf);
}
if(fp!=NULL)
fclose(fp);
}
后来干脆把中间写的注释掉,直接打开,然后就关闭,还是fclose就错误
fp = fopen(lsTmpBuf, "a+");
if(fp == NULL)
{
printf("ERROR: open perror log file faild[%s][%s],check you path!n", strerror(errno), lsTmpBuf);
exit(1);
}
if(fp!=NULL)
fclose(fp);
这样也错,不知道为什么
|
我想,在定义fp时,就应该先进行初始化为NULL。
FILE *fp=NULL;
这样应该就行了。
FILE *fp=NULL;
这样应该就行了。
|
显示一下
lsTmpBuf 值是多少?
lsTmpBuf 值是多少?
|
是多线程的程序吗?