M
mike3
Hi.
I saw this:
http://stackoverflow.com/questions/...ything-and-forget-about-classic-normal-pointe
--
"You should use smart pointers careful. They are not the silver bullet
when considering memory management. Circular references are still an
issue.
When making the class design, always think who has the ownership of an
object (has the responsibility to destroy that object). Complement
that with smart pointers, if necessary, but don't forget about the
ownership."
--
And another thing that once seemed clear is now not so clear. What is
"careful" -- what is one to watch out for?
And does this mean that in many cases (it says "smart pointers, *if
necessary*", suggesting there's a good chance they aren't
"necessary"), the "owning object" should use a raw pointer? Yet I've
heard raw pointers are "evil".
So what is it?
And then I see this:
http://stackoverflow.com/questions/417481/pointers-smart-pointers-or-shared-pointers
--
"Although "never" isn't entirely accurate. If you're implementing a
smart pointer you need to use raw pointers. Outside of that special
case you're much better off using the smart versions." (a comment on
one of the answers)
--
"much better off using the smart versions"... whereas the "use them
carefully" to me implies "sparingly". Or am I wrong, and they should
be used frequently, but the care is in _how_ you use them, not _how
many_ or _how often_?
Consider this illustration snippet:
class Owner {
private:
std::vector<Owned *> owns;
public:
...
~Owner();
void addSomething(Owned *);
};
....
~Owner()
{
for(std::vector<Owner *>::iterator it(owns.begin()); it !=
owns.end(); ++it)
delete (*it);
}
void Owner::addSomething(Owned *smth)
{
owns.push_back(smth);
}
void f()
{
Owner oneOwner, twoOwners;
Owned *smthToOwn = new Owned;
oneOwner.addSomething(smthToOwn); // BUT WAIT! The pointer to
smthToOwn is still in f()!
twoOwners.addSomething(smthToOwn); // FATAL!
}
What I notice is this:
1. Only one Owner can own a given Owned. Yet with a raw pointer,
there's nothing to stop us from putting it into two Owners, causing a
fatal crash when both are destroyed. Now, one could say "well then
just don't do that", but shouldn't that be enforced by code and not
just the user's understanding?
2. This concern suggests to use auto_ptr or something like it. But
auto_ptr is attacked on the same website. And technically, the
auto_ptr owns the object, not the Owner. It seems the only way Owner
can directly own the object is with an _EVIL_ raw pointer.
So what to do?
I saw this:
http://stackoverflow.com/questions/...ything-and-forget-about-classic-normal-pointe
--
"You should use smart pointers careful. They are not the silver bullet
when considering memory management. Circular references are still an
issue.
When making the class design, always think who has the ownership of an
object (has the responsibility to destroy that object). Complement
that with smart pointers, if necessary, but don't forget about the
ownership."
--
And another thing that once seemed clear is now not so clear. What is
"careful" -- what is one to watch out for?
And does this mean that in many cases (it says "smart pointers, *if
necessary*", suggesting there's a good chance they aren't
"necessary"), the "owning object" should use a raw pointer? Yet I've
heard raw pointers are "evil".
So what is it?
And then I see this:
http://stackoverflow.com/questions/417481/pointers-smart-pointers-or-shared-pointers
--
"Although "never" isn't entirely accurate. If you're implementing a
smart pointer you need to use raw pointers. Outside of that special
case you're much better off using the smart versions." (a comment on
one of the answers)
--
"much better off using the smart versions"... whereas the "use them
carefully" to me implies "sparingly". Or am I wrong, and they should
be used frequently, but the care is in _how_ you use them, not _how
many_ or _how often_?
Consider this illustration snippet:
class Owner {
private:
std::vector<Owned *> owns;
public:
...
~Owner();
void addSomething(Owned *);
};
....
~Owner()
{
for(std::vector<Owner *>::iterator it(owns.begin()); it !=
owns.end(); ++it)
delete (*it);
}
void Owner::addSomething(Owned *smth)
{
owns.push_back(smth);
}
void f()
{
Owner oneOwner, twoOwners;
Owned *smthToOwn = new Owned;
oneOwner.addSomething(smthToOwn); // BUT WAIT! The pointer to
smthToOwn is still in f()!
twoOwners.addSomething(smthToOwn); // FATAL!
}
What I notice is this:
1. Only one Owner can own a given Owned. Yet with a raw pointer,
there's nothing to stop us from putting it into two Owners, causing a
fatal crash when both are destroyed. Now, one could say "well then
just don't do that", but shouldn't that be enforced by code and not
just the user's understanding?
2. This concern suggests to use auto_ptr or something like it. But
auto_ptr is attacked on the same website. And technically, the
auto_ptr owns the object, not the Owner. It seems the only way Owner
can directly own the object is with an _EVIL_ raw pointer.
So what to do?