0

Fix an edge case with CameraVideoFrameHandler

The edge case might lead to unexpected behaviour. It happens when `Close()` function is called, then for some reason `camera_video_source_remote_` mojo becomes disconnected, leading to a call to `OnFatalErrorOrDisconnection()`. During `OnFatalErrorOrDisconnection()`, `camera_video_stream_subsciption_remote_` is reset, which would invoke the destructor of `CameraVideoFrameHandler`, while still in the middle of the function.

Change-Id: I9d72d7b8a0ac14fc6dd4aabdb765dcfe03d4f113
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5181880
Commit-Queue: Ahmed Moussa <ahmedmoussa@google.com>
Reviewed-by: Ahmed Fakhry <afakhry@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1248321}
This commit is contained in:
ahmedmoussa
2024-01-17 20:20:05 +00:00
committed by Chromium LUCI CQ
parent e2149e6224
commit 1c61f416b2

@ -689,7 +689,16 @@ void CameraVideoFrameHandler::OnFatalErrorOrDisconnection() {
weak_ptr_factory_.InvalidateWeakPtrs();
video_frame_handler_receiver_.reset();
camera_video_source_remote_.reset();
// There is a chance where resetting `camera_video_stream_subsciption_remote_`
// causes the deletion of `this`. That is the reason why we need to use a weak
// pointer, as to avoid unexpected behaviour.
// For more info check `Close()` function.
auto weak_ptr = weak_ptr_factory_.GetWeakPtr();
camera_video_stream_subsciption_remote_.reset();
if (!weak_ptr) {
return;
}
if (delegate_) {
delegate_->OnFatalErrorOrDisconnection();