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. RAII / RIIA best-practices question about std::vector

RAII / RIIA best-practices question about std::vector

Scheduled Pinned Locked Moved C / C++ / MFC
questionlearninggraphicsdockerlounge
6 Posts 2 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.
  • M Offline
    M Offline
    Mike the Red
    wrote on last edited by
    #1

    So one of my (many) recent questions ellicited a link to Resource Acquisition Is Initialization[^]. The wikipedia entry made sense and it seemed like, in general, this structure would be a good idea. So now I'm using std::vector for the first time since my RAII introduction, and I recall that vector<> leaks unless I call vector<>.clear() when I'm done with the vector. Does that mean, considering RAII, that whenever I want to use std::vector the best-practice method would be to write a specialized container class that calls .clear() in the destructor and exposes all the std::vector methods? Now that I think of it, the container could be written as a template class.... but I'm going to let the question stand, since I'm just learning about RAII... Is this an exception to the rule, or would a container class be best? A thousand praises to all those who exhibit patience with the noob! MZR

    S 1 Reply Last reply
    0
    • M Mike the Red

      So one of my (many) recent questions ellicited a link to Resource Acquisition Is Initialization[^]. The wikipedia entry made sense and it seemed like, in general, this structure would be a good idea. So now I'm using std::vector for the first time since my RAII introduction, and I recall that vector<> leaks unless I call vector<>.clear() when I'm done with the vector. Does that mean, considering RAII, that whenever I want to use std::vector the best-practice method would be to write a specialized container class that calls .clear() in the destructor and exposes all the std::vector methods? Now that I think of it, the container could be written as a template class.... but I'm going to let the question stand, since I'm just learning about RAII... Is this an exception to the rule, or would a container class be best? A thousand praises to all those who exhibit patience with the noob! MZR

      S Offline
      S Offline
      Stuart Dootson
      wrote on last edited by
      #2

      Mike the Red wrote:

      I recall that vector<> leaks unless I call vector<>.clear() when I'm done with the vector.

      You recall incorrectly :-) vector deallocates and calls the destructor of each contained object when it's destructed.

      Mike the Red wrote:

      Does that mean, considering RAII, that whenever I want to use std::vector the best-practice method would be to write a specialized container class that calls .clear() in the destructor and exposes all the std::vector methods?

      No - just use vector. All STL container classes are (minus trivial bugs) intended to have that behaviour, i.e. they destruct the contained elements and deallocate their storage when destructed.

      Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

      M 1 Reply Last reply
      0
      • S Stuart Dootson

        Mike the Red wrote:

        I recall that vector<> leaks unless I call vector<>.clear() when I'm done with the vector.

        You recall incorrectly :-) vector deallocates and calls the destructor of each contained object when it's destructed.

        Mike the Red wrote:

        Does that mean, considering RAII, that whenever I want to use std::vector the best-practice method would be to write a specialized container class that calls .clear() in the destructor and exposes all the std::vector methods?

        No - just use vector. All STL container classes are (minus trivial bugs) intended to have that behaviour, i.e. they destruct the contained elements and deallocate their storage when destructed.

        Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

        M Offline
        M Offline
        Mike the Red
        wrote on last edited by
        #3

        That I wasted the last 4 hours trying to stop this from leaking:

        int _tmain(int argc, _TCHAR* argv[])
        {
        _CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
        vector<char *> vc;
        vc.push_back("test");
        _CrtDumpMemoryLeaks();
        return 0;
        }


        Detected memory leaks!
        Dumping objects ->
        c:\program files\microsoft visual studio .net 2003\vc7\include\crtdbg.h(689) : {44} normal block at 0x008A0FF0, 4 bytes long.
        Data: < C > A0 A5 43 00
        Object dump complete.
        The program '[7368] ConTest.exe: Native' has exited with code 0 (0x0).

        When all I needed was a $#^$#&-ing pair of braces...

        int _tmain(int argc, _TCHAR* argv[])
        {
        _CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
        {
        vector<char *> vc;
        vc.push_back("test");
        }
        _CrtDumpMemoryLeaks();
        return 0;
        }

        I just love learning curves, don't you? Thank you for your help, Stuart! ...thanks to you, I wasted 4 hours instead of 5 or 6 ;P

        S 1 Reply Last reply
        0
        • M Mike the Red

          That I wasted the last 4 hours trying to stop this from leaking:

          int _tmain(int argc, _TCHAR* argv[])
          {
          _CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
          vector<char *> vc;
          vc.push_back("test");
          _CrtDumpMemoryLeaks();
          return 0;
          }


          Detected memory leaks!
          Dumping objects ->
          c:\program files\microsoft visual studio .net 2003\vc7\include\crtdbg.h(689) : {44} normal block at 0x008A0FF0, 4 bytes long.
          Data: < C > A0 A5 43 00
          Object dump complete.
          The program '[7368] ConTest.exe: Native' has exited with code 0 (0x0).

          When all I needed was a $#^$#&-ing pair of braces...

          int _tmain(int argc, _TCHAR* argv[])
          {
          _CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
          {
          vector<char *> vc;
          vc.push_back("test");
          }
          _CrtDumpMemoryLeaks();
          return 0;
          }

          I just love learning curves, don't you? Thank you for your help, Stuart! ...thanks to you, I wasted 4 hours instead of 5 or 6 ;P

          S Offline
          S Offline
          Stuart Dootson
          wrote on last edited by
          #4

          Ummmm - the vector hasn't been destructed when you call _CrtDumpMemoryLeaks... Try this:

          #include
          #include
          #include

          int _tmain(int argc, _TCHAR* argv[])
          {
          _CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );

          {
          	vector vc;
          	vc.push\_back("test");
          }
          
          \_CrtDumpMemoryLeaks();
          
          return 0;
          

          }

          Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

          M 1 Reply Last reply
          0
          • S Stuart Dootson

            Ummmm - the vector hasn't been destructed when you call _CrtDumpMemoryLeaks... Try this:

            #include
            #include
            #include

            int _tmain(int argc, _TCHAR* argv[])
            {
            _CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );

            {
            	vector vc;
            	vc.push\_back("test");
            }
            
            \_CrtDumpMemoryLeaks();
            
            return 0;
            

            }

            Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

            M Offline
            M Offline
            Mike the Red
            wrote on last edited by
            #5

            -nt-

            S 1 Reply Last reply
            0
            • M Mike the Red

              -nt-

              S Offline
              S Offline
              Stuart Dootson
              wrote on last edited by
              #6

              Faster than a speeding bullet :-)

              Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

              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