Q: stl, boost smart pointer to prevent memory leaking

Discussion in 'C++' started by zl2k, Apr 3, 2006.

  1. zl2k

    zl2k Guest

    hi,
    When I considered about preventing memory leaking, the method came up
    to my mind is using boost smart pointer if possible (use stl::vector
    instead of type[], use smart pointer whenever declare an instance of
    class). With the container of STL, this may often result in a vector of
    smart pointers, or a smart pointer of STL container. Am I making things
    too complicated or this is an usual implementation? Thanks in advance.
    zl2k
    zl2k, Apr 3, 2006
    #1
    1. Advertising

  2. zl2k

    Axter Guest

    zl2k wrote:
    > hi,
    > When I considered about preventing memory leaking, the method came up
    > to my mind is using boost smart pointer if possible (use stl::vector
    > instead of type[], use smart pointer whenever declare an instance of
    > class). With the container of STL, this may often result in a vector of
    > smart pointers, or a smart pointer of STL container. Am I making things
    > too complicated or this is an usual implementation? Thanks in advance.
    > zl2k


    This is a common implementation.
    Be careful that you don't try to copy one vector of smart pointers to
    another if you're using boost::shared_ptr, because then you end up with
    two container pointing to the samething.
    For STL containers, I recommend using a clone (deep-copy) smart pointer
    like the following:
    http://axter.com/smartptr
    It's more efficient the boost::shared_ptr, and it can perform deep copy
    without requiring the target type to have a clone method.

    You can also consider using boost pointer containers, which also
    perform deep copies, but they require the target type to have a clone
    method.
    For more information, read the above link.

    ---------------------------------------------------------------------------­-------------

    David Maisonave
    http://axter.com

    Author of policy smart pointers (http://axter.com/smartptr)
    Top ten member of C++ Expert Exchange:
    http://www.experts-exchange.com/Cplusplus
    ---------------------------------------------------------------------------­-------------
    Axter, Apr 3, 2006
    #2
    1. Advertising

  3. zl2k

    benben Guest

    zl2k wrote:
    > hi,
    > When I considered about preventing memory leaking, the method came up
    > to my mind is using boost smart pointer if possible (use stl::vector
    > instead of type[], use smart pointer whenever declare an instance of
    > class). With the container of STL, this may often result in a vector of
    > smart pointers, or a smart pointer of STL container. Am I making things
    > too complicated or this is an usual implementation? Thanks in advance.
    > zl2k
    >


    Are you making an over-complication? It depends. Memory management is a
    complex problem and there isn't a single simple solution that addresses
    every aspect.

    You don't have to use a smart pointer to create every object. If the
    object life time is scope-controlled, use a local variable:

    void log_event(const std::string& path, event e)
    {
    ofstream output(path); // scope-controlled
    output << e;
    }


    If the ownership of the object is well defined, use an auto_ptr (which
    is not a honest-to-God smart pointer:)

    std::auto_ptr<xml_doc> load_xml_doc(const std::string& path)
    {
    std::auto_ptr<xml_doc> pdoc(new xml_doc);
    pdoc->load_document(path);
    return pdoc; // transfer of ownership
    }

    If you don't need polymorphism or shallow copy, use a container of that
    type instead of one of pointers to that type.

    template <typename T>
    class matrix
    {
    std::deque<T> storage;
    // not std::deque<shared_ptr<T> > storage;
    // ...
    };

    And last but not least, if the class should behave like a value type,
    make it so. This is typically done by encapsulating the smart pointer:

    class string
    {
    struct storage_type
    {
    std::vector<char> characters;

    // ...
    };

    shared_ptr<storage_type> storage;

    // ...
    }

    string operator+ (const string& a, const string& b)
    {
    // use of string is as intuitive value types
    string c = a;
    c += b;
    return c;
    }

    Regards,
    Ben
    benben, Apr 3, 2006
    #3
  4. zl2k

    loufoque Guest

    benben wrote :

    > If the ownership of the object is well defined, use an auto_ptr (which
    > is not a honest-to-God smart pointer:)
    >
    > std::auto_ptr<xml_doc> load_xml_doc(const std::string& path)
    > {
    > std::auto_ptr<xml_doc> pdoc(new xml_doc);
    > pdoc->load_document(path);
    > return pdoc; // transfer of ownership
    > }


    Or you could simply write

    xml_doc load_xml_doc(const std::string& path)
    {
    xml_doc pdoc;
    pdoc.load_document(path);
    return pdoc;
    }
    loufoque, Apr 3, 2006
    #4
  5. zl2k

    loufoque Guest

    zl2k wrote :
    > hi,
    > When I considered about preventing memory leaking, the method came up
    > to my mind is using boost smart pointer if possible


    The question is why are you even using pointers ?
    You should only need them for polymorphism or shared resources.
    loufoque, Apr 3, 2006
    #5
  6. zl2k

    mlimber Guest

    loufoque wrote:
    > benben wrote :
    >
    > > If the ownership of the object is well defined, use an auto_ptr (which
    > > is not a honest-to-God smart pointer:)
    > >
    > > std::auto_ptr<xml_doc> load_xml_doc(const std::string& path)
    > > {
    > > std::auto_ptr<xml_doc> pdoc(new xml_doc);
    > > pdoc->load_document(path);
    > > return pdoc; // transfer of ownership
    > > }

    >
    > Or you could simply write
    >
    > xml_doc load_xml_doc(const std::string& path)
    > {
    > xml_doc pdoc;
    > pdoc.load_document(path);
    > return pdoc;
    > }


    If the xml_doc class has not been made non-copyable and if it is not
    too expensive to make a copy of the class (which happens when you
    return it, unless the compiler happens to optimize it out, which is not
    the general case), then the latter code would work fine. If either of
    those conditions is not true, however, benben's code is
    better/necessary.

    Cheers! --M
    mlimber, Apr 3, 2006
    #6
  7. zl2k

    mlimber Guest

    benben wrote:
    [snip]
    > auto_ptr (which is not a honest-to-God smart pointer:)

    [snip]

    Certainly std::auto_ptr has different semantics than other smart
    pointers, but I take "smart pointer" to mean any object that
    automatically deletes its pointee and overloads the * and -> operators.
    What do you mean by it?

    Cheers! --M
    mlimber, Apr 3, 2006
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. jojo
    Replies:
    5
    Views:
    373
    jan V
    Sep 8, 2005
  2. Simon Andrews

    Leaking memory when writing to URL

    Simon Andrews, Jan 6, 2006, in forum: Java
    Replies:
    6
    Views:
    3,253
    Robert Klemme
    Jan 6, 2006
  3. Sam L.
    Replies:
    0
    Views:
    262
    Sam L.
    Sep 24, 2005
  4. Sigmathaar

    Memory leaking

    Sigmathaar, Dec 20, 2005, in forum: C++
    Replies:
    6
    Views:
    594
    Axter
    Dec 20, 2005
  5. gNash

    Memory leaking..

    gNash, Nov 15, 2007, in forum: C Programming
    Replies:
    25
    Views:
    774
    J. J. Farrell
    Nov 16, 2007
Loading...

Share This Page