费罗尔导致访问冲突
ferror leads to access violation
我在调用ferror(f)
时收到访问冲突。问题是我检查f
不是空指针。我还能够在收到访问冲突之前读出文件。它发生在 http://www.bzip.org 的 bzip 库中(仅经过修改,以便可以构建,例如禁用错误并删除多个主要功能(。
这是我的主要
int main() {
int e = 0;
int *error = &e;
FILE *f = fopen("./test", "r"); //open file
if (f == NULL) { //Changed, as beforehand both checks happend at the same time
//However the programm passes both checks
std::cout << "f* is NULLn";
exit(1);
}
if (ferror(f)) {
std::cerr << "Can't open the file " << ferror(f) << 'n';
exit(1);
}
char *c = char[20];
fread(c, 1, 20, f); // Here we can read succesfully out of the file
std::cout << c;
BZFILE* bzfile = BZ2_bzReadOpen(error, f, 1, 0, NULL, 0); //The failing function call
}
这是库外失败的函数:
BZFILE* BZ_API(BZ2_bzReadOpen)
( int* bzerror,
FILE* f,
int verbosity,
int small,
void* unused,
int nUnused )
{
bzFile* bzf = NULL;
int ret;
BZ_SETERR(BZ_OK);
if (f == NULL || //A check in the function itself, which also passes
(small != 0 && small != 1) ||
(verbosity < 0 || verbosity > 4) ||
(unused == NULL && nUnused != 0) ||
(unused != NULL && (nUnused < 0 || nUnused > BZ_MAX_UNUSED)))
{ BZ_SETERR(BZ_PARAM_ERROR); return NULL; };
if (ferror(f)) // Here is the access violation
{ BZ_SETERR(BZ_IO_ERROR); return NULL; };
}
具体错误是
Exception thrown at 0x0003E5C4 in wikiParser.exe: 0xC0000005: Access violation executing location 0x0003E5C4.
我尝试将程序移动到另一台PC,但是仍然发生相同的错误。指针不为 NULL;有两个检查,我可以自己打电话给ferror
,而不会违反访问。
if(f == NULL || ferror(f)) // Check that f isn't NULL nor has an Error
std::cerr << "Can't open the file " << ferror(f) << 'n';
换句话说,如果f== NULL
那么cerr << ferror(f)
.
难怪它会爆炸。
相关文章:
- 写入位置0x0000000C时发生访问冲突
- 引发异常:读取访问冲突**dynamicArray**为0x1118235.发生
- 链表中写入访问冲突的未知原因
- C++中的openCV Mat访问冲突
- C++尝试深度复制唯一指针时出现内存访问冲突
- C++ 中动态二维数组的访问冲突
- 从嵌套循环中的 std::list 中删除将返回访问冲突
- 写入访问冲突异常
- 在类 12.exe 中0x7B37FF80 (ucrtbased.dll) 引发异常: 0xC0000005:访问冲突读
- 0xC0000005:访问冲突写入位置0xCDCDCDCD动态分配错误
- 读取访问冲突.这0xCDCDCDCD
- 0xC0000005:访问冲突读取位置 0x00000000. 重载 == 运算符的问题
- 插入数组时违反写访问冲突
- 使用 ReadProcessMemory 获取字符串值的访问冲突
- 尝试通过共享指针使用变量时读取访问冲突
- 堆栈上的 C++ 访问冲突写入异常
- 引发异常:写访问冲突. temp 为 nullptr
- 将静态字符数组中的字符分配给动态分配的字符数组 - 访问冲突
- 在C++中删除双向链表的头节点后出现访问冲突异常
- 费罗尔导致访问冲突