I'm trying to fix my code to enable Perl to recover unneeded data by weakening references and breaking cycles.
I recently asked a question on How to access Perl ref counts and the answer has been working well for me.
For some of my objects, the reference count is > 1 and I don't know why.
Is there a way for me to add a callback or something to help me know when a reference count is incremented? I want to know who is referencing an object.
bless function is used to return a reference which ultimately becomes an object.
Array references—which anonymous arrays are one type of—allows Perl to treat the array as a single item. This allows you to build complex, nested data structures as well. This applies to hash, code, and all the other reference types too, but here I'll show only the hash reference.
Using the Parameter Array (@_) Perl lets you pass any number of parameters to a function. The function decides which parameters to use and in what order.
Implement a Devel::XXX
package that inspects the refcounts of your objects?
package Devel::Something;
# just emulating Devel::Trace here
# see http://cpansearch.perl.org/src/MJD/Devel-Trace-0.10/Trace.pm
sub DB::DB {
if ($Devel::Something::CHECK) {
my ($package, $file, $linenumber) = caller;
... inspect current refcounts
... if any have changed, print out the details
... including current package/file/linenumber
$Devel::Something::CHECK = 0; # disable until it's enabled again
}
}
1;
# my program
... do some stuff ...
$Devel::Something::CHECK = 1;
... do some more stuff ...
$Devel::Something::CHECK = 1;
$ perl -d:Something my_program.pl ...
You could sprinkle $Devel::Something::CHECK = 1
statements at appropriate places throughout your code, or change the condition in DB::DB
to run at regular intervals (e.g., if (++$Devel::Something::CHECK % 100 == 0) {
to inspect after every 100 statement evaluations).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With