Correctly unlink references if a ResourceIdentifier goes out of scope.

Previously a link between resource id and referred object remained even if the resource identifier got garbage collected. Now the link gets removed correctly if no more instances with the same identifier are around.

This is implemented by tracking the instances at the class level and unlinking any still existing links if the count for one identifier reaches zero. Two tests verify that it works.

I furthermore made two smaller implementation detail changes.

This unfortunately will conflict with #673 but I was already done with most parts when it appeared so I guess we will just have to manually merge them :-(.