Use common memory management functions for lwlib and refactor users.