W
web1110
If I have 2 variables, A and B, referencing the same object and then do a
A.Dispose(), what happens to B?
A.Dispose(), what happens to B?
web1110 said:Hi y'all.
After looking in to this, I better elaborate.
I have a control object. It operates it 2 modes:
A. It can display itself. The display handled by a control display
manager.
B. It can be utilized without being displayed.
Now, the situation I am confused about is as follows:
A. I instantiate such a object.
B. I give it to the control manager to display.
Hi y'all.
After looking in to this, I better elaborate.
I have a control object. It operates it 2 modes:
A. It can display itself. The display handled by a control display
manager.
B. It can be utilized without being displayed.
Now, the situation I am confused about is as follows:
A. I instantiate such a object.
B. I give it to the control manager to display.
C. When the user is done, the control manager (which keeps a reference to
the control in a stack) disposes of its copy.
D. However, the class that instantiated the control in the first place
wants to keep the object present for additional reference.
What happens to the original object after the control manager disposes of
its version?
Thanx,
Bill
Richard Blewett said:You question makes no sense.
You always pass around references to objects - you cannot pass an object
by value.
As far as the OP's question is concerned you have entered a world of pain
that IDisposable does not cope with. For IDisposable to work correcctly
there has to be a strong notion of ownership of the object. You only have
four options really:
1. Decide who is going to take responsibility for calling Dispose and
stick to that architecturally
2. Let the finalizer talke care of releasing resources and don't call
Dispose at all
3. Implement some form of reference counting architecture where everone
"releases" their reference (calling a method) and then the object Disposes
itself when the refcount goes to zero.
4. Clone the object before its passed to the controller so they each have
an independent copy they can Dispose
Richard Blewett said:You question makes no sense.
You always pass around references to objects - you cannot pass an object
by value.
As far as the OP's question is concerned you have entered a world of pain
that IDisposable does not cope with. For IDisposable to work correcctly
there has to be a strong notion of ownership of the object. You only have
four options really:
1. Decide who is going to take responsibility for calling Dispose and
stick to that architecturally
2. Let the finalizer talke care of releasing resources and don't call
Dispose at all
3. Implement some form of reference counting architecture where everone
"releases" their reference (calling a method) and then the object Disposes
itself when the refcount goes to zero.
4. Clone the object before its passed to the controller so they each have
an independent copy they can Dispose
Richard Blewett said:Agreed, thats why I said he has entered a world of pain.
and 3) is the best approach if you can guarantee not to get circular
references. Although I do think 2) has merit if there really is no strong
notion of ownership - granted all the issues about finalizers - but
sometimes only the CLR has a real idea of when something can be cleaned
up.
Richard said:You question makes no sense.
You always pass around references to objects - you cannot pass an object by value.
Richard Blewett said:You always pass around references to objects - you cannot pass an object by value.
John Bailo said:void test (object o)
{
object = Int32(5);
}
void test2()
{ object o = Int32(7);
test(o);
//so what is the value of o at this point? 7 or 5 ?
}
John Bailo said:void test (object o)
{
object = Int32(5);
}
void test2()
{
object o = Int32(7);
test(o);
//so what is the value of o at this point? 7 or 5 ?
}
http://www.c-sharpcorner.com/Code/2005/Feb/Willswapwork.asp
"C# does manipulate objects by reference, and all object variables are
references. On the other hand, C# does not pass method arguments by
reference; it passes them by value, (even if the method arguments are of
reference type)."
Alex said:This leads me to a question.
Since .NET objects are reference types (and are therefore always
passed by reference), is there any advantage to explicitly declaring
and passing objects as "ref"?
Joanna Carter (TeamB) said:"Alex" <[email protected]> a écrit dans le message de (e-mail address removed)...
Since .NET objects are reference types (and are therefore always passed by
reference), is there any advantage to explicitly declaring and passing
objects as "ref"?
No, objects are reference types that are always passed by value unless you
declare a ref parameter.
The difference is that the ref parameter allows you to change the actual
object that is passed in, rather than just being able to change the
properties of the object with a non-ref parameter.