in reply to How can I call other objects from my object during DESTROY
My parsimonious friend tye above has hit the nail on the head, but you may still be wondering what's going on. Let me expand a bit.
Perl reclaims memory in two distinct ways. First, it maintains a reference count, meaning it knows how many ways there are in the current program to refer to a given object. When the reference count goes to zero, the memory can be reclaimed.
With this style, the problem outlined in your code sample won't happen. The instance of Special::Object won't be reclaimed as long as the instance of the containing class refers to it via $self->{_obj} (as long as the parent instance is around and nobody tampers with encapsulation).
When a thread ends (or a single-threaded Perl exits), the so-called "global destruction" phase tries to reclaim memory allocated in that thread by traversing everything allocated in that thread. That's the stage at which your problem is happening. Now, the section on Two-Phased Garbage Collection states:
Objects are destructed in a separate pass before ordinary refs just to prevent object destructors from using refs that have been themselves destructed.But as you've found, that doesn't solve the whole problem.
At this point, your main option is algorithm changes. One way, as tye is suggesting, is to not make objects global. Another possibility is to remove inter-object dependencies as far as destruction is concerned (may be difficult depending on the rest of your code).
HTH
Update: Egads! I have to learn to type faster.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
(tye)Re4: How can I call other objects from my object during DESTROY
by tye (Sage) on Jul 11, 2001 at 05:56 UTC |