C++ debuggin question - detect change in variable?
-
Hi I have a short question about debugging in C++. Let's say I have a member variable that rarely changes, and in my program I suspect there's an invalid modification made to this variable that causes my program to crash. Is there a way to set up a debugger in such a way that it will trap when the variable is changed? Simple text find in the source files won't do, since more complex circumstances (e.g. int tmp[100]; memcpy(tmp, some_buffer, 101*sizeof(int) /*oooops*/); ) MAY also change the value of the variable if it's located right after tmp[99], and these kinds of error won't be detected by source files text find. Thanks!
-
Hi I have a short question about debugging in C++. Let's say I have a member variable that rarely changes, and in my program I suspect there's an invalid modification made to this variable that causes my program to crash. Is there a way to set up a debugger in such a way that it will trap when the variable is changed? Simple text find in the source files won't do, since more complex circumstances (e.g. int tmp[100]; memcpy(tmp, some_buffer, 101*sizeof(int) /*oooops*/); ) MAY also change the value of the variable if it's located right after tmp[99], and these kinds of error won't be detected by source files text find. Thanks!
Hello Indrawati, You've touched on one of my favourite debugging techniques. I assume that you are using Visual C++ 6.0. Do the following : 1. Put a breakpoint at the place where your member variable is first created, A good place would be when your object is instantiated. 2. Start to debug your app. 3. After your object has been created and initialized, select and highlight your member variable. 4. Press SHIFT+F9 to bring up the Quick Watch window. 5. You will see your member variable displayed on the edit box labelled "Expression". 6. Put a '&'symbol just infront of your member variable. The memory address of your member variable will appear on the value column. 7. Remember this memory address. 8. Now press CTRL-B to bring up the "Breakpoints" dialog box. 9. Select the "Data" tab in the "Breakpoints" dialog box. 10. In the edit box labelled "Ënter the expression to be evaluated", type in the memory address of your member variable. 11. In the "Enter the number of elements to watch in an array or structure" edit box, type in the number of bytes that your member variable would take up. 12. Click "OK". You now have a memory address breakpoint. Whenever the contents of this memory address changes (up the the number of bytes you specified in point 11), a message box will appear rto indicate to you that changes have occurred. Hope this advise will help you. Best Regards, Bio.
-
Hello Indrawati, You've touched on one of my favourite debugging techniques. I assume that you are using Visual C++ 6.0. Do the following : 1. Put a breakpoint at the place where your member variable is first created, A good place would be when your object is instantiated. 2. Start to debug your app. 3. After your object has been created and initialized, select and highlight your member variable. 4. Press SHIFT+F9 to bring up the Quick Watch window. 5. You will see your member variable displayed on the edit box labelled "Expression". 6. Put a '&'symbol just infront of your member variable. The memory address of your member variable will appear on the value column. 7. Remember this memory address. 8. Now press CTRL-B to bring up the "Breakpoints" dialog box. 9. Select the "Data" tab in the "Breakpoints" dialog box. 10. In the edit box labelled "Ënter the expression to be evaluated", type in the memory address of your member variable. 11. In the "Enter the number of elements to watch in an array or structure" edit box, type in the number of bytes that your member variable would take up. 12. Click "OK". You now have a memory address breakpoint. Whenever the contents of this memory address changes (up the the number of bytes you specified in point 11), a message box will appear rto indicate to you that changes have occurred. Hope this advise will help you. Best Regards, Bio.