problems with memory.
-
I have some problems with destroying object after using. 1. How can I dispose the form with many events registered and some global fields that has to be destroyed? Or it will destroy automatically? 2. If my class registers some events from other objects (use +=), do I have to unregister all the events (use -=)on its
Dispose()
method? I afraid that the objects firing events will stay still in heap if I dont do that. Thx for you answers. -
I have some problems with destroying object after using. 1. How can I dispose the form with many events registered and some global fields that has to be destroyed? Or it will destroy automatically? 2. If my class registers some events from other objects (use +=), do I have to unregister all the events (use -=)on its
Dispose()
method? I afraid that the objects firing events will stay still in heap if I dont do that. Thx for you answers.How do you know you have a problem? What are the symptoms? It is my understanding that all managed references are cleaned up automatically, however, if you look up discussions online, you will see everyone argues about whether or not to call dispose() explicitly. Personally, I don't clean up unless I know its necessary, ie calling Close() on a connection. Even this doesn't really close the reference, but just frees it back to the connection pool. Pualee
-
How do you know you have a problem? What are the symptoms? It is my understanding that all managed references are cleaned up automatically, however, if you look up discussions online, you will see everyone argues about whether or not to call dispose() explicitly. Personally, I don't clean up unless I know its necessary, ie calling Close() on a connection. Even this doesn't really close the reference, but just frees it back to the connection pool. Pualee
-
Pualee wrote:
about whether or not to call dispose() explicitly
Anyone saying you should not call Dispose explicitly is wrong wrong wrong!
For my personal education, would you care to elaborate. This is a topic I have really been trying to grasp lately. I come from a different environment where I would expect to clean up all my references, but in C# I thought this was automatic. As stated earlier, I see strong opinions on both sides. Is calling Dispose() just good practice, or is it actually necessary to prevent memory leaks and other problems? Thanks, Pualee
-
For my personal education, would you care to elaborate. This is a topic I have really been trying to grasp lately. I come from a different environment where I would expect to clean up all my references, but in C# I thought this was automatic. As stated earlier, I see strong opinions on both sides. Is calling Dispose() just good practice, or is it actually necessary to prevent memory leaks and other problems? Thanks, Pualee
-
It is just good practice. As long as you are in managed code only (ie no unsafe code) memory is 100% managed for you.
I disagree, it is not just good practice. Calling Dispose on instances of a class that has a Dispose() method is mandatory; the class may or may not hold unmanaged resources (encapsulation makes that knowledge irrelevant), and Dispose() would take care of them. So there are two basic rules: - if you design a class that holds unmanaged resources, you MUST provide a Dispose() method - if a class offers a Dispose() method, you MUST call it when (or as soon as) you are done with its instance(s). Hope this helps.
Luc Pattyn [My Articles]