Tips and Tricks C++ Debugging in Visual Studio

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi welcome to visual studio tool box I'm your host Robert Greene and joining me today is Andrew hall hey Andrew Robert welcome back thank you Andrew is here today to talk about C++ debugging I'm gonna do some C++ today yeah so we're gonna do some some tips and tricks awesome any of this stuff new he's have been around in the product for a while most of the stuff I'm gonna show has been in the product for a while there's a few things that were new to Visual Studio 2015 update one okay so nothing incredibly bleeding-edge all right cool and then maybe you'll give us a taste of some things that might be coming in the next version yeah talk about a couple things cool let's see it absolutely so here it's go down to my machine I have a console syllabus console application I've written and when I kick it off to go ahead and run I'm expecting it here to tell me hello Andrew so I have a user name that I thought I was passing in so obviously my first bug is that for some reason I'm not actually something's not going to corrupt the heap I love this game this is yeah this is kind of kind of a the kitchen sink of apps so we have a plague a guessing game play guess what we can calculate some prime numbers we can corrupt our heap and we can quit of course cool all right what's an app without the ability to quit exactly so first I'm gonna do is I'm just gonna stop debugging I'm gonna come back here to my project properties and when I come down here scroll down so we right click on project properties I'm gonna go to the debugging tab and I can see that in project properties I have the ability to pass in command-line arguments I can even specify environment variable values so that can be just a useful thing wall debugging I can see here that I am passing in slash user Andrew and that I have max gas of 30 so I'm passing in my command line arguments as expected so my sort of the bug realm scene slash user isn't that I forgot to pass something in so so it's actually a bug in my particular code so let's come in here to where my game application starts and so let's go to our main function let's just put a break point in here let's go ahead and start debugging again and so as I come in diagnostic tool in Dover here we're going to talk about a little later so I'm just gonna unpin it it'll fly over here to the side and as I step into this great user I can see that I'm short of generating via WC out I'm printing this whole line of stuff so the first tip is as I step over this line in the autos window the debugger is gonna show me the return values of all the functions that were called on that particular line of code so in a case like this where I have a bug I'm doing multiple function calls on a line instead of debugging down into each one as a really quick way of just sort of seeing everything that happened on that line I step over that line of code all the function calls right here you can see get punctuation return to exclamation point get username return slash user get spaced or get whitespace return space and get salutation returned hello so I can see that my issue here is in the get username function so I'm going to use another piece of functionality in the debugger that can be really useful and it's called set next statement so I can either access that by right-clicking and say set next statement or I can simply click and drag this little yellow arrow and that decides what instruction is about to execute next so I move the execution back up to line 118 which means I'm going to execute line 118 again right and this is a really useful piece of functionality for doing things like this like you missed it and you want to go back so I don't have to restart the application again yeah you can also use it to force the debugger or the program to execute down different conditional blocks so you want an e bug into what happens you have an if statement it's actually going to be false because of the state of the application but you're still curious you just grab the little yellow pointer move it inside the F block okay and your program is now executing inside the if block even though that would have evaluated to fullness so now I want to get down into get username I could click on it I could go to definition I could set a breakpoint or I can right-click on the line of code I can say step in to specific and this is going to show me all the functions that are going to be called in this particular line of code and when I choose get username I can see that I'm gonna step down into that get username function and particular case I can see that I'm simply returning the username is that step in a specific a C++ unique feature in all the languages step in a specific is unique to C++ and C sharp and Visual Basic okay so for the sort of the big three languages we have step in a specific it doesn't work for things like JavaScript Python some of the other languages that we support nationals 2d okay but you do give it get it for sort of those three core languages in Visual Studio so I can see here that get user name it's already too late it's just returning a property so I need to actually appears that my bug here is while I'm actually parsing this so when I get back up to my main function and the way I'm going to do that is this is a fun piece of functionality is you can right-click and if I say run to cursor it's gonna actually run the debugger back up to where that to that particular frame on the call stacks I'm gonna right-click I'm gonna say run the cursor on here control left an will do it and then boom I'm back up here so I'm gonna get ahead and use my set next statement functionality to go back up to here to where I'm creating these user options which is where I'm actually doing the parsing and just because I can I'm gonna go f12 and I showed you run to cursor in that particular place up look sorry what I would either options what I need to do that I'm to the wrong place let me go ahead and unpin some of these things for now just to make our editor a little bigger and so down here where I'm parsing this particular user options I'm gonna go down here to where I'm looking if it's the user argh I'm gonna right-click in my editor and I'm gonna say run the cursor here as well so I used in the call stack when - a second ago it's gonna run to this line of code now under the debugger so it's basically a one time breakpoint you don't have to do it through right click ctrl f10 will actually run to that location as well so you just click and hit ctrl f10 so we can keyboard shortcuts for anything that we have in the context menu as well so I can see here that as I go down I'm gonna go ahead and step through this code to try to see what's going on so I can see that it goes inside the if block correctly I can see that it's assigning username and it's parsing this out of the args but I can see that what I really intended to do here was I should have done the X plus plus above ax index accessing that particular array index so if you look down at all my other parameters it's X plus plus 2 increment that then I retrieve the number for my array so in this particular case looks like I just screwed that up I put the X plus after so I'm actually retrieving the argument itself as opposed to the value of the argument so I'm going to use edit and continue which is something we've had in the debugger for a while but new to Visual Studio 2015 you can see I'm debugging the 64-bit version this application and we've never had edit and continue for 64-bit C++ debugging right the other thing about that's new to visual C 2015 is when I'm doing edit and continue I'm actually using the default debug engine and so that means I get functionality that we've added since Visual Studio 2010 to native debugging so in Visual Studio 2013 editing continue as an option or tools options but it actually fell back to the Visual Studio 2010 c++ d bug engine which meant I didn't get things like matte vis which I'll talk about in a few minutes or the async call stack work or some of the other cool stuff that we've added since Visual Studio 2010 because I was a long time ago yeah ok so I'm gonna grab the instruction pointer I'm gonna drag it back up you can see the fiddy bugger is going to recompile that and then when I hit f10 I'm going to step over that it's going to execute my compiled code and I can see that username is now correctly set to Andrew cool so without editing continue or without it and continue no need to stop debugging right just a little make changes on the fly works for x86 works for x64 and that's by default out of the box and Visual Studio 2015 so it's one of the things we're we're really proud of worked really hard on that so let me go ahead and hit f5 here and my console application should pop up and now I can see that it says hello Andrew here the hell will user hello user from the times where I did the set next statement so we were reax acute at that code which is why I printed it three times right so next thing I want to do let's play a little guessing game right yeah let's do that alright so hit one and say play guess what and the first thing I can see is I'm getting this invalid story argument here so I have some issue popping up it doesn't look like it fatal to the application Soyuz no because you shouldn't be seen that I don't know we're not expecting you to see that so we have a little little bug that would have been my guess by the way yeah well it's not not a real number so what I'm gonna do is if I go look in the output window what is a story by the way first people that aren't it's a function that converts text to an integer okay so you'll see that in a second so I'm gonna go back here to the debug pane so I was on the build pane by default because I had done an edit and continue so it showed me the build output but I can see that I'm having an exception thrown so I'm gonna go to the exception settings window which the capability is not new and visuals 2015 but the window itself is new it's really fast now and that's available under debug windows and we have exception settings and you can see that it comes up really fast now so before it was a modal dialog that was pretty slow yeah we got a lot of frustration about that and we have a really good modern search now as opposed to before again it was a very dated search I think the dialog dated back to like visual studio.net or something from like 2005 so let's just go say STD exception so that's the C++ exceptions let's go ahead and clear that so I'm able to do one I'm able to just turn on all the exceptions for category as well so I can search for a specific one I'm gonna turn them on for a category and I can go and so now what's gonna happen is that he bugger is go ahead and exit out of this and let's go ahead and try playing guessing again should now break whenever that exceptions thrown it obviously was being handled correctly we see that we had invalid story argument but the program didn't crash it just let us keep keep playing I'm gonna go ahead and hit one and I can see the debugger broke and it's telling me that there's an invalid argument exception and so when I go ahead and click break I'm gonna walk back up my call stack because I had something she called show external code this allows me to collapse or hide code but anyways I'm going to go ahead and walk back up here one one line to my code and I can see that what's happening is I'm trying to parse this nums actually nothing and so if I look back up somehow I added some really complicated logic using get line and I'm doing some custom parsing and all I really needed to do was to do a see in to my integer so in this particular case I could just say let's go ahead and comment this out use edit and continue again say STD see in nums that should have done it so let's go ahead and hit f5 it should apply that that change boom pop back up let's exit out of this and when I go play guess what perfect we fixed that exception okay so the issue was it was actually the way I was doing the get line it was getting the the newline character when I hit enter to enter my previous selection and then it was trying to convert the newline character into an integer so when you were seeing the story the function that converts text to an integer in C++ is sto I and it wasn't happy trying to convert a newline to an integer because it's not actually okay so you were able to tell it to blow up so that you could see what line of code was actually blowing up on exact handling the exception is great for a user but it doesn't help you figure out what was causing the problem exactly so instead of stepping through my code and like waiting till I see the exception occur I just go into exception settings I changed that setting the debugger actually stops where the exceptions being thrown and then I can figure out how to fix it right there right there so it helps me find that really quickly cool all right so let's enter and our guess and say number two wanted 3001 in 30 25 - hi hi 2015 should have started with 15 10 5 hey so it's actually a mathematical technique for doing that but I decided just to decrement by 5's fair enough which is probably inefficient but that's okay that's alright we didn't we didn't do it in a binary search fashion but great so with that next thing I want to do is I'm going to go ahead and do calculate primes ok so we're gonna kind of shift modes a little bit we've got the application up and running ultimately we're going to convert this into a client-server application it's gonna I'm gonna have a server that's gonna actually run and then when I kick off the client like the the guessing it'll happen on the server and that way I can write have run that and as your someday and I can go put this on all the devices in the world and I can make my millions with my guess what game awesome but it's part of this we're gonna securities based on calculate primes so we're gonna calculate primes for for some good reason and when I do this is I kind of go down this list I can see that it spit out all the primes between one and a hundred including some that aren't from including some that aren't Prime's exactly which one do you see that isn't pro I see the nine then I see a 25 I know those two aren't I'm sure there's others yeah there's a 49 down here that isn't isn't prime either so you're absolutely right like we have we have some particular bug here where we're printing some numbers that aren't prime so nine we conclude it was one that wasn't prime so let's go ahead and dive in and see if we can figure out what's going on in this particular piece of code so I'm going to go over here to my primes and this you make that the guest game guess which of these are not actually primes I like that idea so what I'm gonna do is I'm actually calculating these Prime's in a multi-threaded fashion so instead of calculating them linearly cuz calculating primes can be slow I'm gonna take advantage of all the cores on my particular machine so first I'm gonna do is I'm gonna set a conditional breakpoint I'm and this is prime function and I only want to stop when my particular number is nine was one right that we were calculating so I want to say candidate equals equals nine so conditional breakpoints are a great way to have the debugger kind of like we did the exception settings min ago only when it's a stop for the exception thrown conditional breakpoints gonna say I'm not going to actually ask the debugger to stop until it's the pradhan umber that I know is the problem in this particular case the other types of conditions we have while we're here we have a hit count so I can do that based on hit count and a filter filter lets me choose what I'm actually doing multi-threaded things I can choose information like the thread ID or the process idea that I'm debugging in the case of a conditional expression a conditional expression is conceptually anything I can type inside an if statement that would evaluate to a boolean so let's go ahead and accept this and the way that I got there just worth pointing out as I hovered over the breakpoint and I clicked this little thing here I can also get in here via right-clicking and click conditions the toolbar that I'm showing up here was new to Visual Studio 2015 but the capability to right-click and go set a conditional breakpoint that's been in the product for quite a few releases yep alright so we have that set let's come in here let's go ahead and choose to calculate Prime's again which is number two and what I'm going to expect is I'm going to expect that the debugger is gonna stop and candidate is gonna be nine 180,000 milliseconds well we'll talk about that a little bit let me step real quick is it really that much I'll talk I'll talk about that a little bit it's a great question and I'll answer your query how about it it's a 280,000 milliseconds there was at point two eight seconds actually didn't didn't a number right so so since you asked about it this what that what the number here is is it's called a perf tip yeah we'll talk about a little bit perfect shows me the elapsed time since the last time I entered my break state right so the whole time that we were talking while the application was running that number was being counted so it probably was pretty slow because we were sitting there talking right so I can actually go back to the okay I have a hit that I have a history of it here and so yep 280,000 and 48 milliseconds so that would be 28 seconds 28 seconds right right okay okay alright so that was counting time that the app was running well we were just yeah so so what the perf tape does and again we'll talk about this in a little bit but it counts so you can see that I ran from this breakpoint and I hit f10 to step yeah so now it tells me less than or equal to one millisecond elapsed so simply tells you how long the application was running between states right so that's why I actually come here we can come back in here real quick so it's kind of a good tip for people because you know when we talk about the the perf tips when we demo the we say you know it doesn't include the time we've spent talking about this right because you you step through your code and then you stare at it and the the perfect it doesn't include that time that you were staring at it during debugging counts the amount time your applications running right but if you but if you do it start the app and then stare at it and then break then it did include the time we spend staring it right next how long your apps run how so why I was apps up here running right and the debugger doesn't know if it's running because sitting there waiting for user input or if you're actually like intending that to be fast and it's not like all what we even know is hey your apps running so we're gonna tell you how long the next time you hit a breakpoint or you've stopped on your next step that it took from the last time you stopped to this time right so now when I come in here and say to calculate Prime's right I'm gonna hit that again and it's gonna be 60 you're right 200 was was 280 seconds you're absolutely okay for so we were talking for quite a while while the city in that state that's right because we played the guessing game in there too all right Kyle code it's impressive that's actually a good tip that if you're going to be using this make sure that you're not accidentally counting time where the user is looking at the app and where the pause is supposed to be part of the app right exactly yeah so we were apparently talking for a minute there while it was sitting there running and we were chatting away again so candidate here is 9 so I'm gonna take advantage of another feature that I really like when doing iterative stuff like this and that's the ability to click this little pin icon over here and it's gonna stick it I can drag it anywhere I want in the editor so now I know what the value of candidate is and so as I step right I don't have to hover over it every time again sort of the same deal we measured how long the application run is ran and it took less than one millisecond to step from from there to there I mentioned that was calculating this in a multi-threaded way one of the questions we get asked a lot is hey I'm doing multi-threaded debugging like what's the best way to debug that and understand what's going on out in the application so my favorite windows in a case like this where you're doing something in parallel and you want to understand what's going on is to use the parallel stacks window I'm gonna go here and it's a from the debug windows one of my favorite tips in Visual Studio are not directly related to debugging but you tend to open a lot of tool windows wall debugging is the quick launch or control queue so instead of accessing things from the from the debug you know windows menu I can actually just go up here to here I can hit ctrl Q or click up in the top right and say quick launch and I can start typing parallel and I can open my parallel sax window and when the parallel Stax window is going to open it's going to show me all of the threads at the moment that are running my code so in this case I can see that there are 13 threads total in the application that are running an external code and so this is something I briefly mentioned earlier but we have something called just my codes if I right-click and I say show external code we're gonna what we're trying tempting to do is collapse all the stuff that we know is system or framework code and not yours is not part of your application and so when I say show external code you can see that this is going to get really ugly really quick and most of these threads aren't doing anything I particularly care about right so there's one thread and and something here and 11 threads doing this and right this just all addresses I don't even know what that means at the moment I haven't loaded symbols for them which is why it's not even giving me meaningful function names I'm gonna go ahead and right-click I'm gonna turn off show external code and I'm back to I'll call it the view of the world yeah where I understand what's going on but that's a tip doesn't adjust your code this is just my code right exactly stuff that's compiled into my application which in this case is only writing the is prime 5 threads are in the right Prime and then I have one thread that's just taken a slightly different path but is also in the calc Prime's function ok so what if I want to know so it looks like I have actually seven threads right now that are actually calculating a prime or in the process they're a bit so if I want to know what all of those threads are doing we have a tool window for that so you know I'm visual studio I like to say we have a window for that so control PR ctrl Q again ctrl P is not what I wanted I don't need to print anything and I'm gonna go ahead and type parallel watch so we have a parallel watch window and what the parallel watch window is gonna do is I'm gonna go ahead and stick it down here under here it's a nice way to look at it and when I enter something it's gonna show me the value across all the threads that are currently in the current location so in this particular case I've already typed candidate here I could go ahead and if I cleared it out you could see ni right so it goes away I click to add watch I can type candidate again and it's gonna show me the value of the candidate variable across all the threads that are currently executing so the little yellow arrow shows me the thread that I'm currently on that's nine that's what we would expect because we static conditional breakpoint only stopped ends 9 but I have these other threads that are calculating at 40 3 is prime 3 is prime 23 9 or 19 and 37 so parallel watch really great way to see variables the value variable across all of your your various threads so mystery in case I want to debug what's going on with 9 and I don't necessarily want to want to deal with the rest of my threads so what I'm gonna do is I'm gonna come in to this particular thing and I'm going to go ahead and select all of these threads so I should be like right click up here say select all and then I come back select all alright well that's all right we're gonna somebody who had an in do do this I'm gonna go ahead and flag all of not that thread I'm gonna go ahead and flag depth reading I'm gonna right click on the thread and I'm gonna say freeze this particular threads what the freezing is gonna do is going to prevent that thread from actually executing ok and by flagging the other threads I'm adding them to a group and you'll see why that's interesting here in a minute so I'm gonna go ahead and say freeze boom and I'm gonna go ahead and come in here and I'm gonna freeze this particular thread thread sorry now I'm not gonna freeze it I'm gonna flag it sorry faso thaws the opposite of freeze makes sense i'm gonna go ahead and just flag that particular thread so it already now is I want to come down here and I want to go to this particular line here I'm going to right click and I'm gonna say we did run to cursor earlier I can run all with my flag red sticker sir and so it running on the flag threads the cursor is just gonna get them into us into a known location but I can just let them sit so this right prime up here there's actually a writer lock so it prevents because some concurrency issues you can get weird formatting in a console window when you're printing them I actually take a lock and I don't want to stop any threads that are currently holding a lock because my other thread is gonna eventually deadlock on that I'm going to go ahead and write or click them say run flagged threads to cursor so I'm going to get all of those threads back out and it looks like some of those were already done so they're never gonna get to that particular location in code which wasn't good so we'll go ahead and restart that so that's the danger of run flagged threads to cursor and concurrency demos in general you don't actually know what you're gonna do but let me go ahead and write go ahead and flag all of these threads again boom boom boom boom boom boom boom and we're gonna go ahead and freeze number 9 I go ahead and come in here I'm gonna go ahead and flag you and let's go ahead and just run everybody to this particular line of code right here run a flag threads to cursor hopefully enough people so basically what's happening is is some of those threads are never like they're actually like finishing executing and they're never getting spun up which is why I'm running into this issue where it's just City it's waiting for that like all of those threads to actually get to this particular line of code that's okay we can we can live with that let's go ahead and just and just restart my one more time currency demos never never quite quite work out like you want and so just because things are always always shifting around in the particular program so all I really need to do is I need to get all of whatever threads are currently holding my lock I need to actually get them out so if I want to know which threads are currently actually inside this lock location I can go up here and I can right click the earth not right-click but click the show threads and source on the toolbar and what this is going to do is this is going to show me the all the threads in which particular line of code that they're actually on so what I want to do is I want to get all of these particular threads out of here I'm going to go ahead and come down to my thread again that's right here so if I unset this break point you can see this one here so let's go ahead and freeze this thread like this let's go ahead and now well now we'll make the flag threads to cursor work this they decide really hmm got something something I found a fix so now we'll get you added a lock we'll save run flag thread to cursor perfect now they're all outside the lock nobody's holding a lock and so what I'm gonna do in this particular case is now I'm going to come in and I'm going to freeze all of these threads so I'm going to go ahead and right-click I'm gonna say freeze and I'm gonna freeze all threads below so none of those are gonna run and the beauty of this is remember I was up here on this thread 9 so now when I hover over this right and I can see my source that I have all these seven threads that are here and I have one thread that's here I'm gonna go ahead and thaw this thread and then to show that it's gonna work like I want I'm gonna go ahead and set a breakpoint here and when I hit continue notice that candidate if I go back up here has remained nine and all these other threads are still here so by freezing those threads what I'm preventing them from doing is I'm preventing them from running so it allows me to work with the one thread that I care about in a fashion while not dealing with other threads hitting breakpoints or things like that that whatever connected if I'm it's not possible to freeze because you're gonna deadlock or something like that what you can do is you can I mention it before but use a filter break point and what the filter break point does the filter break point allows me to specify I could do based on the thread ID and I have the break point location toolbar specified right now it's an optional thing to turn on and by having it on I can see the thread ID just on the tool bar I don't have to go look in the threads window or the Prioleau stacks window or anything else so I could say thread ID equals 10,000 and 4 and then that break point would only hit on that particular thread regardless of the value the reason we did this is there are eight threads each running that code so every one of them would hit the breakpoint you'd have no idea you you wouldn't you'd have to keep stepping until you got back into the thread that was the one you wanted to look at exact way you frozen the other ones so you can watch that thread working with candidate nine go through each line of code exactly so in case you're wondering it actually turns out that my bug here all the numbers that we noticed that we're being figured out were primes that were Prime's that weren't Prime's were perfect squares and that's because my bug here is I have less than instead of less than or equal to the square of the object yeah so that should now go ahead and fix that particular issue I'm just gonna go ahead and stop debugging here we'll go ahead and restart we don't need to and so now I want to go ahead and do this instead of unfreezing all of our threads go ahead and calculate primes and you shouldn't see any more big squares in there cool so looks like we fixed that the last thing I want to show here is something and you mentioned that maybe we could do a sneak peek of what's coming and I'll talk about this is debugging keep corruption can be a really big pain in native code so I'm gonna go ahead and corrupt my heap much like multi-threaded stuff it's unpredictable it's gonna happen I'm hoping my applications gonna gonna get really unhappy here in the near future and it sometimes takes a few but eventually it's gonna die because I'm corrupting the heap every time I do this there we go oh right and so it crashed in some random location and that notice every time I chose that particular set of options it actually corrupted the heap and so I corrupted my eat multiple times and it wasn't till like the fourth time that I chose some option which could have been a completely different option unrelated to heap corruption that it actually crashed and you can see where I crashed as I'm actually simply calling the standard C out and trying to print a message and so that really shouldn't be be crashing right and the problem is at this point where the exception actually occurred it's not interesting the bug occurred a long long time ago where I over wrote memory that I hadn't intended to overwrite and so what's the best way to debug that and this is a piece of something that's actually built into Windows at the moment I have to use the command line to turn it on but I'm happy to announce that in Visual Studio 15 so not 2015 but the version right that you know we're shipping previews of right now we're gonna be adding the ability to turn this capability on through the visual studio you I and so the functionality is called page Heep and the way that i do that is i installed the windows debugging tools so this is they come from the windows sdk and they bring a utility called g flags and so when I turn G flags on for my particular application so it's /p that's the page cheap slash enable and then guess what underscore single app dot eggsy is my particular application it's going to have a performance impact on the application what it's going to do it's gonna actually stop me raise an exception just like when I turned on first chance exceptions a little bit ago when I corrupt that heap okay so now let's go ahead and turn this on its gonna tell me that it's enabled and assuming I typed the name of that correctly let's go ahead and trigger my local windows debugger I'm gonna go ahead and say heap corruption and I'm gonna say Robert and I can see that Visual Studio has broken and it told me that an access violation exception was raised writing the particular location of memory and so actually if I look what I'm doing here creating a message I'm disposing it and then I'm writing information back into that memory that's that since been deleted and so that's why I'm corrupting my heap once I've never freed the memory it goes back I don't know that anymore but I still know the address of it and I'm writing new data into into the address so I basically said Robert I'm returning it to the general resource pool which means somebody else can get it but now I'm writing over somebody else's memory you don't have to worry about that as a c-sharp guy but this is one of the more painful things to debug and so the G Flags capability I think it's worth throwing into a tips and tricks talk yeah and I said I mentioned one eight will expose the way to turn that on through visual in Visual Studio version 15 through Visual Studio you won't have to go get G flags and in foreign application is correct user none for an application so it does have a relatively you know significant 20% or so performance overhead on the execution speed of your application so you wouldn't went on for the whole operating system for example it is possible to do that but your Windows is going to run a lot slower and then when you're done debugging page heap you want to go ahead and turn that off so you just want to call sable the same thing and it's gonna go ahead and now disable that for us as well cool all right so we talked a little bit about perfect it's already so I that have been one of the things I wanted to talk about next perf tubes can be really useful for narrowing down performance problems as you're stepping through through code since we're on the subject of memory I want to go ahead and show our integrated memory Tulane individual studio so I'm gonna come over here now and actually this is I'm mentioned we were gonna turn this into a client-server application right so now I'm in my client-server world I have to start my server here so let's go ahead and say I didn't start it before this so let's go ahead and start that real quick and see debug and guess what server does e alright so server is gonna be running mm-hmm and so now when I press f5 over here in this particular application should build just start for me and now I should be able to enter guess now we're between 100 and 150 and I'm actually not thrilled with that it felt a little sluggish to me well while I was doing that I don't know if you noticed that or not but let's try 50 again because now we don't want to get it if you expect that to be a lot of snappier cuz i'm running everything on my local machine so i'm gonna go ahead and come in here where i'm getting the house probably counting on the fingers of each hand so yes let's go ahead and just and that's right it's county honest turn here so let me go ahead and set a breakpoint here let's go ahead and do our 50 guess again just gonna accidentally get it boom and i break on that and so now when i step I can see this perf dip is taking 470 milliseconds hmm to do this build query base because what I'm doing now let's go ahead and get rid of our parallel stock sniper watch window is I'm going to be passing information back and forth between the client and server using this C++ rest SDK and that's going to be doing JSON so I'm building this query and I can go and step through my code I can see how long it takes and looks like most of that particular time is actually being spent in doing building that query base so as I step here I'm gonna go ahead and let's go back up let's go back in here and so if I want to know what's actually happening in this particular time if I click on this that diagnostic tools window opens and with the diagnostic tools one I'm gonna go ahead and pin it now because we're gonna be setting the time I can see that I'm actually pegging the CPU considering there's multiple cores on here that it'll show me that tooltips to be pop-up here is like 13% of all processors it's a four core machine so I'm actually pegging one processor pretty good while I'm doing that and if I want to know what's going on we have an integrated CPU profiler that was new to update one as well in 2015 and so what this is going to do is I'm gonna be able to go in here and expand and look at what my CPU is actually doing and so as I follow it down sorry let's go ahead and drag this up a little bit now I can follow the hot we call this the hot path and I can see where I'm building query base and another scroll down in my query base I can see that I'm doing a bunch of string functions so I'm concatenated a bunch of strings is actually expensive in using my CPU so without the need to stop debugging and go run a different profiler I simply use perf tips and the integrated tools in the debugger to identify a problem in my code and actually turns out I don't need to build this query base every time because the query base stays the same and I simply append the guests every time so I've already taken the liberty to add a local variable I think it's that if I remember correctly the compiler I'll yell at me if that's not it but a local variable that should be the the value of that this one's a little bit slower to recompile because of the whole C++ rest SDK set of dependencies we've actually identified a performance issue with that and where we're fixing that but all right so there we go thank you and so now when I go f10 you can see that that was really fast in my queries the same I hit f5 boom 50 let's go ahead and unset this breakpoint and I would expect this to be pretty snappy now helps if you remember to unsettle you break points and I think we fixed our snare problem feels pretty feels pretty snappy well I'm there I want to briefly go back here and look at my diagnostic tools window and I'll go ahead and expand this out here and I can see something that concerns me here notice that my memories been ticking up the whole time that I've been doing this so much like we have integrated CPU tooling we have integrated memory tooling as well I may need to turn this on again this is off by default much like pay cheap because it introduces a performance hit to excuse the speed of your application somebody go ahead enable snapshots to begin heat profiling I'm gonna take a snapshot and this is gonna set a baseline for me and the nice thing about being integrated 50 buggers you can get really fine-grained control so let's go ahead and come in here where I'm gonna go ahead and say f9 let's go ahead and set a breakpoint so I can say 50 again hit that breakpoint I can take another snapshot I can see what's changed nothing's changed between those two points so I can do snapshots while the program is running or at broome K but now I can step over this line of code I've executed only one line of code and in this particular area hubs come back here I can get back to my diary's tools window I should pin that again and I can take another snapshot and I can see that I've allocated 694 additional kilobytes in that time when I click on this it's gonna open a diff view for me and let's go ahead and do view fullscreen it's another View mode of the editor and so that I can see what's changed I've allocated one additional G result object type and as I've allocated one G additional G result object type I can see that it's big so that's where all that memory is going and when I come in here and click on this I can see the instances of that because I'm stopped in the debugger this is the advantage of being at a breakpoint or when I stop that I can actually inspect it in the debugger I can see the values of it just like code in the watch window so if I'm trying to figure out what's this object type I can help answer that question and then when I come over here I should know so let's run a little just a little bit longer to see maybe things aren't going aren't are going okay so let's go ahead and say a5 boom let's do well maybe a gut was gonna get cleaned up a little bit later that's suspicious we'll take one more snapshot I'm going to have 11 kilobyte so something's still in there zero count diff between those okay so let's keep going but now I can go back and I can baseline this between snapshot one snapshot two so if I go back and basically I'm tuned snapshot one sure enough there's still one G result created so I'm leaking that somewhere and when I come in here when I select this I just can go to the source code where I'm actually allocating that and so I can see that I'm allocating this G result pointer I'm returning it up here but actually it turns out that I'm never never freeing that object so this is where I get it back I come down here I get that pointer so if I were to simply add a delete to go ahead and delete that guest result that would go ahead and fix that particular issue for me the last two things I want to show while I'm here is I'm talking mention G result and so let's go ahead and do one more guest absolute from the right thing 50 go back to visual studio and so when I look at this particular guest result value it's not a very pretty view right so I have this guess which is kind of interesting but then I have this binary data that I don't care about at all I have a server result and all I really care about is the actual result of that server result so if I want to tell the debugger had have visualized this and presented in a nicer fashion I can edit add what's called an at this file that's an XML way to this based way to describe my the way that I want the debugger to actually present this and new to 2015 you could always do in that vis but you used to have to go down into the visual studio installation directory under Program Files and modify the built-in NAT files NAT vis file we added the ability to just simply add it into your project so you can see I just have my dot in that vis in the root of my guess what CLI project and when I uncomment this ctrl K you I'm going to save it and you're gonna see the debugger is gonna instantly update it either you have to stop debugging and now all right it just tells me guess results too low and then when I expand it I can see the guess and I can see the result so a guess was 50 it was too low and if I the all of the other group that I was seen before is hidden under this raw view node so not visiray useful way to describe stuff that you actually want to like how you would want to look at your objects not necessarily how the objects are formatted in memory the last thing I want to show was also new to visual studio 2015 update 1 so let's make a guess of 101 let's not hit our break point and we're gonna break on a read access violation and so the issues here is I have a couple change pointer so I'm going to come down here and show this real quick right so I have guest result arrow server result arrow result and then it equals this value dot as integer so something triggered an access violation how do I know what we tell you now so we're telling you guess result arrow server result was 0 Evan so I can see the server result wasn't initialized correctly this is one of those little things but it can save quite a bit of time right so when you look at this line of code that I'm on right here right there was one two three four potential things that could have triggered Nexus violation instead of hunting or going the watch window hovering over things we just tell you now as part of the exception message that pops up in visual studio cool cool so I think that's what we were trying to talk about today Robert very cool stuff so again a lot of this stuff's been in there for a while some of the stuff new to 2015 or a couple of the updates correctly gave us a taste of what might be coming down the pike that's right cool so when the pike arrives when we aren't down the pike we'll have you back and you can show us more stuff that's new in the next version which is right now in preview that's right preview - I think right yeah preview - is out there'll be a preview 3 coming up at some point a future alright cool so we're getting there it's right in there just keep watching expectancy regular previews thanks for coming on my pleasure all right hope you enjoyed that and we will see you next time on visual studio toolbox [Music]
Info
Channel: SPIDER WEB TECH TECH
Views: 3,581
Rating: 4.8125 out of 5
Keywords: Tips and Tricks C++ Debugging in Visual Studio, debugging, visual studio 2019, visual studio, c#, class 12 computer science, c++ programming, virtual terminal, ansi code, terminal emulator, linux, borland c++, best computer course, learn computer, technology, java, python programming language, html, programming language, coding, learn, skills, how to learn c++, best places to learn c++, learn c++ fast, learn c++ by coding, best way to learn c++, how to study c++, what is c++, php, data
Id: _-MnwUYdggs
Channel Id: undefined
Length: 44min 35sec (2675 seconds)
Published: Mon May 27 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.