Skip to content

Commit df788e2

Browse files
zavitaxOsirisNL
authored andcommitted
obs-browser: BrowserSource::Impl::Listener::OnDraw checks if
browser has not been destroyed to prevent race condition
1 parent 7b6d658 commit df788e2

File tree

5 files changed

+42
-3
lines changed

5 files changed

+42
-3
lines changed

obs-browser/browser-manager-base.cpp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "browser-render-handler.hpp"
1111
#include "browser-load-handler.hpp"
1212
#include "browser-obs-bridge-base.hpp"
13+
#include "browser-listener.hpp"
1314

1415
BrowserManager::BrowserManager()
1516
: pimpl(new BrowserManager::Impl())
@@ -35,6 +36,11 @@ int BrowserManager::CreateBrowser(
3536
return pimpl->CreateBrowser(browserSettings, browserListener);
3637
}
3738

39+
bool BrowserManager::IsValidBrowserIdentifier(int browserIdentifier)
40+
{
41+
return pimpl->IsValidBrowserIdentifier(browserIdentifier);
42+
}
43+
3844
void BrowserManager::DestroyBrowser(int browserIdentifier)
3945
{
4046
pimpl->DestroyBrowser(browserIdentifier);
@@ -128,7 +134,7 @@ int BrowserManager::Impl::CreateBrowser(
128134
const BrowserSettings &browserSettings,
129135
const std::shared_ptr<BrowserListener> &browserListener)
130136
{
131-
int browserIdentifier = 0;
137+
int browserIdentifier = -1;
132138
os_event_t *createdEvent;
133139
os_event_init(&createdEvent, OS_EVENT_TYPE_AUTO);
134140

@@ -167,6 +173,7 @@ int BrowserManager::Impl::CreateBrowser(
167173

168174
if (browser != nullptr) {
169175
browserIdentifier = browser->GetIdentifier();
176+
browserListener->SetBrowserIdentifier(browserIdentifier);
170177
browserMap[browserIdentifier] = browser;
171178
}
172179
os_event_signal(createdEvent);
@@ -177,11 +184,17 @@ int BrowserManager::Impl::CreateBrowser(
177184
return browserIdentifier;
178185
}
179186

187+
bool BrowserManager::Impl::IsValidBrowserIdentifier(int browserIdentifier)
188+
{
189+
return browserIdentifier >= 0 && browserMap.count(browserIdentifier) > 0;
190+
}
191+
180192
void
181193
BrowserManager::Impl::DestroyBrowser(int browserIdentifier)
182194
{
183195
if (browserMap.count(browserIdentifier) > 0) {
184196
CefRefPtr<CefBrowser> browser = browserMap[browserIdentifier];
197+
browserMap.erase(browserIdentifier);
185198
os_event_t *closeEvent;
186199
os_event_init(&closeEvent, OS_EVENT_TYPE_AUTO);
187200
CefPostTask(TID_UI, BrowserTask::newTask([&, browser]

obs-browser/browser-manager-base.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ class BrowserManager::Impl
3333
const BrowserSettings &browserSettings,
3434
const std::shared_ptr<BrowserListener> &browserListener);
3535

36+
bool IsValidBrowserIdentifier(int browserIdentifier);
3637
void DestroyBrowser(int browserIdentifier);
3738
void TickBrowser(int browserIdentifier);
3839

obs-browser/browser-source-listener-base.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,22 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
1919
#include "browser-source-base.hpp"
2020
#include "browser-types.h"
2121
#include "browser-source-listener-base.hpp"
22+
#include "browser-manager-base.hpp"
2223

2324
void BrowserSource::Impl::Listener::OnDraw( BrowserSurfaceHandle surfaceHandle,
2425
int width, int height)
2526
{
2627
UNUSED_PARAMETER(width);
2728
UNUSED_PARAMETER(height);
2829

30+
// Check if our browser has not been destroyed yet.
31+
//
32+
// Due to the asynchronous nature of CEF, we can get an OnDraw call after
33+
// BrowserManager::Instance()->DestroyBrowser() call has been made.
34+
//
35+
if (!BrowserManager::Instance()->IsValidBrowserIdentifier(GetBrowserIdentifier()))
36+
return;
37+
2938
if (textureSet.count(surfaceHandle) > 0) {
3039
obs_enter_graphics();
3140
if (browserSource)
@@ -102,4 +111,4 @@ void BrowserSource::Impl::Listener::DestroySurface(
102111
browserSource->activeTexture = nullptr;
103112
obs_leave_graphics();
104113
}
105-
}
114+
}

shared/browser-listener.hpp

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,18 @@ class BrowserListener : public BrowserListenerBase
8181
}
8282

8383
virtual void Invalidated() override {}
84-
};
84+
85+
private:
86+
int m_browserIdentifier = -1;
87+
88+
public:
89+
void SetBrowserIdentifier(const int browserIdentifier)
90+
{
91+
m_browserIdentifier = browserIdentifier;
92+
}
93+
94+
const int GetBrowserIdentifier()
95+
{
96+
return m_browserIdentifier;
97+
}
98+
};

shared/browser-manager.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ class BrowserManager {
4242
const std::shared_ptr<BrowserListener>
4343
&browserListener);
4444

45+
bool IsValidBrowserIdentifier(int browserIdentifier);
46+
4547
void DestroyBrowser(int browserIdentifier);
4648

4749
void TickBrowser(int browserIdentifier);

0 commit comments

Comments
 (0)