CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Apr 2012
    Posts
    13

    Find all instances of type

    Let's say I've defined a class, and I'm using this class all over the place in my code.

    Now let's say that I need to quickly access every instance of this class and do something to it. Is there a way to do this in C# without keeping a list or dictionary of all of the instances?

    In other words, can I say "find all instances of class Foo on the heap"?

  2. #2
    Join Date
    Oct 2011
    Posts
    97

    Re: Find all instances of type

    I'm not 100% sure, but I'm pretty sure the answer is no. But remember, since C# has garbage collection, if there's an instance of it on the heap, you do have a reference to it somewhere. I think the easiest way, however, would be to just keep a list of references. In the constructor of the object, just have it add itself to the list, and in the destructor, have it remove itself.

  3. #3
    Join Date
    Jun 2012
    Posts
    10

    Re: Find all instances of type

    Could you put a static list<ref classType> field in the class that's updated by the contructor every time a new instance is created?
    The constructor could add a reference to the list every time a new instance is created.
    Then you could could build a function that iterates over the list, ignoring errors (For objects that have been garbage collected) and return you a list<classType>.

    It's a wacky idea, but I don't see why it wouldn't work.

  4. #4
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Find all instances of type

    Quote Originally Posted by B3ertrnd View Post
    Could you put a static list<ref classType> field in the class that's updated by the contructor every time a new instance is created?
    The constructor could add a reference to the list every time a new instance is created.
    Then you could could build a function that iterates over the list, ignoring errors (For objects that have been garbage collected) and return you a list<classType>.

    It's a wacky idea, but I don't see why it wouldn't work.
    The problem here would be stale references. You would need to make sure that objcts which are no longer needed are cleared from the list or they will *never* be GC'd.

    Also, you can't use 'ref' in a generic argument, nor would there be any reason to do so.
    If you liked my post go ahead and give me an upvote so that my epee.... ahem, reputation will grow.

    Yes; I have a blog too - http://the-angry-gorilla.com/

  5. #5
    Join Date
    Jun 2012
    Posts
    10

    Re: Find all instances of type

    Apologies, I didn't mean the 'ref' keyword, I meant it to mean (store a reference to).
    For the stale object problem, perhaps you could implement IDisposable and have the instance remove itself from the static list when it's disposed by the garbage collector.

  6. #6
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Find all instances of type

    Quote Originally Posted by B3ertrnd View Post
    Apologies, I didn't mean the 'ref' keyword, I meant it to mean (store a reference to).
    For the stale object problem, perhaps you could implement IDisposable and have the instance remove itself from the static list when it's disposed by the garbage collector.
    But it won't be cleaned up by the GC because there is a valid reference to the object in the list. There is no other way; you would have to manually manage the list of objects (and you're probably just asking for trouble).
    If you liked my post go ahead and give me an upvote so that my epee.... ahem, reputation will grow.

    Yes; I have a blog too - http://the-angry-gorilla.com/

  7. #7
    Join Date
    Jun 2012
    Posts
    10

    Re: Find all instances of type

    Ah, seems I'm an idiot. I'm pretty new to programming and this is more about me learning than anything else. I'd be interested in understanding how it would be done by looking at the heap. The points of interest for me would be things like how protection-levels come into play and how many 'heaps' there really are, as don't objects have their own heaps? And what about structs? Are these not on the stack? Do threads have their own stacks? Who can see what? There must be a way of just parsing the memory at a low level, but then then must be security measures against this?
    There's a whole lot of potential to learn something here so please do post if you figure it out!
    Last edited by B3ertrnd; June 4th, 2012 at 01:43 PM.

  8. #8
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Find all instances of type

    Not an 'idiot' at all, everyone starts somewhere, and the mere fact that you wish to learn about this stuff puts you ahead of most people already.

    You do seem to hold some misconceptions that are pretty common though. Stack v Heap is not a good way to think about value types v reference types (i.e., structs v classes). You can certainly have a heap-allocated struct. Read: http://blogs.msdn.com/b/ericlippert/...on-detail.aspx

    Also, the GC is (sort of) an implementation detail (until you run out of memory), but it's a good abstraction to break through and learn about. The C# GC is a generational garbage collector. There are basically two heaps; the 'regular' heap and the LOH (large object heap). There are some good articles out there. Here are a couple:

    http://msdn.microsoft.com/en-us/library/ee787088.aspx
    http://msdn.microsoft.com/en-us/magazine/cc534993.aspx
    Last edited by BigEd781; June 4th, 2012 at 02:07 PM.
    If you liked my post go ahead and give me an upvote so that my epee.... ahem, reputation will grow.

    Yes; I have a blog too - http://the-angry-gorilla.com/

  9. #9
    Join Date
    Jun 2012
    Posts
    10

    Re: Find all instances of type

    Thanks for the links. I'm a great believer in the old adage "what's in the micro is in the macro", so if I can get a really good understanding of the fundamentals then the complex stuff will just make sense as is. The low-level stuff is where my heart would lay, so thanks for this.

  10. #10
    Join Date
    May 2007
    Posts
    1,546

    Re: Find all instances of type

    You could create a:

    static List<WeakReference> MyObjects;

    Then every time you create an instance of MyObject (for example do this inside the MyObject constructor or whatever) you can do:

    Code:
    public class MyObject
    {
        public MyObject ()
        {
            MyObjects.Add (new WeakReference (this));
        }
    }
    This will allow the objects be garbage collected and still let you gain access to all the ones which have not been collected yet. You will have to manually remove all WeakReference objects from the list once their 'Target' property returns null. When 'Target' returns null, it means the MyObject instance has been collected.

    Is there a better way to do whatever it is you want? What is it exactly that you want to do?
    www.monotorrent.com For all your .NET bittorrent needs

    NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured