-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

In article <9fki06$[EMAIL PROTECTED]>,
 "Anandprasanna Gaitonde" <[EMAIL PROTECTED]> wrote:
  >Is this wrong and if yes then why is it wrong?? at presnt i feel
  >that both ways are same.

Consider the case where |mFooPtr| participates in a circular ownership
chain.  This is legal, though it typically requires out-of-band
signalling to ensure appropriate |Release|ing of resources.  Now you
directly |Release| |mFooPtr|, which ends up |Release|ing a chain of
objects ending in the object of which |mFooPtr| is a member.  Now that
object is no longer referenced, its |Release| |delete|s the object
itself, which invokes that objects destructor, which in turn, leads to
another call of |Release| on |mFooPtr|.  This second |Release| ends up
invoking double-|delete|s, etc.  This typically leads to a crash.

This is not a hypothetical case.  This situation has actually arisen
in practice.  As the designer of a particular class (like the one that
has the member |mFooPtr|), you don't know all the situations in which
instances of your class may be used.  Clients may well design their
use to include circular chains.  If they do, you better have used the
long form shown in the |nsCOMPtr| guide, or else used an |nsCOMPtr|. 
If you didn't, those clients will get a heap of trouble :-)

Hope this helps,
______________________________________________________________________
Scott Collins                                 http://ScottCollins.net/





-----BEGIN PGP SIGNATURE-----
Version: PGPfreeware 7.0.3 for non-commercial use <http://www.pgp.com>

iQA/AwUBOyO2jfGmojMuVn+fEQI4tgCfeRmLBtH9clRqQHcReECzwxVRJq4AoOvj
gpJ+7X/lWkmLRQ0/rOFxMFwI
=6kCB
-----END PGP SIGNATURE-----

Reply via email to