当前位置: 技术问答>linux和unix
××调用fopen时出错?××
来源: 互联网 发布时间:2015-12-05
本文导语: 请问:调用fopen时程序出core是可能是什么原因?gdb信息如下,可以看到fopen中内存分配出错,但为什么fopen不返回错误呢?谢谢! gdb信息如下: #0 0x4207a3a9 in chunk_alloc () from /lib/i686/libc.so.6 #1 0x4207a058 in malloc ...
请问:调用fopen时程序出core是可能是什么原因?gdb信息如下,可以看到fopen中内存分配出错,但为什么fopen不返回错误呢?谢谢!
gdb信息如下:
#0 0x4207a3a9 in chunk_alloc () from /lib/i686/libc.so.6
#1 0x4207a058 in malloc () from /lib/i686/libc.so.6
#2 0x4206bb71 in fopen@@GLIBC_2.1 () from /lib/i686/libc.so.6
#3 0x0804dbbb in testfile () at adindex.cpp:1647
#4 0x0804e4c9 in save_index (path=0x80afe54 "./data/adindex/incadindex/0/") at adindex.cpp:703
#5 0x0804fa42 in build_inc_index () at adindex_inc.cpp:500
#6 0x0804b330 in build_normal_index () at adindex.cpp:181
#7 0x0804b0ba in main (argc=1, argv=0xbffff9f4) at adindex.cpp:79
#8 0x42017499 in __libc_start_main () from /lib/i686/libc.so.6
gdb信息如下:
#0 0x4207a3a9 in chunk_alloc () from /lib/i686/libc.so.6
#1 0x4207a058 in malloc () from /lib/i686/libc.so.6
#2 0x4206bb71 in fopen@@GLIBC_2.1 () from /lib/i686/libc.so.6
#3 0x0804dbbb in testfile () at adindex.cpp:1647
#4 0x0804e4c9 in save_index (path=0x80afe54 "./data/adindex/incadindex/0/") at adindex.cpp:703
#5 0x0804fa42 in build_inc_index () at adindex_inc.cpp:500
#6 0x0804b330 in build_normal_index () at adindex.cpp:181
#7 0x0804b0ba in main (argc=1, argv=0xbffff9f4) at adindex.cpp:79
#8 0x42017499 in __libc_start_main () from /lib/i686/libc.so.6
|
像这种内存出现down的,一般都是因为定义的变量没有初始化,长度不符,资源没有释放等,如打开文件的指针没有close,请仔细查看
|
其实我更想看你贴出的源代码
|
你fopen的时候有其他进程存在吗?
man page说存在以下原因:
ERRORS
The fopen() function will fail if:
[EACCES]
Search permission is denied on a component of the path prefix, or the file exists and the permissions specified by mode are denied, or the file does not exist and write permission is denied for the parent directory of the file to be created.
[EINTR]
A signal was caught during fopen().
[EISDIR]
The named file is a directory and mode requires write access.
[ELOOP]
Too many symbolic links were encountered in resolving path.
[EMFILE]
{OPEN_MAX} file descriptors are currently open in the calling process.
[ENAMETOOLONG]
The length of the filename exceeds {PATH_MAX} or a pathname component is longer than {NAME_MAX}.
[ENFILE]
The maximum allowable number of files is currently open in the system.
[ENOENT]
A component of filename does not name an existing file or filename is an empty string.
[ENOSPC]
The directory or file system that would contain the new file cannot be expanded, the file does not exist, and it was to be created.
[ENOTDIR]
A component of the path prefix is not a directory.
[ENXIO]
The named file is a character special or block special file, and the device associated with this special file does not exist.
[EOVERFLOW]
The named file is a regular file and the size of the file cannot be represented correctly in an object of type off_t.
[EROFS]
The named file resides on a read-only file system and mode requires write access.
The fopen() function may fail if:
[EINVAL]
The value of the mode argument is not valid.
[EMFILE]
{FOPEN_MAX} streams are currently open in the calling process.
[EMFILE]
{STREAM_MAX} streams are currently open in the calling process.
[ENAMETOOLONG]
Pathname resolution of a symbolic link produced an intermediate result whose length exceeds {PATH_MAX}.
[ENOMEM]
Insufficient storage space is available.
[ETXTBSY]
The file is a pure procedure (shared text) file that is being executed and mode requires write access.
但是你的症状像是在返回之前就core dump了,你是在什么地方打开文件呢?终端?还是其他的
man page说存在以下原因:
ERRORS
The fopen() function will fail if:
[EACCES]
Search permission is denied on a component of the path prefix, or the file exists and the permissions specified by mode are denied, or the file does not exist and write permission is denied for the parent directory of the file to be created.
[EINTR]
A signal was caught during fopen().
[EISDIR]
The named file is a directory and mode requires write access.
[ELOOP]
Too many symbolic links were encountered in resolving path.
[EMFILE]
{OPEN_MAX} file descriptors are currently open in the calling process.
[ENAMETOOLONG]
The length of the filename exceeds {PATH_MAX} or a pathname component is longer than {NAME_MAX}.
[ENFILE]
The maximum allowable number of files is currently open in the system.
[ENOENT]
A component of filename does not name an existing file or filename is an empty string.
[ENOSPC]
The directory or file system that would contain the new file cannot be expanded, the file does not exist, and it was to be created.
[ENOTDIR]
A component of the path prefix is not a directory.
[ENXIO]
The named file is a character special or block special file, and the device associated with this special file does not exist.
[EOVERFLOW]
The named file is a regular file and the size of the file cannot be represented correctly in an object of type off_t.
[EROFS]
The named file resides on a read-only file system and mode requires write access.
The fopen() function may fail if:
[EINVAL]
The value of the mode argument is not valid.
[EMFILE]
{FOPEN_MAX} streams are currently open in the calling process.
[EMFILE]
{STREAM_MAX} streams are currently open in the calling process.
[ENAMETOOLONG]
Pathname resolution of a symbolic link produced an intermediate result whose length exceeds {PATH_MAX}.
[ENOMEM]
Insufficient storage space is available.
[ETXTBSY]
The file is a pure procedure (shared text) file that is being executed and mode requires write access.
但是你的症状像是在返回之前就core dump了,你是在什么地方打开文件呢?终端?还是其他的
|
你把fopen那句贴出来看看,我怀疑是你的那个(const char *)变量有问题