473,378 Members | 1,512 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Q: stl, boost smart pointer to prevent memory leaking

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
6 2281
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: P G | last post by:
I hope this is on topic here. I have a problem compiling a simple example of the use of boost::bind. Please take a look at the program below. ...
9
by: BekTek | last post by:
How do you think? and why?
34
by: Guch Wu | last post by:
Boost has many terrific libraries. But I want to know whether they are ready for using in real projects. Which of them are mature enough, or just only in progress?
3
by: figsys | last post by:
I am looking for a simple wrapper or mechanism to send a boost smart_ptr using WIN32 PostMessage (or PosthreadMessage). The smart_ptr should be incremented when sent and decremented when received...
16
by: Yong Hu | last post by:
Does the smart pointer never lead to memory leak? Thanks,
9
by: Christopher | last post by:
If a method is declared to return a type boost::shared_ptr<sometype>, how can the method be changed to do the equivalent of returning NULL when it was declared to return a raw pointer?
5
by: johanatan | last post by:
Does anyone know the reasons for the lack of an implicit casting operator in any greater depth than: A. Automatic conversion is believed to be too error prone. (from the FAQ at the bottom of:...
5
by: number774 | last post by:
I've used Boost for this example; in fact we have our own pointer class, for historic reasons. #include "boost\shared_ptr.hpp" // A heirarchy of classes class G1 {}; class G2: public G1 {};...
13
by: Phil Bouchard | last post by:
I am currently writting a smart pointer which is reasonnably stable and I decided supporting allocators for completion because of its increase in efficiency when the same pool used by containers is...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.