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 / C++ / MFC
  4. memory leak

memory leak

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++performancehelp
12 Posts 5 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.
  • Z Offline
    Z Offline
    zon_cpp
    wrote on last edited by
    #1

    hi, how do i solve the "memory leak" problem in my VC++ project? is it calling SetProcessWorkingSetSize function?

    Zo.Naderi-Iran

    R N A 3 Replies Last reply
    0
    • Z zon_cpp

      hi, how do i solve the "memory leak" problem in my VC++ project? is it calling SetProcessWorkingSetSize function?

      Zo.Naderi-Iran

      R Offline
      R Offline
      rp_suman
      wrote on last edited by
      #2

      Where did you find memory leak in code? Or do you want to find if there is memory leak in your code?

      -- "Programming is an art that fights back!"

      1 Reply Last reply
      0
      • Z zon_cpp

        hi, how do i solve the "memory leak" problem in my VC++ project? is it calling SetProcessWorkingSetSize function?

        Zo.Naderi-Iran

        N Offline
        N Offline
        Niklas L
        wrote on last edited by
        #3

        1. Search for all occurrences of the new keyword. 2. Make sure they have a corresponding delete (or delete [] where applicable) 3. Search for alloc (will catch malloc, calloc, ...) 4. Make sure they have a corresponding free(). What else? Do you use COM components? GDI? Files? Databases? Registry? Try to run a subset of your application with a low code coverage to see if you still leak to narrow it down. (This might require code changes.) Increase the coverage in steps. A code review can also make wonders. Purchase a detection tool if your budget allows it.

        home

        Z 1 Reply Last reply
        0
        • Z zon_cpp

          hi, how do i solve the "memory leak" problem in my VC++ project? is it calling SetProcessWorkingSetSize function?

          Zo.Naderi-Iran

          A Offline
          A Offline
          Aescleal
          wrote on last edited by
          #4

          The best way to avoid memory leaks is not to cause them in the first place. So.... - avoid using new and delete, make your objects automatic and use "parameterise from the top" - if you're using arrays use std::vector instead - if you're using arrays of characters use std::string instead - use classes to manage resources generally - google RAII and have a read - don't use raw pointers unless you're interacting with legacy code Cheers, Ash PS: For the pedants - you can break all these rules, but have a reason to apart from "I didn't do that in C" or "I read it in a book by Herb Schildt"

          N L 2 Replies Last reply
          0
          • N Niklas L

            1. Search for all occurrences of the new keyword. 2. Make sure they have a corresponding delete (or delete [] where applicable) 3. Search for alloc (will catch malloc, calloc, ...) 4. Make sure they have a corresponding free(). What else? Do you use COM components? GDI? Files? Databases? Registry? Try to run a subset of your application with a low code coverage to see if you still leak to narrow it down. (This might require code changes.) Increase the coverage in steps. A code review can also make wonders. Purchase a detection tool if your budget allows it.

            home

            Z Offline
            Z Offline
            zon_cpp
            wrote on last edited by
            #5

            yes, i checked my project. all new keyword have delete and ... an infinite loop there is in my project. in this loop, i new and delete several pointer. is it useful , the calling SetProcessWorkingSetSize , in first or end of loop?

            Zo.Naderi-Iran

            N 1 Reply Last reply
            0
            • A Aescleal

              The best way to avoid memory leaks is not to cause them in the first place. So.... - avoid using new and delete, make your objects automatic and use "parameterise from the top" - if you're using arrays use std::vector instead - if you're using arrays of characters use std::string instead - use classes to manage resources generally - google RAII and have a read - don't use raw pointers unless you're interacting with legacy code Cheers, Ash PS: For the pedants - you can break all these rules, but have a reason to apart from "I didn't do that in C" or "I read it in a book by Herb Schildt"

              N Offline
              N Offline
              Niklas L
              wrote on last edited by
              #6

              Just out of curiosity, what did Herb Schildt do?

              home

              A 1 Reply Last reply
              0
              • Z zon_cpp

                yes, i checked my project. all new keyword have delete and ... an infinite loop there is in my project. in this loop, i new and delete several pointer. is it useful , the calling SetProcessWorkingSetSize , in first or end of loop?

                Zo.Naderi-Iran

                N Offline
                N Offline
                Niklas L
                wrote on last edited by
                #7

                You have a serious problem in your code, and SetProcessWorkingSetSize will unfortunatly not save you. Try and remove the new/delete in your loop if possible. Are you sure you don't have any condtional deletes? (whithin if/else block?) Does all branches clean up as expected? I didn't ask, but how is the leak showing? Can you spot it from the task manager while executing, or does visual studio inform you when you exit?

                home

                Z 1 Reply Last reply
                0
                • N Niklas L

                  Just out of curiosity, what did Herb Schildt do?

                  home

                  A Offline
                  A Offline
                  Aescleal
                  wrote on last edited by
                  #8

                  Mr. Schildt has written loads of rather bad books about C++. I've met plenty of poor programmers that have learnt C from one of his books thinking they were learning C++. And more annoyingly I bought a couple of his books incredibly cheap from a remaindered book store in 1998 and even though each was the price of a pint of beer at the time I felt exceedingly short changed after reading them. He's not unique BTW, there are plenty of appalling books out there that try to teach C++ but he's churned out far more than most of the others. What I find particularly distrurbing are the number of professional educators that don't seem to be able to tell what's a good teaching text and what isn't. Cheers, Ash PS: Just so I'm not being wholly negative, there are two very good books to learn C++ from: - "Accelerated C++" by Koenig and Moo - this is great for experienced programmers who want to learn C++ - "Programming -- Principles and Practice Using C++" by Stroustrup. This is better for people who have no exposure to programming but still gives them a good grounding.

                  modified on Wednesday, June 9, 2010 7:34 AM

                  1 Reply Last reply
                  0
                  • N Niklas L

                    You have a serious problem in your code, and SetProcessWorkingSetSize will unfortunatly not save you. Try and remove the new/delete in your loop if possible. Are you sure you don't have any condtional deletes? (whithin if/else block?) Does all branches clean up as expected? I didn't ask, but how is the leak showing? Can you spot it from the task manager while executing, or does visual studio inform you when you exit?

                    home

                    Z Offline
                    Z Offline
                    zon_cpp
                    wrote on last edited by
                    #9

                    the memory value, in task manager , is increscent.

                    Zo.Naderi-Iran

                    N 1 Reply Last reply
                    0
                    • Z zon_cpp

                      the memory value, in task manager , is increscent.

                      Zo.Naderi-Iran

                      N Offline
                      N Offline
                      Niklas L
                      wrote on last edited by
                      #10

                      Ok, so let's assume the leak really is from within the loop. Do you maybe have any exception / exception handling where the new/delete pair breaks? like:

                      try {
                      int *p = new int;
                      if (bNowAndThen)
                      throw "Exception";
                      delete p;
                      }
                      catch (const char* e)
                      {
                      }

                      If not, you need to start inspecting the functions you call from the loop and see if any of them is causing this. Do this by removing as much as you can, recompile and run. If it looks ok try adding a few calls until the error occurs again.

                      home

                      Z 1 Reply Last reply
                      0
                      • N Niklas L

                        Ok, so let's assume the leak really is from within the loop. Do you maybe have any exception / exception handling where the new/delete pair breaks? like:

                        try {
                        int *p = new int;
                        if (bNowAndThen)
                        throw "Exception";
                        delete p;
                        }
                        catch (const char* e)
                        {
                        }

                        If not, you need to start inspecting the functions you call from the loop and see if any of them is causing this. Do this by removing as much as you can, recompile and run. If it looks ok try adding a few calls until the error occurs again.

                        home

                        Z Offline
                        Z Offline
                        zon_cpp
                        wrote on last edited by
                        #11

                        ok, thank you, dear Niklas . i will do it. if i don't luck, i question you ,again. thanx

                        Zo.Naderi-Iran

                        1 Reply Last reply
                        0
                        • A Aescleal

                          The best way to avoid memory leaks is not to cause them in the first place. So.... - avoid using new and delete, make your objects automatic and use "parameterise from the top" - if you're using arrays use std::vector instead - if you're using arrays of characters use std::string instead - use classes to manage resources generally - google RAII and have a read - don't use raw pointers unless you're interacting with legacy code Cheers, Ash PS: For the pedants - you can break all these rules, but have a reason to apart from "I didn't do that in C" or "I read it in a book by Herb Schildt"

                          L Offline
                          L Offline
                          Lost User
                          wrote on last edited by
                          #12

                          I have to say your answer is one of the most informative and useful I have seen for a long time. I've learned two new patterns and got some suggestions for further reading.

                          It's time for a new signature.

                          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