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

小结

在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

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值