Modern C++ API: Consider Using a ListenerBinder Instead of a Raw Listener Pointer

When you associate a listener to an Entity, such as DomainParticipant or a DataReader, the Entity's automatic destruction gets disabled until you reset the listener (see how reference types work here). To avoid handling the Entity and listener deletion in your application, you should attach a listener to an Entity using bind_and_manage_listener whenever possible: 

auto listener_binder = rti::core::bind_and_manage_listener(entity, new MyListener, mask);

Or if you don’t want to have the listener deleted (for example, if it's a stack variable), use bind_listener instead:

auto listener_binder = rti::core::bind_listener(entity, my_listener, mask);

The type of listener_binder is a reference type. It will automatically detach my_listener from entity—and in the first case, also delete my_listener—when the last reference goes out of scope, allowing the destruction of entity to occur normally.

For example, we can simplify the following code:

void example()
    MyListener my_listener = new MyListener; 
    DomainParticipant my_participant(
    my_participant.listener(NULL, ...);
    delete my_listener;

By using bind_and_manage_listener we get this:

void example()
     DomainParticipant my_participant(my_domain_id); 
     auto listener_binder = rti::core::bind_and_manage_listener(
             new MyListener,


     // No code to detach and delete required