how to make destructor

B

Boni

Hi all,
is it possible to force GC to call destructor?
I did:
class A
sub dispose
end sub
end class
but dispose is never called.
Do I have to register it somehow?
Thanks in advance,
Boni
 
H

Herfried K. Wagner [MVP]

Boni said:
is it possible to force GC to call destructor?

'GC.Collect', 'GC.WaitForPendingFinalizers'. Be aware that explicitly
calling these methods can lead to reduced performance. Thus I recommend to
find another solution, such as implementing the 'IDisposable' pattern if
unmanaged resources are used.
I did:
class A
sub dispose
end sub
end class
but dispose is never called.
Do I have to register it somehow?

The client code using your class will have to call the 'Dispose' method by
hand. I suggest to read the chapters about garbage collection and the
'IDisposable' interface in the documentation.
 
B

Boni

Dear Herfried,
Is it possible to delete object explicitely?
after findning a hard to find memory leak in my app, I really think that
managed programming is not more productive as unmanaged.
Futhermore in unmanaged code where I must delete all object expicitely there
are tools, which can check if I lost some objects and software will in any
case crash if you refer to deleted object. Where in managed code, I
accedentaly had somewhere one more pointer insead of weak referense to the
object, which was supposed to be dead. Fuhermore tools can only show you
number of objects. But if you have 300 new objects and 299 deleted (after
each new snapshoot 1 object is added) you have no chance to trace down which
objects is incorrect one, and why it was not deleted.
IMHO it is absolutely not easier to think which pointer you need (weak or
strong) and where you have all strong pointers than explicitely delete
object.
Is there some recommendadtions or comments about this?
Thanks a lot,
Boni

Where in managed code If I have a reference to the obje
 
M

Marina

Dispose will get called when the GC runs. It does not get called when the
object is out of scope.

You are supposed to call Dispose when you are done with the object. And when
the GC runs it will all it again, which is good if you forgot to call
Dispose yourself. But, since it may be a while before the GC runs, you
should always be disposing of objects that need it yourself and not rely on
the GC.
 

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