Need to dispose??

J

Just Me

I have many subs that creates a graphics object:
Dim lG As Graphics

After use I dispose it.

usually just before leaving the Sub

Is that necessary or does leaving the Sub cause it to be disposed soon if
needed.



Same question about a Bitmap object.
 
H

Herfried K. Wagner [MVP]

Just Me said:
I have many subs that creates a graphics object:
Dim lG As Graphics

After use I dispose it.

usually just before leaving the Sub

Is that necessary or does leaving the Sub cause it to be disposed soon if
needed.

Yes, it's necessary/recommended, alghough it's not mandatory. If you don't
call 'Dispose', it will be called when the object is destroyed by the GC.
However, it's good practice to call 'Dispose' by hand to release unmanaged
resources occupied by the object.
 
J

Just Me

This is what I was told about a different type of object.

'Calling from .NET doesn't produce a IRichEditOle COM object but a RCW
'(Runtime Callabe Wrapper), which is a .NET object that wraps the COM object
'and behaves like any other .NET object. The pointer will be released when
'the object is not used anymore and the runtime performs garbage collection.
'To release the COM object without waiting for the garbage collection,
'use the ReleaseComObject method of the
System.Runtime.InteropServices.Marshal class
'End Sub

After I received the above I removed all the releases.
Would you recommend disposing in the above situation also.

Thanks again for the help
 
C

Chris Dunaway

I think the general rule to follow is this:

If you created it, dispose it. If you didn't create it, don't dispose
unless specifically told to.

As for letting the GC handle it, you never know when the GC will run.
It is best to dispose of resources as soon as you know you no longer
need them.

As for COM wrappers, I don't know if they typicaly have a dispose
method, but I always call ReleaseComObject on them.
 

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