I suggest an attribute property called 'leak_check' that when set does
the following at destruction:

 * Weaken the reference stored in the attribute
 * Return if the ref is undef'd because the object has no strong ref's
 * Take action (warn/die/custom) if the ref remains (there is a
hardref somewhere)

This attribute property would be very useful when you define an
attribute that you know should be the only remaining ref to an object
when your instance is destroyed.

Example usage:

has something => (
    isa => 'Something::Temporary',
    is   => 'ro',
    leak_check => sub { # Could also use 'fatal' or 'warn'
        my ( $self, $attr_name, $ref ) = @_;
        ...
    },
    default => sub { Something::Temporary->new() },
);

I just finished debugging an issue that was caused by an object ref
being held in a location where it should have been undef'd. The issue
was in an object that serializes its changes to disk on destruction.
This object was stored in an object implemented with Moose. If Moose
had this capability, and I as a developer had used it on this
attribute which I knew should cleanup with the parent, the memory leak
warning would have saved me time.

For now, to prevent this issue in the future I have manually added
logic to validate some attributes go away on cleanup. But I think this
would be a valuable tool for developers to activate when they know
what they are doing, and have an attribute that should be cleaned with
the parent.

Thank you,

Chad Granum

Reply via email to