{"id":10311,"date":"2015-10-29T13:00:00","date_gmt":"2015-10-29T13:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/visualstudioalm\/2015\/10\/29\/improvement-to-debugging-c-access-violations-in-visual-studio-2015-update-1\/"},"modified":"2020-03-05T06:09:52","modified_gmt":"2020-03-05T14:09:52","slug":"improvement-to-debugging-c-access-violations-in-visual-studio-2015-update-1","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/devops\/improvement-to-debugging-c-access-violations-in-visual-studio-2015-update-1\/","title":{"rendered":"Improvement to Debugging C++ Access Violations in Visual Studio 2015 Update 1"},"content":{"rendered":"<p>In this blog post I will introduce a small but useful Visual Studio 2015 Update 1 improvement for debugging C\/C++ code.<\/p>\n<p>When writing C\/C++ code it is common to deal with complex lines that utilize multiple pointers, for example dereferencing multiple pointers on a single line. However, it can be difficult to decipher which part of that line of code is the problem when an access violation occurs. You may have previously broken up the line of code into multiple lines in order to debug this problem, but no longer. When this happens while using Visual Studio 2015 Update 1, you can easily see which pointer caused this exception. We now show a message directly in the Exception Dialog that informs you which variable was the nullptr.<\/p>\n<p>Currently this capability is only available while debugging C\/C++ code, but we are working toward applying the experience to .NET debugging in the future. If you\u2019d like to see this feature for managed code, please continue to add votes and comments to <a href=\"http:\/\/visualstudio.uservoice.com\/forums\/121579-visual-studio-2015\/suggestions\/2371587-better-nullpointerexception-error-message\">this UserVoice item<\/a> detailing the request.<\/p>\n<h2>Example<\/h2>\n<p>Let\u2019s take a look at the small C++ code sample below (<a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/6\/2019\/02\/NativeAccessViolationDemo.zip\">the full source is attached<\/a>). In this example we have a series of classes, each of which contains a pointer to another class, and one function named <span style=\"font-family: consolas;\">GetHelloWorld()<\/span>. In the main()method we dereference all of the pointers in order to print \u201cHello World\u201d.<\/p>\n<blockquote><p><span style=\"font-family: consolas;\"><span style=\"color: #0000ff;\">int<\/span> main()\n<\/span><span style=\"font-family: consolas;\">{\n<\/span><span style=\"font-family: consolas;\"><span style=\"color: #2b91af;\">\u00a0\u00a0 ClassA<\/span>* A = <span style=\"color: #2b91af;\">new ClassA<\/span>();\n<\/span><span style=\"font-family: consolas;\">\u00a0\u00a0 printf(A-&gt; B-&gt; C-&gt; D-&gt; GetHelloWorld());\n<\/span><span style=\"font-family: consolas;\"><span style=\"color: #0000ff;\">\u00a0\u00a0 return<\/span> 0;\n<\/span><span style=\"font-family: consolas;\">}<\/span><\/p><\/blockquote>\n<p>When I run this simple demo I get an exception for an access violation on the line <span style=\"font-family: consolas;\">printf(A-&gt; B-&gt; C-&gt; D-&gt; GetHelloWorld());<\/span> I know that something on this line must be causing the access violation. But what? With prior versions of Visual Studio, I would have to investigate further into the code to figure that out. Now with VS 2015 Update 1, all I have to do is look at the message in the Exception Dialog that pops up. The debugger tells me that a read access violation occurred and that A-&gt;B was nullptr.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/00\/00\/00\/45\/92\/metablogapi\/3225.NullReference_23D4C337.png\"><img decoding=\"async\" style=\"float: none; padding-top: 0px; padding-left: 0px; margin-left: auto; padding-right: 0px; margin-right: auto; border-width: 0px;\" title=\"NullReference\" src=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2015\/10\/1007.NullReference_thumb_0A6CBFFD.png\" alt=\"NullReference\" width=\"559\" height=\"318\" border=\"0\" \/><\/a><\/p>\n<p>Now I can break into my code knowing exactly what part of the chained line caused the access violation and can fix the issue.<\/p>\n<h2>Wrap up<\/h2>\n<p><a href=\"https:\/\/www.visualstudio.com\/news\/vs2015-update1-vs\">Install Visual Studio 2015 Update 1 RC<\/a> today, and try out this new feature. We are aware this functionality is highly desirable for .NET debugging as well. We\u2019re working on it, but can\u2019t promise when it will be available. Please vote for and comment on <a href=\"http:\/\/visualstudio.uservoice.com\/forums\/121579-visual-studio-2015\/suggestions\/2371587-better-nullpointerexception-error-message\">this UserVoice item<\/a> and provide your feedback. As always, we welcome feedback with open ears so please let me know what you think in the comments section below, through Visual Studio\u2019s <a href=\"http:\/\/connect.microsoft.com\/VisualStudio\/content\/content.aspx?ContentID=31113\">Send a Smile tool<\/a>, or via <a href=\"https:\/\/twitter.com\/vs_debugger\">Twitter<\/a>.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/6\/2019\/02\/NativeAccessViolationDemo.zip\">NativeAccessViolationDemo.zip<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this blog post I will introduce a small but useful Visual Studio 2015 Update 1 improvement for debugging C\/C++ code. When writing C\/C++ code it is common to deal with complex lines that utilize multiple pointers, for example dereferencing multiple pointers on a single line. However, it can be difficult to decipher which part [&hellip;]<\/p>\n","protected":false},"author":181,"featured_media":45953,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1,225],"tags":[],"class_list":["post-10311","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-devops","category-git"],"acf":[],"blog_post_summary":"<p>In this blog post I will introduce a small but useful Visual Studio 2015 Update 1 improvement for debugging C\/C++ code. When writing C\/C++ code it is common to deal with complex lines that utilize multiple pointers, for example dereferencing multiple pointers on a single line. However, it can be difficult to decipher which part [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/posts\/10311","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/users\/181"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/comments?post=10311"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/posts\/10311\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/media\/45953"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/media?parent=10311"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/categories?post=10311"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/tags?post=10311"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}