Pull Request for Isssue #571 (Library leak in widgets when switching profiles)

A cleaner pull request as suggested in issue #571.