Q: stl, boost smart pointer to prevent memory leaking

Z

zl2k

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
 
A

Axter

zl2k said:
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
---------------------------------------------------------------------------­-------------
 
B

benben

zl2k said:
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
 
L

loufoque

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;
}
 
L

loufoque

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.
 
M

mlimber

loufoque said:
benben wrote :


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
 
M

mlimber

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top