0
点赞
收藏
分享

微信扫一扫

Visual Studio 2019程序运行出现‘Access violation reading location‘的问题


文章目录

  • ​​小结​​
  • ​​问题及解决​​
  • ​​方法一​​
  • ​​方法二​​
  • ​​Unexpected end of file error​​
  • ​​vs2019 debug 出现: printf is ambiguous​​
  • ​​参考​​

小结

在Visual Studio 2019的C++程序调试运行中出现’Access violation reading location’的问题,​​Unhandled exception at 0xxxxxxxxx (xxxx.dll) in xxxx.exe: 0xxxxxxxxx: Access violation reading location 0xxxxxxxxxx​​,进行了解决。

问题及解决

在Visual Studio 2019的C++工程,编译是没有问题的,但是在程序调试运行时经常会出现’Access violation reading location’的问题,经过调试跟踪后,发现是由于变量被多个代码段访问到,可能出现了空指针的情况。这种错误也不是每次都发生,所以比较难找到原因和追踪到。具体原因可能是所访问的变量已经被其它部分代码改动了,具体建议如下:
​​​Then verify that the values are not being unintentionally changed somewhere in the program by creating a Data Breakpoint for the pointer in question to make sure it isn’t being modified elsewhere in the program.​

使用以下两个办法可以大大减少此类问题的发生。

方法一

在有可能发生变量使用冲突的地方加上​​try {...} catch (std::exception ex) {}​​来捕捉这个异常,异常被捕捉到后程序是可以接着运行的,不会出现退出的情况:

try {
...
}
catch (std::exception ex) {
std::cout << "Exception happened! " << ex.what() .
}

方法二

减少多段代码访问同一个变量的情况,我将没有必要的修改冲突变量的地方进行了注释删除。经过多次调试测试,没有再发生​​Access violation reading location​​的问题。

Unexpected end of file error

​Unexpected end of file error ​​​这个问题的出现可以通过配置工程文件属性进行解决: 工程性性 -> C/C++ -> Precompiled Headers ->Create/Use Precompiled Header选择​​Not Using Precompiled Headers​​.

vs2019 debug 出现: printf is ambiguous

​printf is ambiguous​​​这个问题的解决比较奇怪:在代码中添加​​using namespace std;​​​,保存,问题消失,如果不行,删除​​using namespace std;​​,保存,添加,保存,也就是再来一遍同样的操作。

using namespace std;

参考

​​Code Project: Access violation reading location​​​  
​​​Stackoverflow: Catching access violation exceptions?​​​​Microsoft Doc: Structured Exception Handling (C/C++) ​​​​Microsoft Doc: How Can I Debug an Access Violation?​​​​Stackoverflow: Unexpected end of file error​​​​vs2019 debug 出现: printf is ambiguous​​


举报

相关推荐

0 条评论