That must be intrusive thoughts.https://en.wikipedia.org/wiki/Intrusive_thoughts
In the case of the knife, being in a roof or things like that, it works like this:
-The presence of the object escalates your anxiety.
-An intrusive thought appears about you wanting to use it.
(this way the anxiety disappears).
tldr; the cause of the anxiety is the presence of danger, if this danger is actually realized it ends so your anxiety ends too.
(freud would say thanathos instinct)
But you would be dead if you really did that, so you just simulate it in your head.
It's similar to the monsters and shadows that scare kids. The monsters are always GOING to do something to you, but they never actually do it. This builds anxiety.
Unless in a nightmare they actually do it, and you wake up from it.