【问题标题】:AccessViolationException when using std::basic_ifstream::get() in C++/CLI, why?在 C++/CLI 中使用 std::basic_ifstream::get() 时出现 AccessViolationException,为什么?
【发布时间】:2013-08-23 02:36:05
【问题描述】:

当我尝试运行时:

#include <tchar.h>
#include <fstream>

int _tmain(int argc, TCHAR *argv[])
{
    std::basic_ifstream<TCHAR> file("TestInput.txt");
    file.get();
}

我通过这个堆栈跟踪得到一个AccessViolationException

ntdll.dll!_RtlpWaitOnCriticalSection@8()  + 0xae bytes  
ntdll.dll!_RtlpEnterCriticalSectionContended@4()  + 0xa1 bytes  
ntdll.dll!_RtlEnterCriticalSection@4()  - 0x1f885 bytes 
msvcr120.dll!__lock_file()  + 0x2ce45 bytes 
[Managed to Native Transition]  
MyProject.exe!std::basic_filebuf<char,std::char_traits<char> >::_Lock() Line 355    C++
msvcp120d.dll!std::basic_istream<char,std::char_traits<char> >::_Sentry_base::_Sentry_base()  + 0x55 bytes  
msvcp120d.dll!std::basic_istream<char,std::char_traits<char> >::sentry::sentry()  + 0x32 bytes  
msvcp120d.dll!std::basic_istream<char,std::char_traits<char> >::get()  + 0x5c bytes 
[Managed to Native Transition]  
MyProject.exe!wmain(int argc = 0x2, wchar_t** argv = 0x054AA3F8) [line # removed]   C++
MyProject.exe!__tmainCRTStartup()    [line # removed]   C
[Managed to Native Transition]  
mscoreei.dll!__CorExeMain@0()  + 0x71 bytes 
mscoree.dll!_ShellShim__CorExeMain@0()  + 0x227 bytes   
mscoree.dll!__CorExeMain_Exported@0()  + 0x8 bytes  
ntdll.dll!___RtlUserThreadStart@8()  + 0x27 bytes   
ntdll.dll!__RtlUserThreadStart@8()  + 0x1b bytes    

为什么会发生这种情况,在尝试读取文件时如何避免这种情况?

【问题讨论】:

  • 为什么我有一种感觉不会发生在我身上:/
  • @chris:是的,我也有同样的感觉,不知道为什么……你在 VS 2013 上吗?
  • 是的,虽然我没有创建文件,所以我不妨试试。
  • 呃,不,我还是找不到异常。
  • @chris:没关系,由于某种原因,我也无法让它独立复制。 (我在另一个项目中作为 _tmain() 的第一行对其进行了测试,但我想肯定是先运行了其他东西......)我会缩小范围,谢谢你告诉我。

标签: c++ visual-c++ c++-cli access-violation visual-studio-2013


【解决方案1】:

这是因为我正在使用 _DEBUG 宏进行编译(从原生转换之前遗留下来的)。 删除它可以解决问题。

【讨论】:

  • _DEBUG 符号的作用是什么导致了这个异常?
【解决方案2】:

听起来你有宏在做意想不到的事情。当您需要弄清楚 Visual C++ 中发生了什么时,让它转储预处理器输出。详细信息记录在here

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多