in reply to Garbage collected class attributes with inside out classes.

I'm a bit confused the docs for Class::Std say "Every class that loads the Class::Std module automatically has a DESTROY() destructor, which automatically cleans up any attributes declared with the :ATTR() trait (see below)." So doesn't that mean it already handles this? If it doesn't wouldn't it work to catch the DESTROY of each object and removes its attributes as the objects died? There is the definite possibility that I'm talking about something completely different than you! ;)


___________
Eric Hodges

Replies are listed 'Best First'.
Re^2: Garbage collected class attributes with inside out classes.
by kyle (Abbot) on Mar 01, 2007 at 22:47 UTC

    The documentation is correct, of course!

    In my first implementation, the class attributes are declared as :ATTR, and they behave as described. That's why that implementation does not have its own DEMOLISH (called by the Class::Std DESTROY). That is, in a way, its advantage. You do your work up front in a way that the module takes care of the rest of the work at the end.

    The difference between what I have declared as :ATTR and what is normally declared as :ATTR is that normally the elements of the hash are all different. Each instance has its own key and value. In my case, the values of an individual hash are all the same—one reference shared by every instance. (Each still has its own key, but they all have the same value.) I'm tricking the instance attributes into behaving as class attributes.

    In the second implementation, the class attributes are not declared as :ATTR at all, so I have to clean them up myself with my own DEMOLISH.

      I think I understand now. So you are talking about having class attributes along side the instance attributes? If that is the case wouldn't you want the class attributes to exist until the program terminated in case you created a new instance after all the others where destroyed?

      This my ($other_ident) = keys %example_hash_of; is confusing me, what does that do? It would seem that its just grabbing the first key in the hash regardless of what it is.

      I have the distinct feeling I'm confusing the trees with the forest here, but I'm also determined to understand!


      ___________
      Eric Hodges

        wouldn't you want the class attributes to exist until the program terminated in case you created a new instance after all the others where destroyed?

        In many cases, I'd expect that to be true, yes. Part of my background for this, however, is that one of these class attributes holds a large data structure that I don't want to hold in memory. There could be other reasons for wanting one thing shared by all instances that you also want to deallocate when there are no more instances.

        It would seem that its just grabbing the first key in the hash regardless of what it is.

        Yes, that's exactly what it does. It finds any other instance of the same class. if there is no other instance, $other_ident will be undef. If there is some other instance, I take a copy of that instance's class attribute reference. Since they all have the same reference, it doesn't matter which one I get.