Answered by:
Dispose of Application Settings Object

Question
-
Hello,
I have a custom settings file that I designed using the designer. In my form, I have a function that creates a new instance of this settings by using a dim statement. Then I add this new settings object to a hashtable. After I exit this function, I no longer have access to the settings object without using the hashtable to retrieve it. If I want to remove the object from the hashtable, I can do a simple remove but the settings object still exists somewhere in memory. How can I dispose of this object as well since it inherits from ApplicationSettingsBase and does not have a built in Dispose() function?
Thanks
JonMonday, December 28, 2009 3:09 PM
Answers
-
If the settings object no longer has any references from your code, it will be removed by the garbage collector - there is normally no need to dispose of it. If you have added something to the object that prevents it from being garbage collected, then in your inherited object you need to implement a Dispose method and do whatever cleanup is required. Then call this Dispose method just before you remove the reference to the object from the hash table.
- Proposed as answer by Chao KuoModerator Tuesday, December 29, 2009 9:01 AM
- Marked as answer by jonlee107 Tuesday, December 29, 2009 1:50 PM
Monday, December 28, 2009 11:51 PM -
An object which has no reference anymore itself and is not referenced by an other object, will be released by the GC as soon as it recognizes that both type of references don't exist anymore.
An object looses in a method its own reference as soon as it goes out of scope but then it is not released as long as you have not removed like you wrote from the hashtable.
Then comes the difference between modern computing and the way it was done when there was only 64K in a computer.
Releasing objects is something that should not be done, when there is no need for this, so Microsoft created the Garbage Collector which releases memory as soon as that is needed. The GC as it is normally called will be improved in Net version 4.0.
The dispose method of the component class (from which all forms and asp controls derive) has a slight influence on the GC as it takes it as a signal that it maybe can be released and becomes in another cycle of that, but that is in fact all it does.
The dispose method from compenents is only meant to release unmanaged resources like pens and win32 objects. If you have not used components, then the change is low that your object contains those unmanaged resources.
On Internet there are some fools who think that an object can dispose itself by using the dispose method, but that is impossible. The dispose method from components has nothing to do with the finalize method.
Success
Cor- Marked as answer by jonlee107 Tuesday, December 29, 2009 1:50 PM
Tuesday, December 29, 2009 9:58 AM -
I don't think you can know for sure that something will be garbage collected, but I believe that databindings, like handlers, are discarded when the object goes out of scope. Certainly, I have never worried about taking them down. The only debug options I am aware of is locals or your selected watch variables, but since the setting you are referring to is now out of scope you wouldn't have any label by which to referrence it or to identify it if it could be displayed.
If you need to be certain then create a Dispose method for your derived class and undo whatever you can find to undo (including the databindings) in that method, and call it before deleting the hash table entry. It won't do any harm.- Marked as answer by jonlee107 Wednesday, December 30, 2009 1:44 PM
Tuesday, December 29, 2009 9:15 PM
All replies
-
If the settings object no longer has any references from your code, it will be removed by the garbage collector - there is normally no need to dispose of it. If you have added something to the object that prevents it from being garbage collected, then in your inherited object you need to implement a Dispose method and do whatever cleanup is required. Then call this Dispose method just before you remove the reference to the object from the hash table.
- Proposed as answer by Chao KuoModerator Tuesday, December 29, 2009 9:01 AM
- Marked as answer by jonlee107 Tuesday, December 29, 2009 1:50 PM
Monday, December 28, 2009 11:51 PM -
An object which has no reference anymore itself and is not referenced by an other object, will be released by the GC as soon as it recognizes that both type of references don't exist anymore.
An object looses in a method its own reference as soon as it goes out of scope but then it is not released as long as you have not removed like you wrote from the hashtable.
Then comes the difference between modern computing and the way it was done when there was only 64K in a computer.
Releasing objects is something that should not be done, when there is no need for this, so Microsoft created the Garbage Collector which releases memory as soon as that is needed. The GC as it is normally called will be improved in Net version 4.0.
The dispose method of the component class (from which all forms and asp controls derive) has a slight influence on the GC as it takes it as a signal that it maybe can be released and becomes in another cycle of that, but that is in fact all it does.
The dispose method from compenents is only meant to release unmanaged resources like pens and win32 objects. If you have not used components, then the change is low that your object contains those unmanaged resources.
On Internet there are some fools who think that an object can dispose itself by using the dispose method, but that is impossible. The dispose method from components has nothing to do with the finalize method.
Success
Cor- Marked as answer by jonlee107 Tuesday, December 29, 2009 1:50 PM
Tuesday, December 29, 2009 9:58 AM -
Hey Acamar,
Yeah I knew that much about the garbage collector. However, how do I know for sure if it is being disposed of properly or not. Like I said, it is an Application Settings object which may or may not dispose properly especially since I bound the settings to a set of controls. I am not sure if I have to unbind those settings before the GC will clean up the unused settings object.
I have not figured this out yet but is it possible to view a list of all the current variables in memory after running the application or do I have to set breakpoints and view the locals? I am using Visual Studio 2008.
Thanks
JonTuesday, December 29, 2009 2:11 PM -
I don't think you can know for sure that something will be garbage collected, but I believe that databindings, like handlers, are discarded when the object goes out of scope. Certainly, I have never worried about taking them down. The only debug options I am aware of is locals or your selected watch variables, but since the setting you are referring to is now out of scope you wouldn't have any label by which to referrence it or to identify it if it could be displayed.
If you need to be certain then create a Dispose method for your derived class and undo whatever you can find to undo (including the databindings) in that method, and call it before deleting the hash table entry. It won't do any harm.- Marked as answer by jonlee107 Wednesday, December 30, 2009 1:44 PM
Tuesday, December 29, 2009 9:15 PM -
Thanks Acamar,
I kind of figured that was the case. Thanks for the thorough explanation.
JonWednesday, December 30, 2009 1:45 PM