I've just added an undef ....; to the end of all relevent functions
to correspond with all the my ......;
Whether undef-ing works, depends on what exactly you undef.
In the trivial example above, undef $h at the end of the loop wouldn't help (as that's implicitly
happening anyway), while undef %$h, undef $h->{myself} or delete $h->{myself} would help.
Point is that you have to break the circle before the data structure becomes unreachable...
In real life, self-referencing circles are often not immediately
evident, as they may come into being indirectly through several data
structures, e.g. A references B, which references C, which ...
references A — or some such. Your ref counts > 1 might hint at
such a situation, unless you have another good explanation for them :)
Sometimes (if all else fails), it helps to step by step disable
parts of the program, until the problem goes away. In this case, look
more closely into whatever that last part was, etc. — Without
seeing any actual code, it's hard to come up more specific tips.