Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Wednesday, September 14, 2011 1:36 PM
Hi,
I think this is not possible but since I am fairly new to C#, I want to double-check.
I need to find all objects holding a reference to a given object and I need to do so at runtime, in my code. Is there a way to do this without going through all my objects looking for matching references (which is CPU intense).
Thanks!
All replies (7)
Wednesday, September 14, 2011 4:29 PM âś…Answered
In theory it could be done using a technique similar to the garbage collector. Essentially this means finding all root objects (e.g. statics) and tracing the references they hold that end with the required object. The trace could be stored as a doubly linked list so you could then trace back to the root and intermediate objects.
As with the GC there are a number of problems to solve e.g. breaking any cycles.
So in theory I think the answer might be Yes.
But in practice I think it is No - at least not without considerable effort.
PS Of course you also have the problem that just as you have built your list of references the GC kick in so the entries are no longer valid. So you have to prevent the GC moving things or detect when it happens and rebuild the list. Of course that might cause it to kick in again, so....
Regards David R
Every program eventually becomes rococo, and then rubble. - Alan Perlis
The only valid measurement of code quality: WTFs/minute.
Wednesday, September 14, 2011 1:37 PM
Hi..
Im sorry but the answer is no, you cant.
If my post is helpful. Please mark it as such : )
Wednesday, September 14, 2011 2:43 PM
This is basically what the garbage collector is doing. I'm not sure if there's some way to piggy back off of the fact that it is already doing these checks or not. I'm guessing not, but it's worth investigating.
Out of curiosity, are you writing your own GC?
Wednesday, September 14, 2011 4:08 PM
If I'm not wrong, the GC goes from the roots to the referenced objects, not the other way.
Wednesday, September 14, 2011 4:26 PM
If I'm not wrong, the GC goes from the roots to the referenced objects, not the other way.
True, but it's generating a complete graph of that information, not just a partial graph, so (if the information is exposed to you at all) you could in theory look in the other direction.
Tuesday, June 5, 2012 2:57 PM
Sorry for the very late reply.
No, I am not writing my own GC. I was looking for a way to cross-reference objects that represent a graphical programming language. I need to find all symbols and where they are used.
Tuesday, June 5, 2012 5:12 PM
I don't think anybody thinks that are trying to right your own GC. But it looks like the algorithm you would need to use is similar to that of the GC. I.e. to find all arbitrary references to a specific object, start at the roots and trace to the object. That means identifying root objects, following references and knowing that you have reached the specified object.
Not knowing the GPL you are using it's difficult to suggest anything else.
Regards David R
The great thing about Object Oriented code is that it can make small, simple problems look like large, complex ones.
Object-oriented programming offers a sustainable way to write spaghetti code. - Paul Graham.
Every program eventually becomes rococo, and then rubble. - Alan Perlis
The only valid measurement of code quality: WTFs/minute.