- 论坛徽章:
- 29
|
先说一下基本的环境
用wmware6.0 在windowXP的机器上建了一个red hat enterprise linux 4的虚拟机。然后通过ssh secure shell client客户端连到虚拟机上写程序,用的 g++ 编译源代码。
我用fopen 打开一个文件获得文件指针fp,然后进行读操作,在退出程序前用fclose关闭fp。这是程序报错
SIGSEGV, Segmentation fault. 我用gdb调试发现程序死在 fclose函数中的free操作上。在调用fclose前检查fp的值,发现值没有变化。用 "p *fp"检查 *fp 结构的内容。close就是执行不了。
我又在调用fclose之前用ferror测试fp,返回值为0;用fread取文件内容,也没有报错,能读出数据。
但在fread成功后,马上调用fclose还是出错。
这是我的测试语句:
int ret = 0;
char buffer[1024];
printf("file error = %d\n",ferror(pFile->pFile));
ret = ftell(pFile->pFile);
printf("file pos = %d\n",ret);
fseek(pFile->pFile,0,SEEK_SET);
memset(buffer,0,1024);
ret = fread(buffer, 1, 10, pFile->pFile);
buffer[ret] = 0;
printf("buffer = %s:length = %d\n",buffer, ret);
fclose(pFile->pFile);
这是测试语句对应的输出:
file error = 0
file pos = 20
buffer = <a>
<b>1<:length = 10
这是用gdb调试报的错误信息。
Program received signal SIGSEGV, Segmentation fault.
0x004dcb12 in _int_free () from /lib/tls/libc.so.6
(gdb) up
#1 0x004dd33a in free () from /lib/tls/libc.so.6
(gdb) up
#2 0x004cd746 in fclose@@GLIBC_2.1 () from /lib/tls/libc.so.6
(gdb) up
#3 0x08048ddf in FILE_close (pFile=0x804ba20) at ldr_file.c:95
95 fclose(pFile->pFile);
Current language: auto; currently c++
请问一下有谁碰到过这种情况?是什么原因造成的?
[ 本帖最后由 wxycyel 于 2008-7-24 10:16 编辑 ] |
|