473,671 Members | 2,319 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

A question about smart pointer

Does the smart pointer never lead to memory leak?

Thanks,

Jun 29 '06 #1
16 4117
In article <11************ **********@75g2 000cwc.googlegr oups.com>,
"Yong Hu" <yh*******@gmai l.com> wrote:
Does the smart pointer never lead to memory leak?


You can still get memory leaks even if you use smart pointers. Much
depends, of course on what the smart pointer does but I think all of
them are pretty susceptible to circular reference problems.
Jun 29 '06 #2
Yong Hu wrote:
Does the smart pointer never lead to memory leak?


I will explain by comparing other languages. In C++, you must build smart
pointers and manage them carefully so they don't leak. If you use a raw
pointer to a 'new' object, you manage it more carefully or it will leak.
And you can create an object without 'new', and it has much lower odds of
leaking.

The language Java was designed to be sold to your boss. The designers want
to tell your boss, "if your programmers use Java, they will never leak
memory, like that naughty C++ language lets them leak."

So Java has built-in smart pointers. You cannot write the smart pointer
with Java - it's part of the language. And all objects must use smart
pointers. No object can create without a secret call to a kind of 'new'
function, deep inside Java.

And Java also leaks memory, plenty, if you abuse it or make many kinds of
mistakes.

In general, Garbage Collection is a very tough problem, and all languages
have compromises. All languages can leak. C++, even with smart pointers,
is higher risk than most languages because C++ lets you write safe code
that goes very fast.

--
Phlip
Jun 29 '06 #3
In message <11************ **********@75g2 000cwc.googlegr oups.com>, Yong
Hu <yh*******@gmai l.com> writes
Does the smart pointer never lead to memory leak?

It's "smart", not "magic".

The way to avoid memory leaks is to define a rigorous ownership policy,
and then consistently use the correct tools (which might be smart
pointers, or something else) to implement and enforce it.

--
Richard Herring
Jun 29 '06 #4
Yong Hu wrote:
Does the smart pointer never lead to memory leak?


Of course not -- particularly because they can be misused. Does a seat
belt always prevent fatalities in auto accidents? No, especially if one
uses it improperly (e.g., laying down across the seat to which one is
belted). Nevertheless, smart pointers (and seat belts) can go a long
way toward improving safety, and smart pointers should usually be
preferred over manual memory management.

Cheers! --M

Jun 29 '06 #5

Daniel T. wrote:
In article <11************ **********@75g2 000cwc.googlegr oups.com>,
"Yong Hu" <yh*******@gmai l.com> wrote:
Does the smart pointer never lead to memory leak?


You can still get memory leaks even if you use smart pointers. Much
depends, of course on what the smart pointer does but I think all of
them are pretty susceptible to circular reference problems.


The circular reference problem is mostly associated with smart pointers
that have shared ownership logic.
std::auto_ptr does not have shared ownership logic.
And the following smart pointer uses clone ownership logic, and not
shared logic:
http://axter.com/smartptr/classcopy__ptr.htm

Some smart pointers can use either shared logic or clone ownership
logic, like the following policy based smart pointer:
http://axter.com/smartptr

Jun 29 '06 #6
Axter wrote:
Daniel T. wrote:
In article <11************ **********@75g2 000cwc.googlegr oups.com>,
"Yong Hu" <yh*******@gmai l.com> wrote:
Does the smart pointer never lead to memory leak?


You can still get memory leaks even if you use smart pointers. Much
depends, of course on what the smart pointer does but I think all of
them are pretty susceptible to circular reference problems.


The circular reference problem is mostly associated with smart pointers
that have shared ownership logic.
std::auto_ptr does not have shared ownership logic.
And the following smart pointer uses clone ownership logic, and not
shared logic:
http://axter.com/smartptr/classcopy__ptr.htm

Some smart pointers can use either shared logic or clone ownership
logic, like the following policy based smart pointer:
http://axter.com/smartptr


Right, and some have sole ownership semantics like std::tr1::scope d_ptr
(aka boost::scoped_p tr) which do not have problems with circular
references.

Cheers! --M

Jun 29 '06 #7
Please check
http://www.boost.org/libs/smart_ptr/smart_ptr.htm

