annotate Lib/IMPL/Profiler/Memory.pm @ 189:08015e2803f1

IMPL::Vew::Web - fixed memory leaks, more tests
author cin
date Wed, 04 Apr 2012 02:49:45 +0400
parents 029c9610528c
children cd1ff7029a63
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 0
diff changeset
1 package IMPL::Profiler::Memory;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 0
diff changeset
2
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 0
diff changeset
3 use strict;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 0
diff changeset
4 use Carp qw(longmess shortmess);
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 0
diff changeset
5 use Scalar::Util qw(refaddr weaken isweak);
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 0
diff changeset
6
188
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 49
diff changeset
7 my %listeners;
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 49
diff changeset
8 my $trapped;
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 0
diff changeset
9
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 0
diff changeset
10 BEGIN {
188
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 49
diff changeset
11 $trapped = 0;
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 49
diff changeset
12 }
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 0
diff changeset
13
188
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 49
diff changeset
14 sub import {
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 49
diff changeset
15 if (not $trapped) {
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 49
diff changeset
16 *CORE::GLOBAL::bless = sub {
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 49
diff changeset
17 $_[1] |= caller unless $_[1];
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 49
diff changeset
18 my $ref = CORE::bless $_[0],$_[1];
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 49
diff changeset
19
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 49
diff changeset
20 $_->track($ref) foreach values %listeners;
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 49
diff changeset
21
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 49
diff changeset
22 return $ref;
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 49
diff changeset
23 };
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 49
diff changeset
24 $trapped = 1;
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 49
diff changeset
25 }
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 49
diff changeset
26 }
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 0
diff changeset
27
188
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 49
diff changeset
28 sub _ConnectListener {
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 49
diff changeset
29 my ($self,$listener) = @_;
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 49
diff changeset
30
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 49
diff changeset
31 die "Invalid listener" unless ref $listener;
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 49
diff changeset
32
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 49
diff changeset
33 $listeners{refaddr($listener)} = $listener;
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 49
diff changeset
34 }
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 0
diff changeset
35
188
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 49
diff changeset
36 sub _RemoveListener {
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 49
diff changeset
37 my ($self,$listener) = @_;
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 49
diff changeset
38
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 49
diff changeset
39 die "Invalid listener" unless ref $listener;
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 49
diff changeset
40
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 49
diff changeset
41 delete $listeners{refaddr($listener)};
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 49
diff changeset
42 }
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 0
diff changeset
43
188
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 49
diff changeset
44 sub Monitor {
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 49
diff changeset
45 my ($self,$code) = @_;
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 49
diff changeset
46
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 49
diff changeset
47 my $data = IMPL::Profiler::Memory::Data->new();
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 49
diff changeset
48
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 49
diff changeset
49 $data->Monitor($code);
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 49
diff changeset
50
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 49
diff changeset
51 return $data;
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 0
diff changeset
52 }
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 0
diff changeset
53
188
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 49
diff changeset
54 package IMPL::Profiler::Memory::Data;
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 49
diff changeset
55 use parent qw(IMPL::Object::Fields);
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 49
diff changeset
56
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 49
diff changeset
57 use Data::Dumper();
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 49
diff changeset
58 use Scalar::Util qw(refaddr weaken isweak);
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 49
diff changeset
59
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 49
diff changeset
60 use fields qw( objects );
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 49
diff changeset
61
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 49
diff changeset
62 sub CTOR {
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 49
diff changeset
63 my $this = shift;
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 49
diff changeset
64 $this->{objects} = [];
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 49
diff changeset
65 }
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 49
diff changeset
66
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 49
diff changeset
67 sub track {
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 49
diff changeset
68 my $i = scalar @{$_[0]->{objects}};
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 49
diff changeset
69 $_[0]->{objects}[$i] = $_[1];
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 49
diff changeset
70 weaken($_[0]->{objects}[$i]);
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 49
diff changeset
71 }
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 49
diff changeset
72
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 49
diff changeset
73 sub Purge {
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 49
diff changeset
74 my $this = shift;
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 49
diff changeset
75
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 49
diff changeset
76 return $this->{objects} = [ grep defined($_), @{$this->{objects}}];
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 0
diff changeset
77 }
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 0
diff changeset
78
188
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 49
diff changeset
79 sub Dump {
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 49
diff changeset
80 my $this = shift;
189
08015e2803f1 IMPL::Vew::Web - fixed memory leaks, more tests
cin
parents: 188
diff changeset
81 return Data::Dumper->Dump([$this->{objects}]);
188
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 49
diff changeset
82 }
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 0
diff changeset
83
188
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 49
diff changeset
84 sub isLeak {
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 49
diff changeset
85 my ($this) = @_;
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 49
diff changeset
86 $this->Purge();
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 49
diff changeset
87 return ( scalar(@{$this->{objects}}) > 0);
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 0
diff changeset
88 }
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 0
diff changeset
89
188
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 49
diff changeset
90 sub Monitor {
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 49
diff changeset
91 my ($this,$code) = @_;
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 49
diff changeset
92
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 49
diff changeset
93 die "A reference to a subroutine is required" unless ref $code;
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 49
diff changeset
94
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 49
diff changeset
95 IMPL::Profiler::Memory->_ConnectListener($this);
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 49
diff changeset
96 eval {
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 49
diff changeset
97 $code->();
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 49
diff changeset
98 };
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 49
diff changeset
99 my $err = $@;
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 49
diff changeset
100 IMPL::Profiler::Memory->_RemoveListener($this);
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 49
diff changeset
101
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 49
diff changeset
102 die $err if $err;
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 49
diff changeset
103
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 49
diff changeset
104 return;
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 0
diff changeset
105 }
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 0
diff changeset
106
188
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 49
diff changeset
107
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 49
diff changeset
108
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 0
diff changeset
109 1;