masks: make removal of gui forms more bullet proof (should fix #9762)

still needs to be found out if there is a deeper lying root cause
(cherry picked from commit 614f33e582a6c81105770b528e33a51f43388e14)
Fix broken logic.
(cherry picked from commit 74fe0713bc762b199f7c057540faf89c86bf0f38)