[cscore] Fix crash when usbcamera is deleted before message pump thread fully starts (#3804)

shared_from_this will assert if the shared pointer is in the middle of being destructed. Because we access shared_from_this in the message pump, this can easily occur. The solution is to grab the weak pointer, manually attempt to lock it, and only continue if that succeeds. The message pump is already synchronized to the usb camera being destructed, so this is a fine behavior.
This commit is contained in:
Thad House
2021-12-19 13:39:23 -08:00
committed by GitHub
parent 11d9859ef1
commit 59a7528fd6

View File

@@ -485,9 +485,16 @@ bool UsbCameraImpl::DeviceConnect() {
const wchar_t* path = m_widePath.c_str();
m_mediaSource = CreateVideoCaptureDevice(path);
if (!m_mediaSource)
if (!m_mediaSource) {
return false;
m_imageCallback = CreateSourceReaderCB(shared_from_this(), m_mode);
}
auto weakThis = weak_from_this();
auto sharedThis = weakThis.lock();
if (sharedThis) {
m_imageCallback = CreateSourceReaderCB(sharedThis, m_mode);
} else {
return false;
}
m_sourceReader =
CreateSourceReader(m_mediaSource.Get(), m_imageCallback.Get());