X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FDBIx%2FClass%2FSchema.pm;h=8270c27425ba2f619a35c9c109099980eb352425;hb=50261284a5486d1974adb202eb84e5ed782d3665;hp=3c78930ee0d68c58b37e3f231f205fcd31ab8ef7;hpb=c76e5262bd2cdd19ac0260b1a916767db304a953;p=dbsrgits%2FDBIx-Class.git diff --git a/lib/DBIx/Class/Schema.pm b/lib/DBIx/Class/Schema.pm index 3c78930..8270c27 100644 --- a/lib/DBIx/Class/Schema.pm +++ b/lib/DBIx/Class/Schema.pm @@ -11,6 +11,7 @@ use File::Spec; use Sub::Name 'subname'; use Module::Find(); use Storable(); +use B qw/svref_2object/; use namespace::clean; use base qw/DBIx::Class/; @@ -1372,6 +1373,29 @@ sub _register_source { $self->class_mappings(\%map); } +{ + my $global_phase_destroy; + + END { $global_phase_destroy++ } + + sub DESTROY { + return if $global_phase_destroy; + + my $self = shift; + my $srcs = $self->source_registrations; + + for my $moniker (keys %$srcs) { + # find first source that is not about to be GCed (someone other than $self + # holds a reference to it) and reattach to it, weakening our own link + if (ref $srcs->{$moniker} and svref_2object($srcs->{$moniker})->REFCNT > 1) { + $srcs->{$moniker}->schema($self); + weaken $srcs->{$moniker}; + last; + } + } + } +} + sub _unregister_source { my ($self, $moniker) = @_; my %reg = %{$self->source_registrations};