Fix freeing custom device in dynamic state tests