- 論壇徽章:
- 29
|
先說一下基本的環(huán)境
用wmware6.0 在windowXP的機器上建了一個red hat enterprise linux 4的虛擬機。然后通過ssh secure shell client客戶端連到虛擬機上寫程序,用的 g++ 編譯源代碼。
我用fopen 打開一個文件獲得文件指針fp,然后進行讀操作,在退出程序前用fclose關(guān)閉fp。這是程序報錯
SIGSEGV, Segmentation fault. 我用gdb調(diào)試發(fā)現(xiàn)程序死在 fclose函數(shù)中的free操作上。在調(diào)用fclose前檢查fp的值,發(fā)現(xiàn)值沒有變化。用 "p *fp"檢查 *fp 結(jié)構(gòu)的內(nèi)容。close就是執(zhí)行不了。
我又在調(diào)用fclose之前用ferror測試fp,返回值為0;用fread取文件內(nèi)容,也沒有報錯,能讀出數(shù)據(jù)。
但在fread成功后,馬上調(diào)用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);
這是測試語句對應(yīng)的輸出:
file error = 0
file pos = 20
buffer = <a>
<b>1<:length = 10
這是用gdb調(diào)試報的錯誤信息。
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 編輯 ] |
|