tags:

views:

64

answers:

4

Hi, I have a confusing problem in my code, in which one object gets initialized properly, but when I look at it through a parent reference, its properties arent initialized anymore. I assigned id's for my objects, and I am perfectly sure that there are no duplicate objects lying around. When looking through the parent, the child is not initialized, but This is something I don't understand. Shoudn't C# references point to the same memory space, and therefore show the correct values even when another reference changed something in that memory space?

EDIT: I don't need the problem above solved. Just a reminder that this is the question which I'm interested in:

So back to the headline question: For easier debugging, I would like to look behind all those abstracted references like I could do in C++. I think it would be immediately clear to me what went wrong when I could do that.

+1  A: 

Take a look here: http://msdn.microsoft.com/en-us/library/f58wzh21%28VS.80%29.aspx

BarsMonster
I'm not in an unsafe context.
Blub
A: 

you can compare references... sounds like you are copying an object somewhere.

Tony Lambert
Yes, it's obvious that those references aren't the same. I'm asking why the memory they are pointing to, isn't the same even though I let them point to the same variable.
Blub
If the references aren't the same then by definition they are pointing to different objects even if their data is the same.
Tony Lambert
+2  A: 

Is it possible that you're using structs and not classes? They get copied on assignment, so changing (or initializing) does not affect the copied object.

If you just want to check whether your objects are the same you can just compare their references. Getting a pointer/address in memory is not possible in a "safe" environment.

Alright, it sucks that this is not possible. Can't be that hard to implement this. If C# had this, it would trump C++ in every way, at least usability wise. This way it will always be a little uncertain what happens internally and I have to look up the referencing internals in every language.My problem was that I had used MemberwiseClone() and the "Parent" variable still pointed to the uncloned parent.
Blub
+1  A: 

If you want to keep track of which object a particular reference is pointing at while debugging, then you can right click on a reference in a Watch window and select 'Make Object ID'. Any other references to the same object will now show the same ID in the debugger (e.g. {1#}), and you can add the ID as a value to watch even if there are no references on the current stack frame.

See here for an example

Phil Devaney
Thank you, this is incredibly useful. I also marked this as answer because it pretty much makes address exposure possible, therfore positively answering the question.
Blub