Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

debugging with pointers in C#

Tags:

c#

pointers

When I programmed in C/C++, I often include pointer values in tracing. This helped correlate objects between trace messages. In C#, I don't have a pointer value available I can trace. Are there any good alternatives for traceable object IDs??

I don't know offhand how to get the underlying memory address, but even if I did that wouldn't help much as the GC could move objects around.

like image 747
Frank Schwieterman Avatar asked Dec 09 '25 12:12

Frank Schwieterman


2 Answers

A tip: inside Visual Studio, you can right click on the object in the locals/watch window, and select "Make Object ID" to give the object a unique tag.

http://blogs.msdn.com/saraford/archive/2008/09/16/did-you-know-how-to-create-an-object-id-to-keep-track-of-your-objects-314.aspx

like image 79
Brian Avatar answered Dec 11 '25 02:12

Brian


Use Object.GetHashCode(). The GetHashCode() method can be accessed from all objects since every class in C# derives from Object. But its guaranteed to be unique only when Object.GetHashCode() is called and its result is used in some way for the generation of hash codes for user defined classes. Please note this is not an absolute guarantee you may still end up getting the same Hashcode for more than one object.

like image 43
Autodidact Avatar answered Dec 11 '25 03:12

Autodidact



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!