Does the smart pointer never lead to memory leak?
Thanks, 16 4104
In article <11**********************@75g2000cwc.googlegroups. com>,
"Yong Hu" <yh*******@gmail.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.
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
In message <11**********************@75g2000cwc.googlegroups. com>, Yong
Hu <yh*******@gmail.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
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
Daniel T. wrote: In article <11**********************@75g2000cwc.googlegroups. com>, "Yong Hu" <yh*******@gmail.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
Axter wrote: Daniel T. wrote: In article <11**********************@75g2000cwc.googlegroups. com>, "Yong Hu" <yh*******@gmail.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::scoped_ptr
(aka boost::scoped_ptr) which do not have problems with circular
references.
Cheers! --M
On Thu, 29 Jun 2006 16:12:41 GMT, Phlip <ph*******@gEEEmail.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
In article <11**********************@d56g2000cwd.googlegroups .com>,
"Chandu" <ch******************@gmail.com> 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*********************@p79g2000cwp.googlegroups. com>,
"mlimber" <ml*****@gmail.com> wrote: Right, and some have sole ownership semantics like std::tr1::scoped_ptr (aka boost::scoped_ptr) which do not have problems with circular references.
There is no std::tr1::scoped_ptr. There is only boost::scoped_ptr.
-Howard
Roland Pibinger wrote: On Thu, 29 Jun 2006 16:12:41 GMT, Phlip <ph*******@gEEEmail.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
sorry, Java refs may have some way of ref counting too
Diego Martins wrote: Roland Pibinger wrote: On Thu, 29 Jun 2006 16:12:41 GMT, Phlip <ph*******@gEEEmail.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.
sorry, Java refs may have some way of ref counting too
Java refs have more than two language-supported activities, but I decline
to determine what they are. (And I suspected that Java used
mark-and-sweep, where Python uses ref counting.)
The example is leading, for neophytes, because it introduces the general
topic of garbage collection.
It leads to the ideal that no smart pointer, no matter how smart or
language-supported, is utterly leak-proof.
--
Phlip
Howard Hinnant wrote: "mlimber" <ml*****@gmail.com> wrote: Right, and some have sole ownership semantics like std::tr1::scoped_ptr (aka boost::scoped_ptr) which do not have problems with circular references.
There is no std::tr1::scoped_ptr. There is only boost::scoped_ptr.
I hadn't noticed that! Any idea why was it not included, too?
(According to http://boost.org/libs/smart_ptr/smart_ptr.htm#History,
the semantics of scoped_ptr were the original semantics for auto_ptr,
but the recommendation of Library Working Group was ignored and
transfer-of-ownership semantics were added to auto_ptr. I find the ToO
semantics quite useful at times, but at the same time adding scoped_ptr
in C++0x would seem to provide a smart pointer that is not susceptible
to accidental destructive copying and would thus serve as the usual
RAII resource manager. In that scheme, auto_ptr would be relegated only
to transferring ownership and would be rather inaptly named.)
Cheers! --M
In article <11**********************@h44g2000cwa.googlegroups .com>,
"mlimber" <ml*****@gmail.com> wrote: Howard Hinnant wrote: "mlimber" <ml*****@gmail.com> wrote: Right, and some have sole ownership semantics like std::tr1::scoped_ptr (aka boost::scoped_ptr) which do not have problems with circular references.
There is no std::tr1::scoped_ptr. There is only boost::scoped_ptr.
I hadn't noticed that! Any idea why was it not included, too? (According to http://boost.org/libs/smart_ptr/smart_ptr.htm#History, the semantics of scoped_ptr were the original semantics for auto_ptr, but the recommendation of Library Working Group was ignored and transfer-of-ownership semantics were added to auto_ptr. I find the ToO semantics quite useful at times, but at the same time adding scoped_ptr in C++0x would seem to provide a smart pointer that is not susceptible to accidental destructive copying and would thus serve as the usual RAII resource manager. In that scheme, auto_ptr would be relegated only to transferring ownership and would be rather inaptly named.)
I can give only my personal opinion, and not anything official.
I am seeking to deprecate auto_ptr and replace it with unique_ptr: http://www.open-std.org/jtc1/sc22/wg...56.html#20.4.5
%20-%20Class%20template%20auto_ptr
The link above describes the motivation for deprecating auto_ptr. It
also introduces and completely describes unique_ptr as the safer
replacement. unique_ptr also offers ownership transfer like auto_ptr,
but not with copy syntax as auto_ptr does. Instead it uses move syntax
(which is easily searched for in source code). Given this, accidental
ownership transfer is no longer something to worry about. unique_ptr
functionality is a superset of boost::scoped_ptr, and not likely to be
accidently abused (as is auto_ptr). Thus I'm personally lacking
motivation to have a scoped_ptr in addition to unique_ptr.
Fwiw, here: http://home.twcny.rr.com/hinnant/cpp...nique_ptr.html
are some minor improvements to the unique_ptr interface which have
surfaced since the publication of N1856 and which I hope to standardize
as well.
If you have use cases for scoped_ptr that unique_ptr is unsuitable to
fulfill, I would be most interested in hearing about them (so that
scoped_ptr could also be proposed for standardization, or so that
unique_ptr could be improved to meet those use cases). Or if you would
just like to question how unique_ptr would behave in certain contexts
(compared to scoped_ptr or auto_ptr), please feel free to ask me
(publicly or privately).
-Howard
On 30 Jun 2006 13:16:36 -0700, "Diego Martins" <jo********@gmail.com>
wrote: sorry, Java refs may have some way of ref counting too
Java has some language mechanisms that enable GC in general or weak
refernces in particular but calling those mechanisms "built-in smart
pointers" is, IMO, not appropriate.
Best wishes,
Roland Pibinger
Howard Hinnant wrote:
I am seeking to deprecate auto_ptr and replace it with unique_ptr:
http://www.open-std.org/jtc1/sc22/wg...56.html#20.4.5
%20-%20Class%20template%20auto_ptr
The link above describes the motivation for deprecating auto_ptr. It
also introduces and completely describes unique_ptr as the safer
replacement. unique_ptr also offers ownership transfer like auto_ptr,
but not with copy syntax as auto_ptr does. Instead it uses move syntax
(which is easily searched for in source code). Given this, accidental
ownership transfer is no longer something to worry about. unique_ptr
functionality is a superset of boost::scoped_ptr, and not likely to be
accidently abused (as is auto_ptr). Thus I'm personally lacking
motivation to have a scoped_ptr in addition to unique_ptr.
That's good work. Do you have a hunch about whether auto_ptr will be
replaced by (or supplemented with) unique_ptr in C++0x?
Fwiw, here:
http://home.twcny.rr.com/hinnant/cpp...nique_ptr.html
are some minor improvements to the unique_ptr interface which have
surfaced since the publication of N1856 and which I hope to standardize
as well.
If you have use cases for scoped_ptr that unique_ptr is unsuitable to
fulfill, I would be most interested in hearing about them (so that
scoped_ptr could also be proposed for standardization, or so that
unique_ptr could be improved to meet those use cases). Or if you would
just like to question how unique_ptr would behave in certain contexts
(compared to scoped_ptr or auto_ptr), please feel free to ask me
(publicly or privately).
I don't see anything obvious. unique_ptr seems to cover all my uses for
scoped_ptr (and more).
Cheers! --M
In article <11**********************@p79g2000cwp.googlegroups .com>,
"mlimber" <ml*****@gmail.comwrote:
Do you have a hunch about whether auto_ptr will be
replaced by (or supplemented with) unique_ptr in C++0x?
I think the odds are a little better than 50/50 that we will deprecate
auto_ptr (not remove, just deprecate), and add unique_ptr (as specified
in N1856). When last reviewed (Oct. 2005), N1856 had strong support in
the LWG, pending acceptance of the rvalue reference language feature.
Rvalue reference has been approved by the EWG and is currently under
review by the CWG: http://www.open-std.org/jtc1/sc22/wg...2006/n2011.htm
-Howard This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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,...
|
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:
|
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...
|
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...
|
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,...
| |
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...
|
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.
|
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...
|
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):
...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
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...
| |
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...
|
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...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |