]> git.sesse.net Git - vlc/blobdiff - src/misc/objects.c
vlc_object_find_name: return vlc_object_t *
[vlc] / src / misc / objects.c
index 1acb14021f135b8882c4866ea96463a9641a0acc..83512eeef6bcb06a6dd1f323ad19eefd43d954b3 100644 (file)
@@ -83,6 +83,7 @@ static void           ListChildren  ( vlc_list_t *, vlc_object_t *, int );
 
 static void vlc_object_destroy( vlc_object_t *p_this );
 static void vlc_object_detach_unlocked (vlc_object_t *p_this);
+static void vlc_object_dump( vlc_object_t *p_this );
 
 /*****************************************************************************
  * Local structure lock
@@ -147,10 +148,12 @@ void *__vlc_custom_create( vlc_object_t *p_this, size_t i_size,
         if( i_type == VLC_OBJECT_LIBVLC )
             p_new->p_libvlc = (libvlc_int_t*)p_new;
         else
+        {
             p_new->p_libvlc = NULL;
+            vlc_mutex_init( &structure_lock );
+        }
 
         p_this = p_priv->next = p_priv->prev = p_new;
-        vlc_mutex_init( &structure_lock );
     }
     else
         p_new->p_libvlc = p_this->p_libvlc;
@@ -305,30 +308,6 @@ static void vlc_object_destroy( vlc_object_t *p_this )
 
     free( p_this->psz_header );
 
-#ifndef NDEBUG
-    if( VLC_OBJECT(p_this->p_libvlc) == p_this )
-    {
-        /* Test for leaks */
-        vlc_object_t *leaked = p_priv->next;
-        while( leaked != p_this )
-        {
-            /* We are leaking this object */
-            fprintf( stderr,
-                     "ERROR: leaking object (id:%i, type:%s, name:%s)\n",
-                     leaked->i_object_id, leaked->psz_object_type,
-                     leaked->psz_object_name );
-            /* Dump object to ease debugging */
-            vlc_object_dump( leaked );
-            fflush(stderr);
-            leaked = vlc_internals (leaked)->next;
-        }
-
-        if( p_priv->next != p_this )
-            /* Dump libvlc object to ease debugging */
-            vlc_object_dump( p_this );
-    }
-#endif
-
     if( p_this->p_libvlc == NULL )
         /* We are the global object ... no need to lock. */
         vlc_mutex_destroy( &structure_lock );
@@ -580,7 +559,8 @@ void * vlc_object_get( libvlc_int_t *p_anchor, int i_id )
     vlc_object_t *obj = NULL;
 #ifndef NDEBUG
     int canc = vlc_savecancel ();
-    fprintf (stderr, "Use of deprecated vlc_object_get(%d)\n", i_id);
+    fprintf (stderr, "Use of deprecated vlc_object_get(%d) ", i_id);
+    vlc_backtrace ();
     vlc_restorecancel (canc);
 #endif
     vlc_mutex_lock( &structure_lock );
@@ -640,15 +620,25 @@ void * __vlc_object_find( vlc_object_t *p_this, int i_type, int i_mode )
     return p_found;
 }
 
+#undef vlc_object_find_name
 /**
- ****************************************************************************
- * find a named object and increment its refcount
- *****************************************************************************
- * This function recursively looks for a given object name. i_mode can be one
- * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
- *****************************************************************************/
-void * __vlc_object_find_name( vlc_object_t *p_this, const char *psz_name,
-                               int i_mode )
+ * Finds a named object and increment its reference count.
+ * Beware that objects found in this manner can be "owned" by another thread,
+ * be of _any_ type, and be attached to any module (if any). With such an
+ * object reference, you can set or get object variables, emit log messages,
+ * and read write-once object parameters (i_object_id, psz_object_type, etc).
+ * You CANNOT cast the object to a more specific object type, and you
+ * definitely cannot invoke object type-specific callbacks with this.
+ *
+ * @param p_this object to search from
+ * @param psz_name name of the object to search for
+ * @param i_mode search direction: FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
+ *
+ * @return a matching object (must be released by the caller),
+ * or NULL on error.
+ */
+vlc_object_t *vlc_object_find_name( vlc_object_t *p_this,
+                                    const char *psz_name, int i_mode )
 {
     vlc_object_t *p_found;
 
@@ -741,6 +731,29 @@ void __vlc_object_release( vlc_object_t *p_this )
 
     if( b_should_destroy )
     {
+#ifndef NDEBUG
+        if( VLC_OBJECT(p_this->p_libvlc) == p_this )
+        {
+            /* Test for leaks */
+            vlc_object_t *leaked = internals->next;
+            while( leaked != p_this )
+            {
+                /* We are leaking this object */
+                fprintf( stderr,
+                         "ERROR: leaking object (id:%i, type:%s, name:%s)\n",
+                         leaked->i_object_id, leaked->psz_object_type,
+                         leaked->psz_object_name );
+                /* Dump object to ease debugging */
+                vlc_object_dump( leaked );
+                fflush(stderr);
+                leaked = vlc_internals (leaked)->next;
+            }
+
+            if( internals->next != p_this )
+                /* Dump libvlc object to ease debugging */
+                vlc_object_dump( p_this );
+        }
+#endif
         /* Remove the object from object list
          * so that it cannot be encountered by vlc_object_get() */
         vlc_internals (internals->next)->prev = internals->prev;
@@ -872,7 +885,7 @@ vlc_list_t * __vlc_list_find( vlc_object_t *p_this, int i_type, int i_mode )
          * not be shared across LibVLC instances. In the mean time, this ugly
          * hack is brought to you by Courmisch. */
         if (i_type == VLC_OBJECT_MODULE)
-            return vlc_list_find ((vlc_object_t *)vlc_global ()->p_module_bank,
+            return vlc_list_find ((vlc_object_t *)p_module_bank,
                                   i_type, FIND_CHILD);
         return vlc_list_find (p_this->p_libvlc, i_type, FIND_CHILD);
 
@@ -1098,13 +1111,13 @@ void vlc_list_release( vlc_list_t *p_list )
 /*****************************************************************************
  * dump an object. (Debug function)
  *****************************************************************************/
-void __vlc_object_dump( vlc_object_t *p_this )
+static void vlc_object_dump( vlc_object_t *p_this )
 {
-    vlc_mutex_lock( &structure_lock );
     char psz_foo[2 * MAX_DUMPSTRUCTURE_DEPTH + 1];
     psz_foo[0] = '|';
+
+    vlc_assert_locked( &structure_lock );
     DumpStructure( p_this, 0, psz_foo );
-    vlc_mutex_unlock( &structure_lock );
 }
 
 /* Following functions are local */