Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. problems with memory.

problems with memory.

Scheduled Pinned Locked Moved C#
questionperformance
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Duong Tien Nam
    wrote on last edited by
    #1

    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.

    P 1 Reply Last reply
    0
    • D Duong Tien Nam

      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.

      P Offline
      P Offline
      Pualee
      wrote on last edited by
      #2

      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

      J 1 Reply Last reply
      0
      • P 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

        J Offline
        J Offline
        J4amieC
        wrote on last edited by
        #3

        Pualee wrote:

        about whether or not to call dispose() explicitly

        Anyone saying you should not call Dispose explicitly is wrong wrong wrong!

        --- How to get answers to your questions[^]

        P 1 Reply Last reply
        0
        • J J4amieC

          Pualee wrote:

          about whether or not to call dispose() explicitly

          Anyone saying you should not call Dispose explicitly is wrong wrong wrong!

          --- How to get answers to your questions[^]

          P Offline
          P Offline
          Pualee
          wrote on last edited by
          #4

          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

          J 1 Reply Last reply
          0
          • P 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

            J Offline
            J Offline
            J4amieC
            wrote on last edited by
            #5

            It is just good practice. As long as you are in managed code only (ie no unsafe code) memory is 100% managed for you.

            --- How to get answers to your questions[^]

            L 1 Reply Last reply
            0
            • J J4amieC

              It is just good practice. As long as you are in managed code only (ie no unsafe code) memory is 100% managed for you.

              --- How to get answers to your questions[^]

              L Offline
              L Offline
              Luc Pattyn
              wrote on last edited by
              #6

              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]

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups