]> git.sesse.net Git - vlc/blobdiff - src/misc/objects.c
Print backtrace in vlc_pthread_fatal
[vlc] / src / misc / objects.c
index 06177dc240ad8867592e6721b8168507ad04d5bc..e717aaf04fc263c1aa29d07ee885c2aa305ceb08 100644 (file)
@@ -85,7 +85,8 @@ static void vlc_object_detach_unlocked (vlc_object_t *p_this);
 /*****************************************************************************
  * Local structure lock
  *****************************************************************************/
-static vlc_mutex_t    structure_lock;
+static vlc_mutex_t     structure_lock;
+static vlc_threadvar_t thread_object;
 
 void *vlc_custom_create( vlc_object_t *p_this, size_t i_size,
                          int i_type, const char *psz_type )
@@ -170,6 +171,13 @@ void *vlc_custom_create( vlc_object_t *p_this, size_t i_size,
     p_priv->pipes[0] = p_priv->pipes[1] = -1;
 
     p_priv->next = VLC_OBJECT (p_libvlc_global);
+#if defined (NDEBUG)
+    /* ... */
+#elif defined (LIBVLC_USE_PTRHEAD)
+    p_priv->creator_id = pthread_self ();
+#elif defined (WIN32)
+    p_priv->creator_id = GetCurrentThreadId ();
+#endif
     vlc_mutex_lock( &structure_lock );
     p_priv->prev = vlc_internals (p_libvlc_global)->prev;
     vlc_internals (p_libvlc_global)->prev = p_new;
@@ -338,23 +346,24 @@ static void vlc_object_destroy( vlc_object_t *p_this )
 #ifndef NDEBUG
         assert( p_global == vlc_global() );
         /* Test for leaks */
-        for( vlc_object_t *leaked = p_priv->next;
-             leaked != p_this;
-             leaked = vlc_internals (leaked)->next )
+        if (p_priv->next != 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 libvlc object to ease debugging */
-            vlc_object_dump( leaked );
-            fflush(stderr);
-        }
+            vlc_object_t *leaked = p_priv->next, *first = leaked;
+            do
+            {
+                /* 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 libvlc object to ease debugging */
+                vlc_object_dump( leaked );
+                fflush(stderr);
+                leaked = vlc_internals (leaked)->next;
+            }
+            while (leaked != first);
 
-        if( p_priv->next != p_this )
-        {
-            /* Dump libvlc object to ease debugging */
+            /* Dump global object to ease debugging */
             vlc_object_dump( p_this );
             /* Strongly abort, cause we want these to be fixed */
             abort();
@@ -648,10 +657,12 @@ void * vlc_object_get( int i_id )
         if( obj->i_object_id == i_id )
         {
             vlc_object_yield( obj );
-            return obj;
+            goto out;
         }
     }
+    obj = NULL;
 
+out:
     vlc_mutex_unlock( &structure_lock );
     return obj;
 }
@@ -838,6 +849,15 @@ void __vlc_object_attach( vlc_object_t *p_this, vlc_object_t *p_parent )
     INSERT_ELEM( priv->pp_children, priv->i_children, priv->i_children,
                  p_this );
 
+    /* Kill the object if parent is already dead.
+     * Note: We should surely lock parent here, but that would
+     * create quite a few dead lock case. Hopefully, it
+     * is perfectly safe to do it that way. We only risk
+     * receiving kill event twice. But given current API
+     * it is ok. */
+    if( p_this->p_parent->b_die )
+        vlc_object_kill( p_this );
+
     vlc_mutex_unlock( &structure_lock );
 }
 
@@ -932,6 +952,7 @@ vlc_list_t * __vlc_list_find( vlc_object_t *p_this, int i_type, int i_mode )
         /* Check allocation was successful */
         if( p_list->i_count != i_count )
         {
+            vlc_mutex_unlock( &structure_lock );
             msg_Err( p_this, "list allocation failed!" );
             p_list->i_count = 0;
             break;
@@ -1465,3 +1486,74 @@ static void ListChildren( vlc_list_t *p_list, vlc_object_t *p_this, int i_type )
         ListChildren( p_list, p_tmp, i_type );
     }
 }
+
+#ifndef NDEBUG
+# ifdef __GLIBC__
+#  include <execinfo.h>
+# endif
+
+void vlc_refcheck (vlc_object_t *obj)
+{
+    static unsigned errors = 0;
+    if (errors > 100)
+        return;
+
+    /* Anyone can use the root object (though it should not exist) */
+    if (obj == VLC_OBJECT (vlc_global ()))
+        return;
+
+    /* Anyone can use its libvlc instance object */
+    if (obj == VLC_OBJECT (obj->p_libvlc))
+        return;
+
+    /* The thread that created the object holds the initial reference */
+    vlc_object_internals_t *priv = vlc_internals (obj);
+#if defined (LIBVLC_USE_PTHREAD)
+    if (pthread_equal (priv->creator_id, pthread_self ()))
+#elif defined WIN32
+    if (priv->creator_id == GetCurrentThreadId ())
+#else
+    if (0)
+#endif
+        return;
+
+    /* A thread can use its own object without reference! */
+    vlc_object_t *caller = vlc_threadobj ();
+    if (caller == obj)
+        return;
+
+    /* The calling thread is younger than the object.
+     * Access could be valid through cross-thread synchronization;
+     * we would need better accounting. */
+    if (caller && (caller->i_object_id > obj->i_object_id))
+        return;
+
+    int refs;
+    vlc_spin_lock (&priv->ref_spin);
+    refs = priv->i_refcount;
+    vlc_spin_unlock (&priv->ref_spin);
+
+    /* Object has more than one reference.
+     * The current thread could be holding a valid reference. */
+    if (refs > 1)
+        return;
+
+    fprintf (stderr, "The %s %s thread object is accessing...\n"
+             "the %s %s object in a suspicous manner.\n",
+             caller && caller->psz_object_name
+                     ? caller->psz_object_name : "unnamed",
+             caller ? caller->psz_object_type : "main",
+             obj->psz_object_name ? obj->psz_object_name : "unnamed",
+             obj->psz_object_type);
+    fflush (stderr);
+
+#ifdef __GLIBC__
+    void *stack[20];
+    int stackdepth = backtrace (stack, sizeof (stack) / sizeof (stack[0]));
+    backtrace_symbols_fd (stack, stackdepth, 2);
+#endif
+
+    if (++errors == 100)
+        fprintf (stderr, "Too many reference errors!\n");
+}
+#endif