   Memory management and pointers
  In C/C++, if I have two pointers pointing to the same object.  And say that the object and one of the pointer is destroy.  There's really no way for the other pointer to know that its pointing address is no longer valid.It seems that smart pointer doesn't even handle this scenario.  Correct me if I'm wrong, but smart pointer work the same way Java's garbage collector work.  Even in Java, a dangling pointer doesn't know if the stuff it points to is valid or not.  On the other hand, a form of callback that notify each pointer when an object is destroy seems rather expensive to do in runtime.Am I just have to rely on good programming practice on this one?   
Quote:In C/C++, if I have two pointers pointing to the same object. And say that the object and one of the pointer is destroy. There's really no way for the other pointer to know that its pointing address is no longer valid.Correct. Raw pointers are quite dumb in this regard.Quote:It seems that smart pointer doesn't even handle this scenario. Correct me if I'm wrong, but smart pointer work the same way Java's garbage collector work. Even in Java, a dangling pointer doesn't know if the stuff it points to is valid or not. On the other hand, a form of callback that notify each pointer when an object is destroy seems rather expensive to do in runtime.This isn't correct.In Java, an object will not be collected while it still has (non cyclical) references. You cannot have a "dangling pointer" in Java.C++ smart pointers are similar, except they generally cannot handle cyclical references, either that or they require the user to check if they are valid before use (e.g. breaking a boost::shared_ptr<> cycle using boost::weak_ptr<>).Obviously you can still go out of your way and break a smart pointer, e.g. by initialising it with a garbage pointer, or by explicitly calling "delete" on the managed pointer.I think you misunderstand how smart pointers work in C++. They usually have a reference counted, either inside the pointed at object (boost::intrusive_ptr<>) or they manage one themselves (boost::shared_ptr<>). The pointed object will not be destroyed while at least one smart pointer lives that refers to it. There are no callbacks involved, no notifying other pointers about what is going on. The reference count is similarly shared between the pointers.   ;  If you use a reference counted smart pointer, like shared_ptr, the smart pointers will keep track of the number of smart pointers that currently refer to an object and will only destroy the object when the number of smart pointers referring to the object reaches zero. Yes, if you destroy the object manually, you'll have problems. The simple solution is not to destroy the object manually and let the smart pointers handle the object lifetime. That's the entire point behind using them.   ;  The point of the smart pointer is that *it* decides when the underlying object will be destroyed. And it's designed in such a way that it only does this when there are no other pointers lying around. Unless of course you go ahead and also make non-smart pointers to the same object (because it has no way to know about those).It's just like any other tool designed in C++ to make your life easier: it protects you from pain and suffering and accidents. It doesn't protect you from going out of your way to break things. Nor should it try (or so goes the C++ philosophy).   