It's probably a combination of things... the caching having it being found by the wrong thing and perhaps something incorrect in your code concerning changing the value of the variable before you attempt to interact with the mapped object.
Suffice it to say, what you're suggesting in this feature request pretty much, as best as I understand it, violates the entire functionality of the NameMapping engine. To map an object based upon it's parent which the engine uses in the first place to find the object itself... yeah, that's a recursive loop that just gives me a headache thinking about it.
Perhaps it would be better, rather than continuing this conversation as part of a feature request, you post the problem to the main forums so folks can help you figure out how to resolve your problem.