Re: AddRef and Release
"George" <George@discussions.microsoft.com> wrote in message
news:3E78503B-0635-4300-9772-9B27D328C8D3@microsoft.com
No you don't. If you decided to omit calling AddRef when you assigned
pIX1 to pIXTmp, you should also omit a corresponding Release.
It does not work for all situations and not safe. For example, if we
do not call AddRef to pIXTmp when assigning pIX2 to it, some other
thread may call pIX2 -> Release() and will result in destroying of
the object instance pointed by pIXTmp.
Yes. In fact, I clearly mentioned this in my very first response: you
can only omit AddRef / Release pair if nobody calls pIX2->Release in
between, so you can rely on it to keep the object alive. This has
nothing to do with pIXTmp being reassigned to point to different objects
at different times, of course.
By the way, if you have another thread that can call pIX2->Release() at
any time, you have a race condition anyway: that thread could call it
right before you assign pIX2 to pIXTmp, and you end up with two dangling
pointers.
--
With best wishes,
Igor Tandetnik
With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925