Destroying objects.

Q

qwert

Hello,

I have multiple references to an object. Is it possible to destroy the
object using one of the references, and setting the rest of them to Nothing?

Dim objA as New A
Dim objAR As A
objAR = objA
objA = Nothing
REM Now the object is still alive.

Dim objA as New A
Dim objAR As A
KILL(objA)
If objAR is Nothing then blnGreat = True

Thanks.
 
Q

qwert

What I meant was:

Dim objA as New A
Dim objAR As A
objAR = objA
KILL(objA)
If objAR is Nothing then blnGreat = True
 
B

Brian Gideon

No. That's not possible. You have very little control over when an
object is actually destroyed. That's the job of the garbage collector.
What you do have complete control over is how many references there
are to an object.

If you're wanting deterministric finalization then use the IDisposable
interface and adhere to the associated design pattern.

If you're wanting to use advanced techniques for controlling the
lifetime of objects you can use the WeakReference class.

Can you explain a little more about what you're wanting to accomplish?

Brian
 
Q

qwert

I want to set all references to an object to Nothing in one statement.
Instead of setting X references to Nothing, I would like to call one
statement with one of the X references, setting them all to Nothing.

I'm not interested in when the object is destroyed. As long as all the
references are Nothing so the garbage collector knows it's good to go.
 
C

Cor Ligthert [MVP]

Qwert

As you do this, then
Dim objA as New A
'A new object A is created on the stack the reference to it is objA

Dim objAR As A
'A placeholder is created on the stack to hold a reference to an object A

objAR = objA
'There is told that the placeholder from objAr is not pointing as well to
the objA

KILL(objA)
'This instruction does not exist if you would see this as
OjbA = Nothing than the only thing that happens that the placeholder for the
reference in the stack is set to Nothing (probably physical null or
something).

If objAR is Nothing then blnGreat = True
'the object itself will still exist until that there is no reference anymore
to it from any place, even the places that you did not set yourself.

To give another example
Dim A as new classA
Dim A = new classA

Here are two objects created on the managed heap.
The memorey of the first one will direct as the Garbage Collector start be
released.

And as advice don't take to much care of releasing objects as long as there
are no problems. Used memory is never a problem as long as there is enough.
Some strange behaviour is at some developpers that it has forever to be as
low as possible.

I hope this gives an idea.

Cor
 
C

Cor Ligthert [MVP]

Duh and typo and a little addition.
objAR = objA
'There is told that the placeholder from objAr is not pointing as well to
the objA
*now* pointing as well to objA
If objAR is Nothing then blnGreat = True
'the object itself will still exist until that there is no reference
anymore to it from any place, even the places that you did not set
yourself.
So in this case when there is not any other code extra
objA = nothing

Be aware that this has only sense with globaly created objects. The others
goes direct out of scope if there is not pointing indirectly something to
it. (By instance because it is added to a collection as by instance a
control to a form).

Cor
 
B

Brian Gideon

qwert said:
I want to set all references to an object to Nothing in one statement.
Instead of setting X references to Nothing, I would like to call one
statement with one of the X references, setting them all to Nothing.

That's not possible either and for a good reason. It would be nearly
impossible to write stable code if a reference you declare and
initialize could be null at anytime.
I'm not interested in when the object is destroyed. As long as all the
references are Nothing so the garbage collector knows it's good to go.

If you want all references to equal Nothing then go through each one
and set them to Nothing. That's the only way to do it. Maybe I'm not
understanding what it is you're wanting to accomplish. Can you explain
why you believe you can't set each reference to Nothing?
 
Q

qwert

I can set each reference to Nothing. It's not really a technical problem. I
wondered what if a third party uses the assembly, and (s)he is a bit sloppy,
and doesn't clean up as supposed to. Call this one function and away it
goes.

Thanks for the responses.
 
B

Brian Gideon

Okay, this is making more sense now. There's not much you can do to
completely eliminate the possibility of a 3rd party using your assembly
incorrectly. A well designed library can mitigate the problem to some
extent, but if a developer throws your object into a Hashtable or
something and forgets about then that isn't your fault.

Brian
 
C

CMM

Not sure I understand. You can't control who "references" your object and
you can't control when the Garbage Collector destroys it (which it won't do
if there is anybody "referencing" it).
But, you sure can control whether your object kills itself (it refuses to do
work). Implement IDisposable. Call Dispose on the object... keep a private
variable stating in the object that states it considers itself disposed and
put in a conditional checks into each property and method accessed in the
object.

Public MyClass
Implements IDisposable
...
Public Sub DoSomething()
If Not m_disposed Then
'do your work
Else
Throw New Exception("nuh uh, you can't access me.")
End If
End Sub

Public Property Something
Get
If Not m_disposed Then
Return m_something
Else
Throw New Exception("nuh uh, you can't access me.")
End If
End Get
....
 
C

CMM

You don't even have to do the conditional. You can put an Assert at the top
of the method property code which will terminate the method immediately if
m_disposed.
 
B

Brian Gideon

I usually put all of my argument and state validation code at the top
of the methods in individual if statements. An ObjectDisposedException
should be thrown if that check fails. The problem with Assert is that
it won't make it into the release build.

I try to avoid throwing exceptions from properties since callers don't
expect them in that case.
 
C

CMM

Trace.Assert does make it into Release AFAIK (you're thinking of
Debug.Assert).
But, anyhow, I meant more your own *encapsulated* exception thrower rather
than littering conditionals in all your methods.... which is ugly and hard
to maintain.

DisposedAssert() 'called at the top of your methods

Private Sub DisposedAssert()
If m_disposed Then
Throw New ObjectDisposedException
Endif
End Sub
 
B

Brian Gideon

Ah yes. I was thinking of Debug.Assert. Trace.Assert does indeed get
included in release builds. Well, that is as long as TRACE is defined.
Either way, it's still not an appropriate strategy since they won't be
throwing the ObjectDisposedException. But, that's all moot since I
realize now what you were talking about :)

And yes. I see what you're talking about with moving the checks into
separate methods. It does make things easier.
 

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