本文介绍了ofstream :: open创建文件,但是崩溃(locale :: getloc()中的指针不正确)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一些代码看起来像这样,编写和编译与Visual Studio 2010:

So I have some code that looks like this, written in and compiled with Visual Studio 2010:

if ( outputFile.is_open() )
{
    outputFile.close();
}
if ( !outputFile.is_open() ) // condition for sanity-checking
{
    outputFile.open("errorOut.txt", ios::out);
}

访问冲突时崩溃。附加调试器显示第一个条件为false( outputFile 未打开),第二个条件为true( outputFile 是封闭的,这是好的,因为我只是检查了它)。然后 open()被调用,最终 locale :: getloc()尝试解引用一个空指针,不知道为什么会发生这种情况(因为现在有三个类深入到标准库中)。

This crashes on an access violation. Attaching a debugger shows that the first condition is false (outputFile is not open), the second condition is true (outputFile is closed, which is good since I just checked it). Then open() gets called, and eventually locale::getloc() attempts to dereference a null pointer, but I have no idea why that would be happening (since that's now three classes deep into the Standard Library).

有趣的是,文件errorOut.txt即使打开调用崩溃,

Interestingly, the file "errorOut.txt" does get created, even though the open call crashes.

我已经花了几个小时看在调试器,但我真的不知道发生了什么。任何人都有任何想法,甚至试图确定代码的什么问题?这是完全可能的,一些代码在其他地方造成这种情况(继承代码),但有很多它,我甚至不知道在哪里看。一切都到这一点似乎很好。

I've spent a few hours watching this in a debugger, but I honestly have no idea what's going on. Anyone have any ideas for even trying to determine what's wrong with the code? It's entirely possible that some code elsewhere is contributing to this situation (inherited code), but there's a lot of it and I don't even know where to look. Everything up to that point seems fine.

推荐答案

OK,我不知道这是否是最好的方式来处理,但是由于这涉及到一些真正的奇怪的行为(在STL函数的中间崩溃,以及一些其他怪异的挂在 exit(1); 等),I

OK, I'm not really sure if this is the best way to handle this, but since this involved some truly strange behavior (crashing in the middle of an STL function, and some other oddities like hanging on exit(1); and the like), I'll leave an explanation here for the future.

在我们的例子中,错误似乎源于我们继承的一些真正可怕的代码中的一些内存损坏。清理代码一般消除了这个崩溃和程序显示的其他奇怪的行为。

In our case, the error seemed to derive from some memory corruption going on in some truly awful code that we inherited. Cleaning up the code in general eliminated this crash and other strange behaviors displayed by the program.

我不知道这将对任何人有用吗?也许它会更好地只是删除问题。我实际上有点好奇,如果我应该有,如果有人想留言。

I don't know if this will be useful to anyone; maybe it would have been better to simply delete the question. I'm actually somewhat curious if I should have, if anyone wants to leave a comment.

这篇关于ofstream :: open创建文件,但是崩溃(locale :: getloc()中的指针不正确)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 06:29