They are already part of C++0x

Jun 29 '06 #8
On Thu, 29 Jun 2006 16:12:41 GMT, Phlip <ph*******@gEEE mail.com>
wrote:
So Java has built-in smart pointers. You cannot write the smart pointer
with Java - it's part of the language. And all objects must use smart
pointers. No object can create without a secret call to a kind of 'new'
function, deep inside Java.


Actually, that metaphor is more misleading than enlightening. Pointers
in C/C++ and refences in Java are ultra-lightweight objects that have
2 well-defined, language supported purposes: referencing and
dereferencing. "Smart" pointers in C++, OTOH, are objects that mimic
pointers but may perform some heavy task (eg. reference counting) in
the background which is something entirely different.

Best wishes,
Roland Pibinger
Jun 29 '06 #9
In article <11************ **********@d56g 2000cwd.googleg roups.com>,
"Chandu" <ch************ ******@gmail.co m> wrote:
Please check
http://www.boost.org/libs/smart_ptr/smart_ptr.htm

They are already part of C++0x
Of the six smart pointers in the list you referenced, two of them are in
the C++0X working draft: shared_ptr and weak_ptr.

In article <11************ *********@p79g2 000cwp.googlegr oups.com>,
"mlimber" <ml*****@gmail. com> wrote: Right, and some have sole ownership semantics like std::tr1::scope d_ptr
(aka boost::scoped_p tr) which do not have problems with circular
references.


There is no std::tr1::scope d_ptr. There is only boost::scoped_p tr.

-Howard
Jun 29 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
1800
by: Johnny Hansen | last post by:
Hello, I've been trying to implement smart pointers in C++ (combined with a reference counter) because I want to do some memory management. My code is based on the gamedev enginuity articles, various books and ... whatever I could find on the subject :-) I'll leave out the reference counter part because its pretty basic, but my reference counter class is called xObject. My smart pointer class is called xPointer.
24
2192
by: Christopher Benson-Manica | last post by:
Is there anything wrong with my attempt (below) at implementing something resembling a smart pointer? template < class T > class SmartPointer { private: T *t; public:
9
2305
by: kathy | last post by:
I am using std::vector in my program: func() { std::vector <CMyClass *> vpMyClass; vpMyClass.push_back(new CMyClass()); vpMyClass.push_back(new CMyClass()); vpMyClass.push_back(new CMyClass()); //???? Required ??????????????//
8
5138
by: Axter | last post by:
I normally use a program call Doxygen to document my source code.(http://www.stack.nl/~dimitri/doxygen) This method works great for small and medium size projects, and you can get good documentation like the following: http://axter.com/smartptr Now I'm on a client site, and I'm trying to create the same type of documentation on a very large project. I ran the Doxygen program, and it ran for over 16 hours, before I had
92
5068
by: Jim Langston | last post by:
Someone made the statement in a newsgroup that most C++ programmers use smart pointers. His actual phrase was "most of us" but I really don't think that most C++ programmers use smart pointers, but I just don't know. I don't like them because I don't trust them. I use new and delete on pure pointers instead. Do you use smart pointers?
33
5050
by: Ney André de Mello Zunino | last post by:
Hello. I have written a simple reference-counting smart pointer class template called RefCountPtr<T>. It works in conjunction with another class, ReferenceCountable, which is responsible for the actual counting. Here is the latter's definition: // --- Begin ReferenceCountable.h ---------- class ReferenceCountable
4
1732
by: Deep | last post by:
I'm in doubt about what is smart pointer. so, please give me simple description about smart pointer and an example of that. I'm just novice in c++. regards, John.
13
2063
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 shared. I was told the new standards are being finalized and I am hoping minor but important changes could be applied before anything else. To give a quick overview on the current status of this topic, you will find below the latest text...
50
4474
by: Juha Nieminen | last post by:
I asked a long time ago in this group how to make a smart pointer which works with incomplete types. I got this answer (only relevant parts included): //------------------------------------------------------------------ template<typename Data_t> class SmartPointer { Data_t* data; void(*deleterFunc)(Data_t*);
0
8390
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8819
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8597
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7428
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5692
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4222
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4402
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2048
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1806
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.