Commit 234a17d0 authored by Rémi Denis-Courmont's avatar Rémi Denis-Courmont

Improve the still really dumb reference checker

parent 7a826ae2
...@@ -187,6 +187,9 @@ struct vlc_object_internals_t ...@@ -187,6 +187,9 @@ struct vlc_object_internals_t
vlc_spinlock_t ref_spin; vlc_spinlock_t ref_spin;
unsigned i_refcount; unsigned i_refcount;
vlc_destructor_t pf_destructor; vlc_destructor_t pf_destructor;
#ifndef NDEBUG
vlc_thread_t creator_id;
#endif
/* Objects tree structure */ /* Objects tree structure */
vlc_object_t *prev, *next; vlc_object_t *prev, *next;
......
...@@ -171,6 +171,13 @@ void *vlc_custom_create( vlc_object_t *p_this, size_t i_size, ...@@ -171,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->pipes[0] = p_priv->pipes[1] = -1;
p_priv->next = VLC_OBJECT (p_libvlc_global); 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 ); vlc_mutex_lock( &structure_lock );
p_priv->prev = vlc_internals (p_libvlc_global)->prev; p_priv->prev = vlc_internals (p_libvlc_global)->prev;
vlc_internals (p_libvlc_global)->prev = p_new; vlc_internals (p_libvlc_global)->prev = p_new;
...@@ -1481,28 +1488,47 @@ static void ListChildren( vlc_list_t *p_list, vlc_object_t *p_this, int i_type ) ...@@ -1481,28 +1488,47 @@ static void ListChildren( vlc_list_t *p_list, vlc_object_t *p_this, int i_type )
} }
#ifndef NDEBUG #ifndef NDEBUG
# ifdef __GLIBC__
# include <execinfo.h>
# endif
void vlc_refcheck (vlc_object_t *obj) void vlc_refcheck (vlc_object_t *obj)
{ {
static unsigned errors = 0; static unsigned errors = 0;
vlc_object_t *caller = vlc_threadobj ();
vlc_object_internals_t *priv = vlc_internals (obj);
int refs;
if (errors > 100) if (errors > 100)
return; return;
if (!caller) /* Anyone can use the root object (though it should not exist) */
return; /* main thread, not sure how to handle it */ 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;
/* An object can always access itself without reference! */ /* A thread can use its own object without reference! */
vlc_object_t *caller = vlc_threadobj ();
if (caller == obj) if (caller == obj)
return; return;
/* The calling thread is younger than the object. /* The calling thread is younger than the object.
* Access could be valid, we would need more accounting. */ * Access could be valid through cross-thread synchronization;
if (caller->i_object_id > obj->i_object_id) * we would need better accounting. */
if (caller && (caller->i_object_id > obj->i_object_id))
return; return;
int refs;
vlc_spin_lock (&priv->ref_spin); vlc_spin_lock (&priv->ref_spin);
refs = priv->i_refcount; refs = priv->i_refcount;
vlc_spin_unlock (&priv->ref_spin); vlc_spin_unlock (&priv->ref_spin);
...@@ -1512,25 +1538,22 @@ void vlc_refcheck (vlc_object_t *obj) ...@@ -1512,25 +1538,22 @@ void vlc_refcheck (vlc_object_t *obj)
if (refs > 1) if (refs > 1)
return; return;
/* The parent of an object normally holds the unique reference. fprintf (stderr, "The %s %s thread object is accessing...\n"
* As not all objects are threads, it could also be an ancestor. */ "the %s %s object in a suspicous manner.\n",
vlc_mutex_lock (&structure_lock); caller && caller->psz_object_name
for (vlc_object_t *cur = obj; cur != NULL; cur = cur->p_parent) ? caller->psz_object_name : "unnamed",
if (cur == caller) caller ? caller->psz_object_type : "main",
{ obj->psz_object_name ? obj->psz_object_name : "unnamed",
vlc_mutex_unlock (&structure_lock); obj->psz_object_type);
return; fflush (stderr);
}
vlc_mutex_unlock (&structure_lock); #ifdef __GLIBC__
void *stack[20];
#if 1 int stackdepth = backtrace (stack, sizeof (stack) / sizeof (stack[0]));
if (caller->i_object_type == VLC_OBJECT_PLAYLIST) backtrace_symbols_fd (stack, stackdepth, 2);
return; /* Playlist is too clever, or hopelessly broken. */
#endif #endif
msg_Err (caller, "This thread is accessing...");
msg_Err (obj, "...this object in a suspicious manner.");
if (++errors == 100) if (++errors == 100)
msg_Err (caller, "Too many reference errors"); fprintf (stderr, "Too many reference errors!\n");
} }
#endif #endif
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment