diff --git a/rtc_base/BUILD.gn b/rtc_base/BUILD.gn index e9d1bdb0ad..551383054b 100644 --- a/rtc_base/BUILD.gn +++ b/rtc_base/BUILD.gn @@ -499,6 +499,7 @@ rtc_source_set("rtc_operations_chain") { ":checks", ":macromagic", ":refcount", + "../api:refcountedbase", "../api:scoped_refptr", "../api:sequence_checker", "system:no_unique_address", diff --git a/rtc_base/operations_chain.cc b/rtc_base/operations_chain.cc index 59d30d350e..f42482b3fe 100644 --- a/rtc_base/operations_chain.cc +++ b/rtc_base/operations_chain.cc @@ -37,10 +37,11 @@ void OperationsChain::CallbackHandle::OnOperationComplete() { // static scoped_refptr OperationsChain::Create() { - return new OperationsChain(); + // Explicit new, to access private constructor. + return rtc::scoped_refptr(new OperationsChain()); } -OperationsChain::OperationsChain() : RefCountedObject() { +OperationsChain::OperationsChain() { RTC_DCHECK_RUN_ON(&sequence_checker_); } @@ -63,8 +64,10 @@ bool OperationsChain::IsEmpty() const { } std::function OperationsChain::CreateOperationsChainCallback() { - return [handle = rtc::scoped_refptr( - new CallbackHandle(this))]() { handle->OnOperationComplete(); }; + return [handle = rtc::make_ref_counted( + rtc::scoped_refptr(this))]() { + handle->OnOperationComplete(); + }; } void OperationsChain::OnOperationComplete() { diff --git a/rtc_base/operations_chain.h b/rtc_base/operations_chain.h index 7823f6e238..e6fb4f99d3 100644 --- a/rtc_base/operations_chain.h +++ b/rtc_base/operations_chain.h @@ -19,6 +19,7 @@ #include #include "absl/types/optional.h" +#include "api/ref_counted_base.h" #include "api/scoped_refptr.h" #include "api/sequence_checker.h" #include "rtc_base/checks.h" @@ -113,7 +114,7 @@ class OperationWithFunctor final : public Operation { // The OperationsChain is kept-alive through reference counting if there are // operations pending. This, together with the contract, guarantees that all // operations that are chained get executed. -class OperationsChain final : public RefCountedObject { +class OperationsChain final : public RefCountedNonVirtual { public: static scoped_refptr Create(); ~OperationsChain(); @@ -163,7 +164,7 @@ class OperationsChain final : public RefCountedObject { // std::function, which is a copyable type. To allow the callback to // be copyable, it is backed up by this reference counted handle. See // CreateOperationsChainCallback(). - class CallbackHandle final : public RefCountedObject { + class CallbackHandle final : public RefCountedNonVirtual { public: explicit CallbackHandle(scoped_refptr operations_chain); ~CallbackHandle(); diff --git a/rtc_base/ref_counted_object.h b/rtc_base/ref_counted_object.h index 37612add8c..274eb9e76d 100644 --- a/rtc_base/ref_counted_object.h +++ b/rtc_base/ref_counted_object.h @@ -148,7 +148,7 @@ template , T>::type* = nullptr> scoped_refptr make_ref_counted(Args&&... args) { - return new RefCountedObject(std::forward(args)...); + return scoped_refptr(new RefCountedObject(std::forward(args)...)); } // `make_ref_counted` for complete classes that are not convertible to @@ -175,7 +175,8 @@ template < T>::type* = nullptr> scoped_refptr> make_ref_counted(Args&&... args) { - return new FinalRefCountedObject(std::forward(args)...); + return scoped_refptr>( + new FinalRefCountedObject(std::forward(args)...)); } // `Ref<>`, `Ref<>::Type` and `Ref<>::Ptr`: diff --git a/rtc_base/rtc_certificate.cc b/rtc_base/rtc_certificate.cc index 496b4ac4b4..e9137f4abb 100644 --- a/rtc_base/rtc_certificate.cc +++ b/rtc_base/rtc_certificate.cc @@ -13,6 +13,7 @@ #include #include "rtc_base/checks.h" +#include "rtc_base/ref_counted_object.h" #include "rtc_base/ssl_certificate.h" #include "rtc_base/ssl_identity.h" #include "rtc_base/time_utils.h" @@ -21,7 +22,9 @@ namespace rtc { scoped_refptr RTCCertificate::Create( std::unique_ptr identity) { - return new RTCCertificate(identity.release()); + // Explicit new to access proteced constructor. + return rtc::scoped_refptr( + new RTCCertificate(identity.release())); } RTCCertificate::RTCCertificate(SSLIdentity* identity) : identity_(identity) { @@ -61,7 +64,7 @@ scoped_refptr RTCCertificate::FromPEM( SSLIdentity::CreateFromPEMStrings(pem.private_key(), pem.certificate())); if (!identity) return nullptr; - return new RTCCertificate(identity.release()); + return RTCCertificate::Create(std::move(identity)); } bool RTCCertificate::operator==(const RTCCertificate& certificate) const { diff --git a/rtc_base/task_utils/pending_task_safety_flag.cc b/rtc_base/task_utils/pending_task_safety_flag.cc index 57b3f6ce88..8bff213a03 100644 --- a/rtc_base/task_utils/pending_task_safety_flag.cc +++ b/rtc_base/task_utils/pending_task_safety_flag.cc @@ -12,23 +12,29 @@ namespace webrtc { +// static +rtc::scoped_refptr PendingTaskSafetyFlag::CreateInternal( + bool alive) { + // Explicit new, to access private constructor. + return rtc::scoped_refptr( + new PendingTaskSafetyFlag(alive)); +} + // static rtc::scoped_refptr PendingTaskSafetyFlag::Create() { - return new PendingTaskSafetyFlag(true); + return CreateInternal(true); } rtc::scoped_refptr PendingTaskSafetyFlag::CreateDetached() { - rtc::scoped_refptr safety_flag( - new PendingTaskSafetyFlag(true)); + rtc::scoped_refptr safety_flag = CreateInternal(true); safety_flag->main_sequence_.Detach(); return safety_flag; } rtc::scoped_refptr PendingTaskSafetyFlag::CreateDetachedInactive() { - rtc::scoped_refptr safety_flag( - new PendingTaskSafetyFlag(false)); + rtc::scoped_refptr safety_flag = CreateInternal(false); safety_flag->main_sequence_.Detach(); return safety_flag; } diff --git a/rtc_base/task_utils/pending_task_safety_flag.h b/rtc_base/task_utils/pending_task_safety_flag.h index 6446bfe55e..58772bcbb1 100644 --- a/rtc_base/task_utils/pending_task_safety_flag.h +++ b/rtc_base/task_utils/pending_task_safety_flag.h @@ -93,6 +93,8 @@ class PendingTaskSafetyFlag final explicit PendingTaskSafetyFlag(bool alive) : alive_(alive) {} private: + static rtc::scoped_refptr CreateInternal(bool alive); + bool alive_ = true; RTC_NO_UNIQUE_ADDRESS SequenceChecker main_sequence_; };