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. Can this be the cause of a memory leak

Can this be the cause of a memory leak

Scheduled Pinned Locked Moved C / C++ / MFC
graphicsdockerperformancequestion
12 Posts 6 Posters 55 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.
  • C Offline
    C Offline
    Calin Negru
    wrote on last edited by
    #1

    for(int i=0;i<3;i++)
    {
    vector * Nodes = new vector ();
    thing * Athing = new thing();
    Nodes->push_back(Athing);
    //do stuff
    }

    do I need to delete the vector once the work is done or this is not required? How about the things stored inside the container, do I need to delete those too or calling clear() is enough.

    for(int i=0;i<3;i++)
    {
    vector * Nodes = new vector ();
    thing * Athing = new thing();
    Nodes->push_back(Athing);
    //do stuff
    Nodes->clear();
    delete Nodes;
    }

    V Mircea NeacsuM CPalliniC C 4 Replies Last reply
    0
    • C Calin Negru

      for(int i=0;i<3;i++)
      {
      vector * Nodes = new vector ();
      thing * Athing = new thing();
      Nodes->push_back(Athing);
      //do stuff
      }

      do I need to delete the vector once the work is done or this is not required? How about the things stored inside the container, do I need to delete those too or calling clear() is enough.

      for(int i=0;i<3;i++)
      {
      vector * Nodes = new vector ();
      thing * Athing = new thing();
      Nodes->push_back(Athing);
      //do stuff
      Nodes->clear();
      delete Nodes;
      }

      V Offline
      V Offline
      Victor Nijegorodov
      wrote on last edited by
      #2

      Calin Negru wrote:

      do I need to delete the vector once the work is done? for(int i=0;i<3;i++) { vector<thing*> * Nodes = new vector <thing*>(); thing * Athing = new thing(); Nodes->push_back(Athing); //do stuff Nodes->clear(); delete Nodes; }

      Yes, you do. Since you created it (vector) with new then you need to delete the vector once the work is done

      C 1 Reply Last reply
      0
      • C Calin Negru

        for(int i=0;i<3;i++)
        {
        vector * Nodes = new vector ();
        thing * Athing = new thing();
        Nodes->push_back(Athing);
        //do stuff
        }

        do I need to delete the vector once the work is done or this is not required? How about the things stored inside the container, do I need to delete those too or calling clear() is enough.

        for(int i=0;i<3;i++)
        {
        vector * Nodes = new vector ();
        thing * Athing = new thing();
        Nodes->push_back(Athing);
        //do stuff
        Nodes->clear();
        delete Nodes;
        }

        Mircea NeacsuM Offline
        Mircea NeacsuM Offline
        Mircea Neacsu
        wrote on last edited by
        #3

        On top of what Victor said, look into using std::unique_ptr instead of "naked" pointers. Something like this:

        auto Nodes = new std::vector< < std::unique_ptr >;
        Nodes->push_back( std::make_unique() );
        //do stuff
        delete []Nodes;

        Edit: It is a bit unusual to "new" vectors. Given they can grow dynamically, in most cases you would write something like:

        std::vector< >nodes;
        nodes.push_back (std::make_unique( /*constructor params go here*/ ) );
        /* node[0] is a (unique) pointer to Thing */

        // do more stuff

        When nodes goes out of scope all Things get deleted automatically.

        Mircea

        1 Reply Last reply
        0
        • C Calin Negru

          for(int i=0;i<3;i++)
          {
          vector * Nodes = new vector ();
          thing * Athing = new thing();
          Nodes->push_back(Athing);
          //do stuff
          }

          do I need to delete the vector once the work is done or this is not required? How about the things stored inside the container, do I need to delete those too or calling clear() is enough.

          for(int i=0;i<3;i++)
          {
          vector * Nodes = new vector ();
          thing * Athing = new thing();
          Nodes->push_back(Athing);
          //do stuff
          Nodes->clear();
          delete Nodes;
          }

          CPalliniC Offline
          CPalliniC Offline
          CPallini
          wrote on last edited by
          #4

          Yes, you have to explicitly delete the vectors you allocated using new. But that's not enough. Try running the following code

          #include
          #include

          using namespace std;

          class thing
          {
          public:
          thing(){cout << "thing ctor\n";}
          ~thing(){cout << "thing dtor\n";}
          };

          int main()
          {

          for(int i=0;i<3;i++)
          {
          vector * Nodes = new vector ();
          thing * Athing = new thing();
          Nodes->push_back(Athing);
          //do stuff
          Nodes->clear();
          delete Nodes;
          }
          }

          "In testa che avete, Signor di Ceprano?" -- Rigoletto

          In testa che avete, signor di Ceprano?

          C 1 Reply Last reply
          0
          • V Victor Nijegorodov

            Calin Negru wrote:

            do I need to delete the vector once the work is done? for(int i=0;i<3;i++) { vector<thing*> * Nodes = new vector <thing*>(); thing * Athing = new thing(); Nodes->push_back(Athing); //do stuff Nodes->clear(); delete Nodes; }

            Yes, you do. Since you created it (vector) with new then you need to delete the vector once the work is done

            C Offline
            C Offline
            Calin Negru
            wrote on last edited by
            #5

            What happens if I don’t delete the vector, will that cause a memory leak or it’s just allocated memory that is not used and takes extra space. The program doesn’t break with an error when I do source code version No 1 I have unexpected behavior somewhere else in my program and I was wondering if this could be the cause of it.

            K 1 Reply Last reply
            0
            • C Calin Negru

              What happens if I don’t delete the vector, will that cause a memory leak or it’s just allocated memory that is not used and takes extra space. The program doesn’t break with an error when I do source code version No 1 I have unexpected behavior somewhere else in my program and I was wondering if this could be the cause of it.

              K Offline
              K Offline
              k5054
              wrote on last edited by
              #6

              With a memory leak, the memory footprint of the running process increases over time. If the conditions that lead to the leak are encountered often enough, the process will eventually run out of available memory, usually causing an exception of some sort. Unless you're in a very specialized environment, the memory associated with the process gets released when it exits. That means that the overall memory on the system doesn't get incrementally consumed over time. So you really only run the risk of the one process running out of memory, not the system as a whole. I hope I've explained that clearly enough. Some system calls (and some user written functions!) use this to their advantage. On the first call they will allocate some memory, and then reuse it on successive calls. With no cleanup routine, they just rely on the program exit to do the memory release. That's why you might get notice of memory in use at exit when running a program under a memory diagnostic tool like Valgrind. When you trace back to where the memory was allocated, it might be inside something like fopen().

              "A little song, a little dance, a little seltzer down your pants" Chuckles the clown

              C 1 Reply Last reply
              0
              • K k5054

                With a memory leak, the memory footprint of the running process increases over time. If the conditions that lead to the leak are encountered often enough, the process will eventually run out of available memory, usually causing an exception of some sort. Unless you're in a very specialized environment, the memory associated with the process gets released when it exits. That means that the overall memory on the system doesn't get incrementally consumed over time. So you really only run the risk of the one process running out of memory, not the system as a whole. I hope I've explained that clearly enough. Some system calls (and some user written functions!) use this to their advantage. On the first call they will allocate some memory, and then reuse it on successive calls. With no cleanup routine, they just rely on the program exit to do the memory release. That's why you might get notice of memory in use at exit when running a program under a memory diagnostic tool like Valgrind. When you trace back to where the memory was allocated, it might be inside something like fopen().

                "A little song, a little dance, a little seltzer down your pants" Chuckles the clown

                C Offline
                C Offline
                Calin Negru
                wrote on last edited by
                #7

                Thanks k5054 I think I understand. What I described in the first example is a memory leak but probably is not causing problems elsewhere.

                K 1 Reply Last reply
                0
                • CPalliniC CPallini

                  Yes, you have to explicitly delete the vectors you allocated using new. But that's not enough. Try running the following code

                  #include
                  #include

                  using namespace std;

                  class thing
                  {
                  public:
                  thing(){cout << "thing ctor\n";}
                  ~thing(){cout << "thing dtor\n";}
                  };

                  int main()
                  {

                  for(int i=0;i<3;i++)
                  {
                  vector * Nodes = new vector ();
                  thing * Athing = new thing();
                  Nodes->push_back(Athing);
                  //do stuff
                  Nodes->clear();
                  delete Nodes;
                  }
                  }

                  "In testa che avete, Signor di Ceprano?" -- Rigoletto

                  C Offline
                  C Offline
                  Calin Negru
                  wrote on last edited by
                  #8

                  Thank you guys for your feedback. I think I understand what a memory leak is now.

                  CPalliniC 1 Reply Last reply
                  0
                  • C Calin Negru

                    Thank you guys for your feedback. I think I understand what a memory leak is now.

                    CPalliniC Offline
                    CPalliniC Offline
                    CPallini
                    wrote on last edited by
                    #9

                    You are welcome. As suggested, have a look at smart pointers, they could make your coding life easier.

                    "In testa che avete, Signor di Ceprano?" -- Rigoletto

                    In testa che avete, signor di Ceprano?

                    1 Reply Last reply
                    0
                    • C Calin Negru

                      Thanks k5054 I think I understand. What I described in the first example is a memory leak but probably is not causing problems elsewhere.

                      K Offline
                      K Offline
                      k5054
                      wrote on last edited by
                      #10

                      If by "not causing problems elsewhere" you mean it's not affecting other processes, that's mostly true. You can, of course, run into an Out Of Memory (OOM) situation, where all the RAM and swap is marked as "in use", and Bad Things start happening. Assuming you've got a 64 bit OS with lots of RAM and swap configured, (heck, even a 32 bit OS with good Virtual Memory), that's only likely to happen if you've got a *lot* of memory allocated. As a rule of thumb, you should clean up memory when it's no longer needed. Think of it like craftsmanship. A piece of Faberge jewelry shows attention to detail from both the back and the front. Freeing up unused memory is part of the attention to detail, just like closing files after use, for example.

                      "A little song, a little dance, a little seltzer down your pants" Chuckles the clown

                      C 1 Reply Last reply
                      0
                      • K k5054

                        If by "not causing problems elsewhere" you mean it's not affecting other processes, that's mostly true. You can, of course, run into an Out Of Memory (OOM) situation, where all the RAM and swap is marked as "in use", and Bad Things start happening. Assuming you've got a 64 bit OS with lots of RAM and swap configured, (heck, even a 32 bit OS with good Virtual Memory), that's only likely to happen if you've got a *lot* of memory allocated. As a rule of thumb, you should clean up memory when it's no longer needed. Think of it like craftsmanship. A piece of Faberge jewelry shows attention to detail from both the back and the front. Freeing up unused memory is part of the attention to detail, just like closing files after use, for example.

                        "A little song, a little dance, a little seltzer down your pants" Chuckles the clown

                        C Offline
                        C Offline
                        Calin Negru
                        wrote on last edited by
                        #11

                        Quote:

                        Go to ParentIf by "not causing problems elsewhere" you mean it's not affecting other processes, that's mostly true

                        I mean it`s not like when you write to an array out of bounds and while doing so you`re overwriting stuff that has nothing to do with the array.

                        1 Reply Last reply
                        0
                        • C Calin Negru

                          for(int i=0;i<3;i++)
                          {
                          vector * Nodes = new vector ();
                          thing * Athing = new thing();
                          Nodes->push_back(Athing);
                          //do stuff
                          }

                          do I need to delete the vector once the work is done or this is not required? How about the things stored inside the container, do I need to delete those too or calling clear() is enough.

                          for(int i=0;i<3;i++)
                          {
                          vector * Nodes = new vector ();
                          thing * Athing = new thing();
                          Nodes->push_back(Athing);
                          //do stuff
                          Nodes->clear();
                          delete Nodes;
                          }

                          C Offline
                          C Offline
                          charlieg
                          wrote on last edited by
                          #12

                          Ah memory leaks (and small buffer overruns). I would point out that your example is fairly obvious. If you have a long running task, and this code is in some sort of processing loop, you'll see it quickly. What will really bite you in the a$$ are the small leaks. A byte here, a byte there. I live in the embedded world where customers forget our equipment was installed under their production line 10 years ago. The engineer responsible either died, retired or moved on to another company. I'm not being morbid, I have stories I could tell you :) The group I work in is a decent group of smart people. Sadly, they never let us into the field to see how the product is actually used. The few times I've seen examples, they always shock me with "I didn't see that coming" sort of response. What amazes me is that if your customer never turns off your machine, why not set up an area where a test unit runs forever? I guess it falls under diminishing returns.

                          Charlie Gilley “They who can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety.” BF, 1759 Has never been more appropriate.

                          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