Garbage collection and new keyword in parameters

A

atsmith6

Hi all

I'm trying to better understand the behanviour of garbage collection
in .NET 2.0 and was wondering if anyone could shed some light on the
following:

If I have code something like

private void MyMethod(MyObj obj)
{
// Do something with obj
}

private void OtherMethod()
{
MyMethod(new MyObj);
// perhaps more code here
}

Assuming MyMethod uses the object, where is the point (I'm thinking in
terms of the equivalent to sequence points in the C++ language) at
which the object will be flagged for garbage collection? Is the above
code safe?

Many thanks in advance to anyone who may know the answer and reply.
Any references to MSDN pages would also be considered an added bonus
and very much appreciated.

Kind Regards

Anthony Smith
 
G

Guest

Hi,

It seem that when it exit from the MyMethod method then it check the gc flag
for that object, why, becasue it have no root.
the new object (new MyObj) set by the method and live there, the new object
instance cannot be linked within the OtherMethod method.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top