Game Development Community

dev|Pro Game Development Curriculum

How to do a memory watch.... and Torque helps an indie album hit the Grammys?

by Drew Parker · 04/09/2009 (7:01 am) · 18 comments

Let me introduce myself – and Torque did what?
www.drewparker.com/refimages/albumCover_sm.jpg
Hi all. Short introduction to start. My name’s Drew and I’ve been a Torque freelancer for 5 years. This has given me the financial support and freedom to pursue my first true love – music. Though it took almost four years and the hard work of many, my first indie pop/rock album is complete with a few accomplishments to boot (hopefully more to come)

I recently performed live on a TV talk show a song called “Sticky Rice” off my debut album, “On My Way Home.” The album also made it to the first round of the 2009 Grammy Awards (hard to do, say like 100-200 per category make it out of 50,000-60,000 submissions).

Video of me performing on TV a song off my debut indie pop/rock album


So here is me performing "Sticky Rice" from my debut album "On My Way Home”. It’s a song about growing up in Tokyo, Japan. Plus the song has two video game references, so you have to watch it. :)



Me playing “Sticky Rice” on TV on youTube

Moving right along

In this blog, I’d like to do two things:
1) Say “thanks” to GG and the GG Community for helping to make this dream of releasing an indie pop/rock album a reality, by helping me with Torque
2) Show you how to fire a breakpoint when a variable in memory changes

THANK YOU GG and GG community


Thank you GG for making fantastic high-quality products and releasing them at indie prices, so that I can make a living and have a blast doing it! Thank you GG community for answering a bazillion of my questions even when it seemed they would never end! And most of all, thank you Torque for being so awesome!!

Watching a memory location (Setting up a data breakpoint)


This is some good stuff to have in your debugging toolkit. Setting a data breakpoint allows you to breakpoint a memory location instead of a code line, and when that memory location changes, the IDE will break wherever that variable changed. Is that awesome or what?

Why is that handy? Let me give you an example. Have you ever had times where something is changing a certain variable, but you don’t know what it is? You could have a bug messing up a variable but can’t find the cause, or your investigating 3rd party code to try and understand how it works.

This technique also lets you know when "some other code is inadvertently stomping your memory location without ever directly accessing your variable (like a string buffer overrunning)." (Thanks Matt Fairfax)

The old solution I use to have for this was to search the code for function calls, do code traces, and when it gets worse (like there are multiple paths to the same variable) start putting breakpoints everywhere to see which path was changing the variable. And obviously that won't work for a buffer overrun anyway.

Now the first thing I try is setting a data breakpoint, and it has saved me boatloads of time. So let’s get started!

My setup


In this tutorial I’ve used Microsoft Visual Studio 2005 Express Edition (C++). But for any good IDE I’m betting they will have this feature.

Getting your variable


First, figure out what variable you want to watch. In this case I want to watch the mStretch variable of one of my classes. I put a breakpoint in a function that can SEE this variable before it changes in a way I care about (maybe a constructor, or some processing loop). Below is a cut and paste from the IDE watch window of the variable:

mStretch	0.00000000	float

Next, use the IDE watch window to discover its memory location this application run (when you restart the application, the memory location will probably change). Do that by adding a & directly in front of it.

&mStretch	0x0117cf44 	float

You'll have to figure out how to display the value correctly in the watch window with just the memory address. It will take some casting and playing around. Here is how to do a float:

*(float*)(0x0117af44)	0.00000000	float

In this case, the variable we want to watch is mStretch. It's memory location is 0x0117cf44. To watch that memory location in the watch window, paste this line into a watch window entry:

*(float*)(0x0117af44)

Setup a data break point


Now that you have your memory location, you need to add a data breakpoint. This will stop the program from executing whenever that data location is changed.

In the application menu at the top of the screen, do: Debug->New Breakpoint->New Data Breakpoint

In the "Address" field, put the memory location you set up previously: 0x0117af44
Adjust the byte count if you need to. Since we're watching a 32 bit (4 byte) float variable, 4 bytes is the correct size for this example. (It is also the max byte size allowed). However, if you were watching a char (U8) variable, the correct byte size would be 1.

Now Hit the OK button.

The data breakpoint will show up in the breakpoint window. It will say:

When '0x0117af44' changes (4 bytes)

Now the program will stop whenever that memory location changes. This may or may not be what you want. If the program is stopping all the time, you may have to filter out some changes by setting a condition.

Setting a stop condition


Sometimes you will only want the program to break when the memory location changes in certain ways, not when it changes in ANY way. So you have to set a condition. Right-click on the breakpoint and select "Condition."

A new popup appears:

Enter an expression. I wanted to detect when the value had changed by a certain amount, so this is what I did:

*(float*)(0x0117af44) > 0.3


Click the "Is true" radio button.

Let her rip!


Now run the program and turn on the breakpoint. The program may run noticeably slower. But when the watched variable mStretch gets above 0.3, the program will break! Then you can look at the callstack to find out what was causing the change.

Yeah!

I've found I can have a handful of data breakpoints and Torque will execute in debug build ok. But if I have over 5 or more data breakpoints, it really starts to chug.

Wrap up


That’s a wrap, and I hope that was helpful for somebody!

If you liked this write-up and want to get in my good books, you can watch my music video above to help raise the play count. :)

If you are really happy and want to get on Santa’s nice list, if you liked the video you can give it a good rating by signing into youTube. That will boost it on youTube’s page, giving it exposure to more viewers.

