.NET Framework Bookmark and Share   
 index > Common Language Runtime > it's possible to know if an object is referenced?
 

it's possible to know if an object is referenced?

hi all, for debugging purpose i want know if an object is already referenced by same other objects.

what i want to do is samething as

void Checks(object o)
{
WeakReference wr = new WeakReference(o);
o = null;

bool b = GCHelper.CanBeCollectedByGC(o.Target);
//Log
}
korkless
A WeakReference is specifically designed to deal with this. Its IsAlive property tells you if the object still exists. If it no longer exists, it is not referenced by anybody. Why doesn't it suit you?

Hans Passant.
  • Marked As Answer bykorkless Friday, September 11, 2009 12:32 PM
  •  
nobugz
Hi,
You need to implement the singleton patteron for this

have a look at this link
http://msdn.microsoft.com/en-us/library/ms998558.aspx
Best Regards, C.Gnanadurai ----------------------- Please mark the post as answer if it is helpfull to you
Gnanadurai
i don't need a singleton, in same place of my application i want know if an object out of that method can be garbaged by GC or if there's same bug and it's already referenced by same other places
korkless
A WeakReference is specifically designed to deal with this. Its IsAlive property tells you if the object still exists. If it no longer exists, it is not referenced by anybody. Why doesn't it suit you?

Hans Passant.
  • Marked As Answer bykorkless Friday, September 11, 2009 12:32 PM
  •  
nobugz
yeah, you are right :), isalive tell if the object has been already collected but i want check if the object can be collected so i wrongly discard it,
if i call gc.collect before checks isAlive, it works perfectly for what i want, thanks
korkless

You can use google to search for other answers

Custom Search

More Threads

• using default ThreadPool for socket programming
• COM Interop and AppDomain problem.
• PE Image Size
• webbrowser in 2005
• Applying CAS to disable/control process creation in .net
• Evalcom2 Creating a DLL
• Double references .. how to remove?
• Error when retrieving a C++ class from VssApi.dll ?
• Comma in command line agument of console application.
• XSD.exe and optional, simple-type attributes