/*****************************************************************************
* 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 )
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;
#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();
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;
}
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 );
}
/* 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;
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