Thanks for reading, and thanks for being such an awesome community!

- Drew

#1
04/09/2009 (7:34 am)
Hey Drew. Congrats on your album. Very catchy tune ... I will be sure to look out for it when (or if) it hits the stores here in the U.K. :)
#2
04/09/2009 (9:13 am)
@ Quinton - thanks a lot! The album is available on iTunes worldwide, including the UK. No retail stores there yet, though. :)
#3
04/09/2009 (1:18 pm)
Awesome job Drew! Good luck following your dream!
#4
04/09/2009 (2:39 pm)
Awesome blog, Drew. A great song and an advanced and useful debugging technique in one post? You may never be able to top this. ;)
#5
04/09/2009 (3:35 pm)
I use this debugging technique fairly often. One thing I have found that it is handy for is letting you know when some other code is inadvertently stomping your memory location without ever directly accessing your variable (like a string buffer overrunning).
#6
04/09/2009 (6:15 pm)
AWESOME Drew!

Glad to see youre still rockin and still torquein! It's awesome you're following your dreams. Been a long time... I'm living in Cincinnati now. Congrats on all the success, I'm sure it's been quite a journey. Drop me a line if you get a chance.
#7
04/09/2009 (7:23 pm)
I only sat through about 20 sec of the song, but what a great debugging tip! Royalty checks from a content pack pay for my cello lessons so it's extra cool that Torque has allowed you to live your dream too.
#8
04/09/2009 (8:27 pm)
Great song Drew I upped you on YouTube and subscribed to your channel. Good info but it's a little advanced for me. I lived in Japan myself for four years and I have to agree sticky rice is the best. Japan is the place I miss the most from all the places I've been. Great song for us tall guys within the crowd that miss Japan.
#9
04/10/2009 (5:36 am)
Always cool to see someone attempt / succeed at their dream so I wish you success with it! 5 Stared the vid. I've always wanted to go to Japan, might be the gaming connection and the interesting weirdness / fusion of the place.
#10
04/10/2009 (5:46 am)
Congrats Drew. Hope all is going well for you.
#11
04/10/2009 (6:10 am)
Which number do I dail to vote for you Drew?



www.twitter.com/tallgamer
#12
04/10/2009 (9:08 am)
@ Phillip - Thanks!

@ Ben - That means a lot coming from you. Thanks for helping me so much on the forums. :) Glad you liked the song!

@ Matt - Yes, I think that is also what forced me to finally track down and learn this technique, a memory stomp. Thanks for reminding me of that, I'll add it to the list of example uses.

@ Jameson - HEY NOW! So great to hear from you. How's Cincinnati? I'm up in Barrie Canada. Guess we are both a long ways away from North Carolina. :) I saw your mySpace message. I'll definitely drop you a line sometime soon. Thanks for the congrats!

@ Michael - no problem about the song, different strokes for different folks they say. Glad you liked the debugging tip. Playing cello from content pack royalties! That's awesome.

@ Glenn - Thanks for the youTube support! And four years in Japan in not a short time. It's a cool place, isn't it? I was there about 5 1/2 years. I'd love to get back there sometime too.

@ Leroy - Thanks for rating my video! The video game connection in Japan is big. Certain downtown districts had HUGE gaming arcades every couple of blocks, and I mean they were big. Never seen anything like it in the US. That was the early 90s, not sure what happened after the "next-gen" consoles came out. Achihabara in Tokyo was the electronics district. Think 10 square kilometers of nothing but gadget and video game stores. Needless to say, I was a frequent visitor as a kid. :)

@ Owen - Great to hear from you! We've got to catch up, I'd love to hear what you've been up to. What's your email? Drop me a line at drewparker.nc A_T gmail D_T com

@ Donald - Ha! Hold on, let me look it up - ok here it is. :) www.youtube.com/watch?v=q69qFjBZhSg&feature=channel_page




#13
04/10/2009 (10:33 pm)
What part of North Carolina?
#14
04/11/2009 (7:17 am)
Quote:
One thing I have found that it is handy for is letting you know when some other code is inadvertently stomping your memory location without ever directly accessing your variable (like a string buffer overrunning).

Won't you trigger a Debug Guard if you do this?
Anyway, lots to learn here. Thanks!
#15
04/12/2009 (7:45 am)
@ Matt - Greensboro, and then Durham. Jameson, Owen and myself worked on a Torque project together for a research outfit based out of UNC Greensboro. I love it up here in Canada but also miss the good buddies and lush greenery of good old NC! Have you spent some time in NC?

@ Stefan - Glad it was of some use! In terms of a debug guard, I can't remember specifically, that may be the case. When I hit a situation like this (actually, it was string memory being freed incorrectly then still being used, later causing a crash when overwritten), the only way I could solve it was by memory watching multiple values at the same time.
#16
04/12/2009 (8:09 am)
I didn't realize the Express edition of Visual C++ allowed memory breakpoints. I thought this featured was disabled in the Express editions (both 2005 and 2008). Honestly this is the only feature I am missing by not having the Pro edition.
#17
04/13/2009 (11:00 am)
Mark,
I use it pretty regular in VC++ Express 2008 and I seem to remember it being in 2005 as well.
#18
04/13/2009 (11:02 am)
Drew,
I grew up just across the border from Charlotte, NC and worked there for a number of years before I moved out to Oregon.

I've only been to the Durham area a couple of times unfortunately but I do have relatives in that area.