Add some sanity checks in freemat()