|
If the Dispose method is empty, you don't need it. Dispose is used when an object needs to clean up resources, like unmanaged file handles and such or managed objects that your object holds onto that don't clean themselves up, like a Graphics object. You put the code in the Dispose method that cleans these "loose ends" up before your object is destroyed.
Dave Kreskowiak
Microsoft MVP - Visual Basic
|
|
|
|
|
cleako wrote: I guess Im unsure what objects require a Dispose call
Any objects that hold resources of significant size, or resources that are unmanaged ( such as GDI handles )
cleako wrote: when do I actually call Dispose
That bit is easy - when you are about to lose access to an object, either because it's a local variable, or you're about to change the value ( or, you may set a value to null after calling Dispose on the basis that you don't need it anymore )
cleako wrote: when does the code do it for me?
Never. The code will call Finalise when it finally GCs the object. I don't think that Dispose is ever called by the framework. Really, your code should use finalise as well, and clean up anything there that was not Dispose'd of.
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
|
|
|
|
|
I used to call .Dispose on objects all the time then I asked a trainer about it and they made it sound like I was doing too much. So 2 more questions then.
What is a good resource other than that page I was pointed to read up on this stuff and when should I do something like .Dispose and when should I do something like Implements IDisposable etc...?
Cleako
|
|
|
|
|
As a general rule of thumb, you call .Dispose on object that implement it.
You implement IDisposable on your own object when they have resources that you need to specifically release when your done with them.
Dave Kreskowiak
Microsoft MVP - Visual Basic
|
|
|
|
|
rules to live by:
1) if an object has a dispose method, you should ALWAYS call it or bad things like memory leaks can occur
2) "try" to never rely on vb to clean up your objects for you.
3) add a dispose method to your classes and call it to safely release(close,dispose,nothing) all of your objects.
4) in routines, try to utilize they key word "using" which is supposed to guarantee (according to the help file) proper release of memory that your object was taking up.
|
|
|
|
|
So if I have a class for DataAccess and I call it just to make the connection and return a dataset, should I also have a .Dispose in that class that disposes of the Connection Object everytime and any data adapters etc...?
Cleako
|
|
|
|
|
You should be closing your database connection when you're done with the SQL query. NEVER hold open an SQL connection longer than is absolutely necessary. SQL server licenses are expensive. Holding a connection open for the life of your application hogs that license so no other application can use it when you're not using it. Remember, Windows and Servers are SHARED resources, so you have to write your code to work and play nice with others.
Dave Kreskowiak
Microsoft MVP - Visual Basic
|
|
|
|
|
Oh no, dont mark me as a bad coder! I wont be able to answer questions in good conscience again! I always close the connection in the Finally block after I use it. What I was asking as an example are what objects in that class are "Disposable"? Would I not only call a CN.Close but also a CN.Dispose? I think you can call a StreamReader.Close/Flush/Dispose.
So the path that got me to where I am right now is I havent used Implements IDisposable before. I also lost touch with my .Dispose self somewhere along the line and I guess I need to get back in touch with it.
The code I have "inherited" implements the IDisposable in a data access class. I dont know why but it does. The information I have read about Implements IDisposable makes it sound as if you only call that with COM objects, COM wrappers, unmanaged code calls, etc... Im trying to figure out the boundary of crazy "Disposing" and necessary "Disposing"
Thanks!!
Cleako
|
|
|
|
|
cleako wrote: Im trying to figure out the boundary of crazy "Disposing" and necessary "Disposing"
Add to your list that you should implement and call dispose on any class that holds on to an object that implements IDisposable as well. For example:
Class A implements IDisposable.
Class B holds a reference to an instance of class A as a field (member variable)
Class B needs to implement IDisposable because it holds an object of class A.
The Dispose method on class B calls the Dispose method on class A.
Also remember to implement a Finalizer method that calls Dispose on any class that implements it. This is to ensure that any object that is being garbage collected by the system and implements Dispose actually gets disposed. It is a final safe guard against accidentally holding on to things that should have been released. Once the object is garbage collected nothing can release the resource. This causes your application to leak memory, use up resources and eventually implode under its own weight.
|
|
|
|
|
When I minimize a child form in VB.Net it always goes to the bottom left corner. when I try to reference it from the parent and move it to the right some it just sits there, it seems like there is a property holding it. When minimized I can take my mouse and move the minimized form and it will then open and minimized to that location. Any help will be appriciated.
|
|
|
|
|
try ChildForm.SetBounds(300, 300, 0, 0)
|
|
|
|
|
that is only changing the form when it is restored, not the minimized title bar part.
|
|
|
|
|
i couldn't find a way to do what you asked about so by using this technique you can "simulate a minimize" by setting the size to 0,0 which will allow you to control the location.
in the form's resize event check if the windowstate = minimize and if it is, set the window state to normal then use the setbound to shrink it and to place it where you want. (just an idea)
|
|
|
|
|
Can anyone tell me how to use a command button to change the font of the writing on a form and another button to change the colour of the font on the form?
Using: Visual Basic.net 2002
Thank you!
-- modified at 12:48 Monday 8th January, 2007
|
|
|
|
|
1) your obviously are going to be dealing with the click events of both buttons
2) are you hard coding the font or do you want the "wizards" to pop-up and let them choose?
|
|
|
|
|
I would like the wizards to pop up and to let them choose as long i can change both font and colour
|
|
|
|
|
Ex:
'font
Me.Font = New System.Drawing.Font("GeoSlab703 Lt BT", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
'color
Me.ForeColor = System.Drawing.Color.Red
use the 'font dialog' control and 'color dialog' control from the toolbox to allow the user to choose...
|
|
|
|
|
|
help..plz help me how to read/eject USB flash drive serial number..plzz
tenkz a lot..its a big help..
gladz
|
|
|
|
|
you could try WMI to get the serial, i don't know how to eject it though...
|
|
|
|
|
Hi all,
I'm trying to write shoot date on the images from exif datas. I'm using Graphic.DrawString but it's not working. Weird thing is, when debugging it writes but when running not! My code is very simple;
Dim f As New Font(FontFamily.GenericSerif, 8.5F, FontStyle.Regular, GraphicsUnit.Point)
Dim graph As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(_img)
graph.DrawString(exifListBox.SelectedItem.ToString(), f, Brushes.Black, CSng(_img.Width) - 100.0F, CSng(_img.Height) - 30.0F)
Why it is not working when running?? Anybody has an idea?? Thanks...
Savas Cilve
|
|
|
|
|
Did you call Dispose() on the Graphics object (graph in your code) when you were done drawing with it?
Dave Kreskowiak
Microsoft MVP - Visual Basic
|
|
|
|
|
Yeah I did it after your response but nothing changed
|
|
|
|
|
You might want to include the code around this code. There's something else wrong we can't see because there's not enough code to show us what else is going on.
Dave Kreskowiak
Microsoft MVP - Visual Basic
|
|
|
|
|
There is nothing around this code. But I solved this issue with adding
pictureBox.Image = _img
just after the graph.DrawString(...)
I guess my description of the issue let people think wrong
Thanks so much for your interest..
Savas Cilve
|
|
|
|