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. The Lounge
  3. Automated way of checking if all instances of a type have been properly disposed of [modified]

Automated way of checking if all instances of a type have been properly disposed of [modified]

Scheduled Pinned Locked Moved The Lounge
questioncsharpasp-netvisual-studiocom
27 Posts 13 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.
  • V Vikram A Punathambekar

    My app is creating all these instances, so my app should be disposing of them. I will take a look at SafeHandle tomorrow, thanks.

    Cheers, Vıkram.


    I don't suffer from insanity, I enjoy every moment of it.

    L Offline
    L Offline
    leppie
    wrote on last edited by
    #4

    Vikram A Punathambekar wrote:

    My app is creating all these instances

    That's good, at least you can keep track. Use a list to store them.

    xacc.ide - now with TabsToSpaces support
    IronScheme - 1.0 beta 1 - out now!
    ((lambda (x) `((lambda (x) ,x) ',x)) '`((lambda (x) ,x) ',x))

    V 1 Reply Last reply
    0
    • L leppie

      Vikram A Punathambekar wrote:

      My app is creating all these instances

      That's good, at least you can keep track. Use a list to store them.

      xacc.ide - now with TabsToSpaces support
      IronScheme - 1.0 beta 1 - out now!
      ((lambda (x) `((lambda (x) ,x) ',x)) '`((lambda (x) ,x) ',x))

      V Offline
      V Offline
      Vikram A Punathambekar
      wrote on last edited by
      #5

      leppie wrote:

      Use a list to store them.

      You are joking, right? Dozens of .CS files; instances are local variables, static members, instance members, you name it. The best thing to do, IMO, is go over them manually, make the changes, and have somebody else review them.

      Cheers, Vıkram.


      I don't suffer from insanity, I enjoy every moment of it.

      1 Reply Last reply
      0
      • V Vikram A Punathambekar

        No, this isn't a programming question, so please read properly before lynch-voting :) And if you decide to lynch vote this after all, an explanation would be appreciated. I discovered today that a core component of my app has a lot of instances of a custom type, HDElement*, that are not disposed of. This is causing a HUGE memory leak and it has fallen on my shoulders to remove this leak. Does anybody know of a way to see if all instances have Dispose() called on them? The thought of creating a custom FxCop rule did occur to me, but I've never done that before, and don't know if this is even possible. At the moment, I am going through every source file and adding try...finally blocks that call Dispose() on all HDElement instances :(( I was wondering if there's a better way of doing this? I am using VS 2008 but am targeting .NET 2.0. * HDElement is part of a library provided by another team in another continent in the same company, and holds lots of unmanaged resources that the GC cannot take care of.

        Cheers, Vıkram.


        I don't suffer from insanity, I enjoy every moment of it.

        modified on Monday, January 19, 2009 11:55 AM to include framework version.

        R Offline
        R Offline
        Rob Philpott
        wrote on last edited by
        #6

        No, I can't see it. How is this anything other than a programming question?

        Regards, Rob Philpott.

        L V 2 Replies Last reply
        0
        • R Rob Philpott

          No, I can't see it. How is this anything other than a programming question?

          Regards, Rob Philpott.

          L Offline
          L Offline
          leppie
          wrote on last edited by
          #7

          It's borderline. I gave him the benefit of the doubt, that he was looking for some sort of plugin library :)

          xacc.ide - now with TabsToSpaces support
          IronScheme - 1.0 beta 1 - out now!
          ((lambda (x) `((lambda (x) ,x) ',x)) '`((lambda (x) ,x) ',x))

          1 Reply Last reply
          0
          • V Vikram A Punathambekar

            No, this isn't a programming question, so please read properly before lynch-voting :) And if you decide to lynch vote this after all, an explanation would be appreciated. I discovered today that a core component of my app has a lot of instances of a custom type, HDElement*, that are not disposed of. This is causing a HUGE memory leak and it has fallen on my shoulders to remove this leak. Does anybody know of a way to see if all instances have Dispose() called on them? The thought of creating a custom FxCop rule did occur to me, but I've never done that before, and don't know if this is even possible. At the moment, I am going through every source file and adding try...finally blocks that call Dispose() on all HDElement instances :(( I was wondering if there's a better way of doing this? I am using VS 2008 but am targeting .NET 2.0. * HDElement is part of a library provided by another team in another continent in the same company, and holds lots of unmanaged resources that the GC cannot take care of.

            Cheers, Vıkram.


            I don't suffer from insanity, I enjoy every moment of it.

            modified on Monday, January 19, 2009 11:55 AM to include framework version.

            G Offline
            G Offline
            Gary Wheeler
            wrote on last edited by
            #8

            Vikram, there's a "using" statement in C# that does the try...finally thing in a nice syntax that guarantees that Dispose() gets called. I'd have a link to the MSDN, but my access to MSDN seems to be oddly broken at the moment.

            Software Zen: delete this;

            R V 2 Replies Last reply
            0
            • V Vikram A Punathambekar

              No, this isn't a programming question, so please read properly before lynch-voting :) And if you decide to lynch vote this after all, an explanation would be appreciated. I discovered today that a core component of my app has a lot of instances of a custom type, HDElement*, that are not disposed of. This is causing a HUGE memory leak and it has fallen on my shoulders to remove this leak. Does anybody know of a way to see if all instances have Dispose() called on them? The thought of creating a custom FxCop rule did occur to me, but I've never done that before, and don't know if this is even possible. At the moment, I am going through every source file and adding try...finally blocks that call Dispose() on all HDElement instances :(( I was wondering if there's a better way of doing this? I am using VS 2008 but am targeting .NET 2.0. * HDElement is part of a library provided by another team in another continent in the same company, and holds lots of unmanaged resources that the GC cannot take care of.

              Cheers, Vıkram.


              I don't suffer from insanity, I enjoy every moment of it.

              modified on Monday, January 19, 2009 11:55 AM to include framework version.

              R Offline
              R Offline
              Rama Krishna Vavilala
              wrote on last edited by
              #9

              Is there a finalizer in the HDElement class and that is the Dispose pattern is properly implemented. If yes then where are you experiencing memory leaks? and why is that such a major issue are the users experiencing some issues? Now it may be that GC may not be reclaiming HDElement at a time when you expect if the bulk of the memory used by HDElement is in the unmanaged resources. If you can modify the source code of HDElement, I think you should consider AddMemoryPressure method.

              V 1 Reply Last reply
              0
              • G Gary Wheeler

                Vikram, there's a "using" statement in C# that does the try...finally thing in a nice syntax that guarantees that Dispose() gets called. I'd have a link to the MSDN, but my access to MSDN seems to be oddly broken at the moment.

                Software Zen: delete this;

                R Offline
                R Offline
                R Giskard Reventlov
                wrote on last edited by
                #10

                You do need to implement IDisposable and ensure that the Dispose method actually does something! I think there may be a couple of articles here that give a good overview (you can find them). If not I can send you some code if you like.

                me, me, me

                G 1 Reply Last reply
                0
                • R R Giskard Reventlov

                  You do need to implement IDisposable and ensure that the Dispose method actually does something! I think there may be a couple of articles here that give a good overview (you can find them). If not I can send you some code if you like.

                  me, me, me

                  G Offline
                  G Offline
                  Gary Wheeler
                  wrote on last edited by
                  #11

                  It sounds like Vikram already has a Dispose() method in place. I was merely trying to offer him a more succinct way of making sure it was called.

                  Software Zen: delete this;

                  R 1 Reply Last reply
                  0
                  • G Gary Wheeler

                    It sounds like Vikram already has a Dispose() method in place. I was merely trying to offer him a more succinct way of making sure it was called.

                    Software Zen: delete this;

                    R Offline
                    R Offline
                    R Giskard Reventlov
                    wrote on last edited by
                    #12

                    And a darned good way as well.

                    me, me, me

                    1 Reply Last reply
                    0
                    • V Vikram A Punathambekar

                      No, this isn't a programming question, so please read properly before lynch-voting :) And if you decide to lynch vote this after all, an explanation would be appreciated. I discovered today that a core component of my app has a lot of instances of a custom type, HDElement*, that are not disposed of. This is causing a HUGE memory leak and it has fallen on my shoulders to remove this leak. Does anybody know of a way to see if all instances have Dispose() called on them? The thought of creating a custom FxCop rule did occur to me, but I've never done that before, and don't know if this is even possible. At the moment, I am going through every source file and adding try...finally blocks that call Dispose() on all HDElement instances :(( I was wondering if there's a better way of doing this? I am using VS 2008 but am targeting .NET 2.0. * HDElement is part of a library provided by another team in another continent in the same company, and holds lots of unmanaged resources that the GC cannot take care of.

                      Cheers, Vıkram.


                      I don't suffer from insanity, I enjoy every moment of it.

                      modified on Monday, January 19, 2009 11:55 AM to include framework version.

                      M Offline
                      M Offline
                      Member 96
                      wrote on last edited by
                      #13

                      If I wasn't boycotting the voting process I'd lynch vote you a 1 for asking what is clearly a programming question in the lounge.


                      "It's so simple to be wise. Just think of something stupid to say and then don't say it." -Sam Levenson

                      1 Reply Last reply
                      0
                      • V Vikram A Punathambekar

                        No, this isn't a programming question, so please read properly before lynch-voting :) And if you decide to lynch vote this after all, an explanation would be appreciated. I discovered today that a core component of my app has a lot of instances of a custom type, HDElement*, that are not disposed of. This is causing a HUGE memory leak and it has fallen on my shoulders to remove this leak. Does anybody know of a way to see if all instances have Dispose() called on them? The thought of creating a custom FxCop rule did occur to me, but I've never done that before, and don't know if this is even possible. At the moment, I am going through every source file and adding try...finally blocks that call Dispose() on all HDElement instances :(( I was wondering if there's a better way of doing this? I am using VS 2008 but am targeting .NET 2.0. * HDElement is part of a library provided by another team in another continent in the same company, and holds lots of unmanaged resources that the GC cannot take care of.

                        Cheers, Vıkram.


                        I don't suffer from insanity, I enjoy every moment of it.

                        modified on Monday, January 19, 2009 11:55 AM to include framework version.

                        C Offline
                        C Offline
                        Chris Maunder
                        wrote on last edited by
                        #14

                        Mate, I reckon this falls smakc-bang in the middle of the big circle marked 'Programming questions' :D

                        cheers, Chris Maunder

                        CodeProject.com : C++ MVP

                        V 1 Reply Last reply
                        0
                        • V Vikram A Punathambekar

                          No, this isn't a programming question, so please read properly before lynch-voting :) And if you decide to lynch vote this after all, an explanation would be appreciated. I discovered today that a core component of my app has a lot of instances of a custom type, HDElement*, that are not disposed of. This is causing a HUGE memory leak and it has fallen on my shoulders to remove this leak. Does anybody know of a way to see if all instances have Dispose() called on them? The thought of creating a custom FxCop rule did occur to me, but I've never done that before, and don't know if this is even possible. At the moment, I am going through every source file and adding try...finally blocks that call Dispose() on all HDElement instances :(( I was wondering if there's a better way of doing this? I am using VS 2008 but am targeting .NET 2.0. * HDElement is part of a library provided by another team in another continent in the same company, and holds lots of unmanaged resources that the GC cannot take care of.

                          Cheers, Vıkram.


                          I don't suffer from insanity, I enjoy every moment of it.

                          modified on Monday, January 19, 2009 11:55 AM to include framework version.

                          E Offline
                          E Offline
                          Ennis Ray Lynch Jr
                          wrote on last edited by
                          #15

                          I almost never 1 vote people.

                          Need software developed? Offering C# development all over the United States, ERL GLOBAL, Inc is the only call you will have to make.
                          If you don't ask questions the answers won't stand in your way.
                          Most of this sig is for Google, not ego.

                          P 1 Reply Last reply
                          0
                          • C Chris Maunder

                            Mate, I reckon this falls smakc-bang in the middle of the big circle marked 'Programming questions' :D

                            cheers, Chris Maunder

                            CodeProject.com : C++ MVP

                            V Offline
                            V Offline
                            Vikram A Punathambekar
                            wrote on last edited by
                            #16

                            To be honest, I wasn't asking, "How can I avoid this memory leak?" I expected an answer like "Tool X can do this for you", or "You're buggered, you'll have to review the whole code and do it by hand". If this still qualifies as a programming question, my unqualified apologies.

                            Cheers, Vıkram.


                            I don't suffer from insanity, I enjoy every moment of it.

                            1 Reply Last reply
                            0
                            • G Gary Wheeler

                              Vikram, there's a "using" statement in C# that does the try...finally thing in a nice syntax that guarantees that Dispose() gets called. I'd have a link to the MSDN, but my access to MSDN seems to be oddly broken at the moment.

                              Software Zen: delete this;

                              V Offline
                              V Offline
                              Vikram A Punathambekar
                              wrote on last edited by
                              #17

                              Thanks for the reply, Gary, but I wasn't looking for coding tips to handle the memory leak. I know perfectly well what needs to be done, I was wondering if there is a tool that can do this for me or I had to review the entire code.

                              Cheers, Vıkram.


                              I don't suffer from insanity, I enjoy every moment of it.

                              A 1 Reply Last reply
                              0
                              • R Rama Krishna Vavilala

                                Is there a finalizer in the HDElement class and that is the Dispose pattern is properly implemented. If yes then where are you experiencing memory leaks? and why is that such a major issue are the users experiencing some issues? Now it may be that GC may not be reclaiming HDElement at a time when you expect if the bulk of the memory used by HDElement is in the unmanaged resources. If you can modify the source code of HDElement, I think you should consider AddMemoryPressure method.

                                V Offline
                                V Offline
                                Vikram A Punathambekar
                                wrote on last edited by
                                #18

                                Thanks for the tip, and thanks for not flaming :) HDElement holds unmanaged resources, so the GC won't do. Modifying its source is out of question, so I will have to review all of my app's code and call Dispose manually.

                                Cheers, Vıkram.


                                I don't suffer from insanity, I enjoy every moment of it.

                                1 Reply Last reply
                                0
                                • R Rob Philpott

                                  No, I can't see it. How is this anything other than a programming question?

                                  Regards, Rob Philpott.

                                  V Offline
                                  V Offline
                                  Vikram A Punathambekar
                                  wrote on last edited by
                                  #19

                                  Thanks for the comment, Rob, but I didn't know asking for automated tools qualified as a programming question. Still, I have created a separate apology thread.

                                  Cheers, Vıkram.


                                  I don't suffer from insanity, I enjoy every moment of it.

                                  1 Reply Last reply
                                  0
                                  • V Vikram A Punathambekar

                                    No, this isn't a programming question, so please read properly before lynch-voting :) And if you decide to lynch vote this after all, an explanation would be appreciated. I discovered today that a core component of my app has a lot of instances of a custom type, HDElement*, that are not disposed of. This is causing a HUGE memory leak and it has fallen on my shoulders to remove this leak. Does anybody know of a way to see if all instances have Dispose() called on them? The thought of creating a custom FxCop rule did occur to me, but I've never done that before, and don't know if this is even possible. At the moment, I am going through every source file and adding try...finally blocks that call Dispose() on all HDElement instances :(( I was wondering if there's a better way of doing this? I am using VS 2008 but am targeting .NET 2.0. * HDElement is part of a library provided by another team in another continent in the same company, and holds lots of unmanaged resources that the GC cannot take care of.

                                    Cheers, Vıkram.


                                    I don't suffer from insanity, I enjoy every moment of it.

                                    modified on Monday, January 19, 2009 11:55 AM to include framework version.

                                    S Offline
                                    S Offline
                                    S Senthil Kumar
                                    wrote on last edited by
                                    #20

                                    I ran into a similar problem a while back, but we owned the code for the type (you could always disassemble/assemble). My solution was to store the stack trace of the constructor of every instance of that type as a member in that type, and then in the finalizer, log the stack trace. I also ran a timer in the background that periodically called GC.Collect(); GC.WaitForPendingFinalizers(); GC.Collect();. That way, the finalizer would run for all instances that did not get disposed, and I would be able to see where those instances got created. I would then run the application and try to exercise all paths in the code that deal with that type. I would then go through the log and fix code that was not disposing instances. The trick is to make sure that all code paths that involve that type are exercised, and that's what makes this non-deterministic. A tool like NCover would probably help track code coverage. You just gave me an idea - it would be nice to have a tool automate the first part (track instances and report where they are created in code). Now I know what I'll be doing over the Republic Day weekend :) Hope this helps.

                                    Regards Senthil [MVP - Visual C#] _____________________________ My Home Page |My Blog | My Articles | My Flickr | WinMacro

                                    V 1 Reply Last reply
                                    0
                                    • S S Senthil Kumar

                                      I ran into a similar problem a while back, but we owned the code for the type (you could always disassemble/assemble). My solution was to store the stack trace of the constructor of every instance of that type as a member in that type, and then in the finalizer, log the stack trace. I also ran a timer in the background that periodically called GC.Collect(); GC.WaitForPendingFinalizers(); GC.Collect();. That way, the finalizer would run for all instances that did not get disposed, and I would be able to see where those instances got created. I would then run the application and try to exercise all paths in the code that deal with that type. I would then go through the log and fix code that was not disposing instances. The trick is to make sure that all code paths that involve that type are exercised, and that's what makes this non-deterministic. A tool like NCover would probably help track code coverage. You just gave me an idea - it would be nice to have a tool automate the first part (track instances and report where they are created in code). Now I know what I'll be doing over the Republic Day weekend :) Hope this helps.

                                      Regards Senthil [MVP - Visual C#] _____________________________ My Home Page |My Blog | My Articles | My Flickr | WinMacro

                                      V Offline
                                      V Offline
                                      Vikram A Punathambekar
                                      wrote on last edited by
                                      #21

                                      Thanks for your reply, Senthil. Making any changes to the type itself is out of question. I ploughed through the code today, adding Dispose() calls, and will see tomorrow if it has had any effect. If not, I'll go with your idea of logging the stack trace by inheriting the class (I hope they didn't seal it :doh: ) and replacing all HDElement instances with my subclass. Send me the link when your article is ready ;)

                                      Cheers, Vıkram.


                                      I don't suffer from insanity, I enjoy every moment of it.

                                      S N 2 Replies Last reply
                                      0
                                      • E Ennis Ray Lynch Jr

                                        I almost never 1 vote people.

                                        Need software developed? Offering C# development all over the United States, ERL GLOBAL, Inc is the only call you will have to make.
                                        If you don't ask questions the answers won't stand in your way.
                                        Most of this sig is for Google, not ego.

                                        P Offline
                                        P Offline
                                        PIEBALDconsult
                                        wrote on last edited by
                                        #22

                                        Now there's a straight line: "... only their darn stupid questions." "... monkeys on the other hand..." :-D

                                        1 Reply Last reply
                                        0
                                        • V Vikram A Punathambekar

                                          Thanks for the reply, Gary, but I wasn't looking for coding tips to handle the memory leak. I know perfectly well what needs to be done, I was wondering if there is a tool that can do this for me or I had to review the entire code.

                                          Cheers, Vıkram.


                                          I don't suffer from insanity, I enjoy every moment of it.

                                          A Offline
                                          A Offline
                                          Adar Wesley
                                          wrote on last edited by
                                          #23

                                          To find memory leaks in a program C# or C++ at runtime use Purify Or DevPartner For static code analysis, I am sure it is possible to create an appropriate FxCop rule. --- Adar Wesley

                                          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