Using const references seemed to be causing a crash so have changed back to just passing the function by value.