Fix leaked raw_ptr/raw_ref in BraveOriginService (#34275)

Override KeyedService::Shutdown() to clean up raw_ptr members and
invalidate weak pointers before the service is destroyed. Without
this, the defaulted destructor detects dangling raw_ptr references
to PrefService and PolicyService objects that are already freed
during the profile teardown sequence.

Resolves https://github.com/brave/brave-browser/issues/52757
This commit is contained in:
Netzenbot
2026-03-01 13:11:02 -05:00
committed by GitHub
parent e0f9a2cbaa
commit 95f991747e
2 changed files with 12 additions and 0 deletions
@@ -73,6 +73,15 @@ BraveOriginService::BraveOriginService(
BraveOriginService::~BraveOriginService() = default;
void BraveOriginService::Shutdown() {
weak_ptr_factory_.InvalidateWeakPtrs();
skus_service_.reset();
local_state_ = nullptr;
profile_prefs_ = nullptr;
profile_policy_service_ = nullptr;
browser_policy_service_ = nullptr;
}
bool BraveOriginService::IsPolicyControlledByBraveOrigin(
std::string_view policy_key) const {
if (!IsBraveOriginEnabled()) {
@@ -44,6 +44,9 @@ class BraveOriginService : public KeyedService {
SkusServiceGetter skus_service_getter);
~BraveOriginService() override;
// KeyedService:
void Shutdown() override;
// Check if a policy is controlled by BraveOrigin
bool IsPolicyControlledByBraveOrigin(std::string_view policy_key) const;