|
|
Am 14.03.2012 17:33, schrieb Kevin Wampler:
> On 3/14/2012 9:14 AM, clipka wrote:
>>> And btw, cyclic references are not the only problematic situation with
>>> reference counting. There are cases where objects may be deleted too
>>> early,
>>> while there's still code using them.
>>
>> I can't think of any reason why that could possibly happen, provided
>> that the reference counting is implemented in a thread-safe manner.
>
> Imagine you've implemented a tree where each node is an object which has
> a "delete" method that removes the node from the tree. If you're not
> careful the node could end up removing all references-counted pointers
> to itself (since "this" isn't reference counted) and be reclaimed while
> still running its own delete method.
It is a problem only as long as you tie object destruction to reclaiming
of memory.
If the memory management is designed to /first/ call the destructor of
the "payload object", and only /afterwards/ decrement the reference
counter and release memory, then I don't see anything you'd have to fear
from this scenario.
Problems may arise of course if you try to implement the reference
counter management functionality in the "payload object" itself. Don't
do that - tie the ref counter management to a dedicated pointer type.
Post a reply to this message
|
|