gpt4 book ai didi

c++ - 在 C++/CLI 中使用 std::basic_ifstream::get() 时出现 AccessViolationException,为什么?

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:56:14 26 4
gpt4 key购买 nike

当我尝试运行它时:

#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

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

最佳答案

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

关于c++ - 在 C++/CLI 中使用 std::basic_ifstream::get() 时出现 AccessViolationException,为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18393764/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com