How I can return mavsdk::System in a function

Hello. I want to build a drone object by C ++ using MAVSDK, containing mavsdk :: System as an attribute, a method that returns mavsdk :: System and some functions that perform some actions such as takeoff, landing, mission. But when I call getSystem, it discovers the system but it will exit the program when getSystem returns mavsdk :: System. How can i do that? Thank you! This is my getSystem function:

System& Drone::getSystem(){
Mavsdk dc;
std::string connection_url;
connection_url = “udp://:14540”;
ConnectionResult connection_result;

bool discovered_system = false;

connection_result = dc.add_any_connection(connection_url);

if (connection_result != ConnectionResult::Success) {
    std::cout << ERROR_CONSOLE_TEXT << "Connection failed: " << connection_result
              << NORMAL_CONSOLE_TEXT << std::endl;

system = dc.system();

std::cout << "Waiting to discover system..." << std::endl;
dc.register_on_discover([&discovered_system](uint64_t uuid) {
    std::cout << "Discovered system with UUID: " << uuid << std::endl;
    discovered_system = true;


if (!discovered_system) {
    std::cout << ERROR_CONSOLE_TEXT << "No system found, exiting." << NORMAL_CONSOLE_TEXT
              << std::endl;

return system;