Fix the design flaw with obs_sceneitem_destroy

The previous commit used the scene as a  parameter to check to see if
the scene item was still present within the scene before destroying, but
this was actually unnecessary because the fault was because the destroy
signal was being triggered *before* the scene's mutex locked, thus
causing a race condition.  I changed the code so that it signals after
the lock instead of before, so the scene parameter should no longer be
necessary.
master
jp9000 2014-01-29 18:27:42 -07:00
parent 31ceec04ce
commit 3243a9f8c5
3 changed files with 12 additions and 24 deletions

View File

@ -124,7 +124,7 @@ static void scene_video_render(void *data)
struct obs_scene_item *del_item = item;
item = item->next;
obs_sceneitem_destroy(scene, del_item);
obs_sceneitem_destroy(del_item);
continue;
}
@ -294,38 +294,26 @@ obs_sceneitem_t obs_scene_add(obs_scene_t scene, obs_source_t source)
return item;
}
int obs_sceneitem_destroy(obs_scene_t scene, obs_sceneitem_t item)
int obs_sceneitem_destroy(obs_sceneitem_t item)
{
int ref = 0;
if (!scene || !item)
return ref;
pthread_mutex_lock(&scene->mutex);
bool found = false;
obs_sceneitem_t i = scene->first_item;
while (i) {
if (i == item) {
found = true;
break;
}
}
if (found) {
if (item) {
obs_scene_t scene = item->parent;
struct calldata params = {0};
signal_item_destroy(item, &params);
calldata_free(&params);
pthread_mutex_lock(&item->parent->mutex);
pthread_mutex_lock(&scene->mutex);
signal_item_destroy(item, &params);
detach_sceneitem(item);
pthread_mutex_unlock(&item->parent->mutex);
if (item->source)
ref = obs_source_release(item->source);
bfree(item);
}
pthread_mutex_unlock(&scene->mutex);
pthread_mutex_unlock(&scene->mutex);
calldata_free(&params);
}
return ref;
}

View File

@ -503,7 +503,7 @@ EXPORT obs_sceneitem_t obs_scene_add(obs_scene_t scene, obs_source_t source);
/** Removes/destroys a scene item. Returns the source reference counter
* (if any) */
EXPORT int obs_sceneitem_destroy(obs_scene_t scene, obs_sceneitem_t item);
EXPORT int obs_sceneitem_destroy(obs_sceneitem_t item);
/** Gets the scene parent associated with the scene item */
EXPORT obs_scene_t obs_sceneitem_getscene(obs_sceneitem_t item);

View File

@ -524,7 +524,7 @@ void OBSBasic::on_actionRemoveSource_triggered()
QVariant userData = sel->data(Qt::UserRole);
obs_sceneitem_t item = VariantPtr<obs_sceneitem_t>(userData);
obs_sceneitem_destroy(scene, item);
obs_sceneitem_destroy(item);
obs_scene_release(scene);
gs_leavecontext();