[vlc-commits] [Git][videolan/vlc][master] upnp: refactor using C++ try/catch

François Cartegnie (@fcartegnie) gitlab at videolan.org
Tue Nov 16 12:55:29 UTC 2021



François Cartegnie pushed to branch master at VideoLAN / VLC


Commits:
60456133 by Alexandre Janniaux at 2021-11-16T11:55:20+00:00
upnp: refactor using C++ try/catch

The try/catch for exception safety was already needed there, and this
simplify the code a lot, so this moves the allocation into unique_ptr to
benefit from RAII destructors.

In addition, this commit change the call to free(p_sys) into the correct
C++ delete, avoiding a mismatch new/free.

Introduced in 0d89fe3fd7d27d7c3f349bb46a915dbae65c02f8.

- - - - -


1 changed file:

- modules/services_discovery/upnp.cpp


Changes:

=====================================
modules/services_discovery/upnp.cpp
=====================================
@@ -1619,32 +1619,18 @@ void *SearchThread(void *data)
 }
 
 static int OpenRD( vlc_object_t *p_this )
+try
 {
     vlc_renderer_discovery_t *p_rd = ( vlc_renderer_discovery_t* )p_this;
-    renderer_discovery_sys_t *p_sys  = new(std::nothrow) renderer_discovery_sys_t;
+    auto p_sys = std::make_unique<renderer_discovery_sys_t>();
 
-    if ( !p_sys )
-        return VLC_ENOMEM;
-    p_rd->p_sys = p_sys;
+    p_rd->p_sys = p_sys.get();
     p_sys->p_upnp = UpnpInstanceWrapper::get( p_this );
 
     if ( !p_sys->p_upnp )
-    {
-        delete p_sys;
         return VLC_EGENERIC;
-    }
 
-    try
-    {
-        p_sys->p_renderer_list = std::make_shared<RD::MediaRendererList>( p_rd );
-    }
-    catch ( const std::bad_alloc& )
-    {
-        msg_Err( p_rd, "Failed to create a MediaRendererList");
-        p_sys->p_upnp->release();
-        free(p_sys);
-        return VLC_EGENERIC;
-    }
+    p_sys->p_renderer_list = std::make_shared<RD::MediaRendererList>( p_rd );
     p_sys->p_upnp->addListener( p_sys->p_renderer_list );
 
     if( vlc_clone( &p_sys->thread, SearchThread, (void*)p_rd,
@@ -1653,11 +1639,22 @@ static int OpenRD( vlc_object_t *p_this )
             msg_Err( p_rd, "Can't run the lookup thread" );
             p_sys->p_upnp->removeListener( p_sys->p_renderer_list );
             p_sys->p_upnp->release();
-            delete p_sys;
             return VLC_EGENERIC;
         }
+
+    /* Release ownership of std::unique_ptr */
+    p_sys.release();
+
     return VLC_SUCCESS;
 }
+catch ( const std::bad_alloc& )
+{
+    vlc_renderer_discovery_t *p_rd = (vlc_renderer_discovery_t*)p_this;
+    renderer_discovery_sys_t *p_sys = static_cast<renderer_discovery_sys_t *>(p_rd->p_sys);
+    if (p_sys && p_sys->p_upnp)
+        p_sys->p_upnp->release();
+    return VLC_ENOMEM;
+}
 
 static void CloseRD( vlc_object_t *p_this )
 {



View it on GitLab: https://code.videolan.org/videolan/vlc/-/commit/60456133bd8a456fb836875ed5ecaaeb94c3a2ab

-- 
View it on GitLab: https://code.videolan.org/videolan/vlc/-/commit/60456133bd8a456fb836875ed5ecaaeb94c3a2ab
You're receiving this email because of your account on code.videolan.org.




More information about the vlc-commits mailing list