|
-
Feb 21st, 2007, 04:39 AM
#1
Thread Starter
Fanatic Member
[RESOLVED] [2005] Using .Dispose on a Bitmap class
My class has a .ToBitmap method to make it easy for a control to visualize its content. Because the data has to be down-sampled before it can be converted to a bitmap, the new bitmap is not attached to the data, rather I for...next through it to set the bits in an array that the bitmap is attached to.
Long story short, the .ToBitmap method returns a new bitmap object each time.
I understand that it is common courtesy for the garbage collector to call the .Dispose method of the bitmap once I'm done with it, but what if I don't?
Does the garbage collector call .Dispose for me if I forget? Will I end up with a memory leak until the GC runs?
-
Feb 21st, 2007, 05:41 AM
#2
Re: [2005] Using .Dispose on a Bitmap class
Disposing an object has no direct effect on memory. If you want an object to be eligible for garbage collection you must remove all references to it. That means that all variables that refer to the object must either lose scope or be explicitly set to Nothing. That's the only way to make an object eligibale to have its memory reclaimed.
Disposing is something else. When you dispose an object you release unmanaged resources, e.g. file handles or image handles. Disposing an object releases those resources back to the system. If an object doesn't hold any unmanaged resources, either directly or indirectly, then it doesn't need to be disposed. That's why not all types have a Dispose method.
If you remove all references to a disposable object without disposing it then you are tying up its unmanaged resources until the garbage collector gets around to cleaning it up. Not only that, instead of being able to clean up the object immediately the garbage collector must invoke its Finalize method and wait for the next pass to reclaim the memory. The Finalize method simply calls the Dispose method and is the insurance that all objects that require it do get disposed.
For the vast majority of situations the rules very, very simple:
1. Always dispose an object that supports it when you are finished with it.
2. If a reference type variable will not or might not lose scope for some time after you're finished with it set it to Nothing.
That's all there is to it. The Framework takes care of the rest.
-
Feb 21st, 2007, 04:14 PM
#3
Thread Starter
Fanatic Member
Re: [2005] Using .Dispose on a Bitmap class
Ok... well then...
That complexifies my code quite a bit.
I'm creating an array of buttons (think like folder contents, but with a custom control instead) and each button will have the return value of the .ToImage method as its image.
What I'll do is create an array of the bitmaps when the folder is set and set the controls. When the user changes folders, I'll dispose the bitmaps from the array and reset for the next folder. I might just use an ImageList component for this instead as it's designed for this purpose.
(BTW, if you're planning on mentioning anything about the ListView control, I'm using a custom designed control in its place. 'Folder' is the best way to portray the idea, but it has nothing to do with the file system in this case.)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|