By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,470 Members | 949 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,470 IT Pros & Developers. It's quick & easy.

Q: stl, boost smart pointer to prevent memory leaking

P: n/a
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

Apr 3 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a
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
---------------------------------------------------------------------------*-------------

Apr 3 '06 #2

P: n/a
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
Apr 3 '06 #3

P: n/a
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;
}
Apr 3 '06 #4

P: n/a
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.
Apr 3 '06 #5

P: n/a
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

Apr 3 '06 #6

P: n/a
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

Apr 3 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.