Welcome to MSDN Blogs Sign in | Join | Help
Setting conditional breakpoints using object ids

In native code, its sometimes useful to set a breakpoint condition so that a breakpoint will only stop for a particular instance of an object. To do this, I simply use the address of the object:

this == (CMyObject*)0x10fc10

In managed code, the garbage collector moves objects around, so even if you were writing in a language that had great support for pointers, setting a conditional breakpoint this way is probably useless. Instead, you can do this using object ids.

Step #1: Make an Object ID. To do this, get the object that you want to use in your condition into the watch window. Then right click on this object and invoke the ‘Make Object ID’ context menu. The watch window output will now include ‘{1#}’:

+  this {cswin.Form1, Text: Form1} {1#} cswin.Form1

The ‘1#’ indicates that this is the first object ID. Each object ID created will get a new object ID (2#, 3#, etc). There is more documentation under 'Object Identity' on MSDN.

Step #2: Set conditional breakpoint. Right click on your breakpoint and click ‘Condition…’, and set it to what you want. For example:

this == 1#

Happy debugging.

Posted: Wednesday, January 17, 2007 1:31 PM by greggm

Comments

GuerreroTook said:

it's great, but when i stop debugging, visual studio doesn't remember my Object ID, so always have to set object ID to do condicional debug

# January 17, 2007 6:54 PM

greggm said:

Good point. This is because the debugger has no way to presist object ids accross debug sessions. Managed code doesn't have an 'allocation identifier' like native code does.

# January 17, 2007 7:36 PM

Jeff Stong said:

Many times I've made use of the ability to set a conditional breakpoint based on the address of object...

# January 18, 2007 2:13 PM

John Schroedl said:

Great tip.  I didn't know about object id.

John

# January 18, 2007 3:54 PM

Kim Gräsman said:

One issue we see with using the address is that memory blocks are reused frequently, so 0x12345678 doesn't necessarily point to the _same_ object, just to an object of the same type created on the same memory space.

I'm curious, does the object ID feature handle this somehow?

Thanks!

# January 20, 2007 9:02 AM

Kim Gräsman said:

Oh, and does it work for both native and managed code?

# January 20, 2007 9:02 AM

greggm said:

Object IDs work like a WeakReference. So they will always indicate the same object.

This only works with managed code.

# January 20, 2007 12:34 PM

Wooseok Seo said:

Great tip.

Many thanks!

Wooseok.

# January 22, 2007 9:03 AM

jaredpar's WebLog said:

Just found out about a neat way to keep track of managed object references while debugging. Check out

# April 18, 2007 5:07 PM

ISerializable - Roy Osherove's Blog said:

Some cool stuff happening in the testing world: * Project white seems like a nice takeoff on winforms

# February 24, 2008 3:41 PM

Bite my bytes said:

Links of the Week #25 (week 8/2008)

# February 24, 2008 5:25 PM

Zunanji viri said:

Development Top-10 Application-Design Mistakes - How not to design your UI. It's hard not to nod

# February 24, 2008 6:12 PM

DotNetKicks.com said:

You've been kicked (a good thing) - Trackback from DotNetKicks.com

# February 25, 2008 8:04 AM

Richard's Rant said:

Interesting blog post describing some of the shortcomings of ASP.MVC Anotherblog post, this time about

# February 25, 2008 8:48 PM
New Comments to this post are disabled
Page view tracker