From ac8ca41623ab1052e4327353a36257b2810b3387 Mon Sep 17 00:00:00 2001 From: Dario Casalinuovo Date: Wed, 7 Sep 2016 20:24:55 +0200 Subject: [PATCH] Fix StreamerPlugin reference counting * BReferenceable deleted itself when the latest reference was released causing a double free. * The code now avoid to call the function when we are about to delete the object. --- src/kits/media/PluginManager.cpp | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/kits/media/PluginManager.cpp b/src/kits/media/PluginManager.cpp index 49cd9fe69c..d9b6ccd486 100644 --- a/src/kits/media/PluginManager.cpp +++ b/src/kits/media/PluginManager.cpp @@ -632,12 +632,12 @@ PluginManager::CreateStreamer(Streamer** streamer, BUrl url, BDataIO** source) } (*streamer)->fMediaPlugin = plugin; + (*streamer)->fReference.AcquireReference(); BDataIO* streamSource = NULL; if ((*streamer)->Sniff(url, &streamSource) == B_OK) { TRACE("PluginManager::CreateStreamer: Sniff success\n"); *source = streamSource; - (*streamer)->fReference.AcquireReference(); return B_OK; } @@ -661,13 +661,14 @@ PluginManager::DestroyStreamer(Streamer* streamer) // destructor... MediaPlugin* plugin = streamer->fMediaPlugin; - streamer->fReference.ReleaseReference(); - // Delete the streamer only when every reference is released - if (streamer->fReference.CountReferences() == 0) { + // we avoid to call ReleaseReference on the last object + // and handle deletion when the streamer is destroyed. + if (streamer->fReference.CountReferences() == 1) { delete streamer; PutPlugin(plugin); - } + } else + streamer->fReference.ReleaseReference(); } }